1 //! This module contain the error from the VirtIO socket driver.
2 
3 use core::{fmt, result};
4 
5 /// The error type of VirtIO socket driver.
6 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
7 pub enum SocketError {
8     /// There is an existing connection.
9     ConnectionExists,
10     /// Failed to establish the connection.
11     ConnectionFailed,
12     /// The device is not connected to any peer.
13     NotConnected,
14     /// Peer socket is shutdown.
15     PeerSocketShutdown,
16     /// No response received.
17     NoResponseReceived,
18     /// The given buffer is shorter than expected.
19     BufferTooShort,
20     /// The given buffer for output is shorter than expected.
21     OutputBufferTooShort(usize),
22     /// The given buffer has exceeded the maximum buffer size.
23     BufferTooLong(usize, usize),
24     /// Unknown operation.
25     UnknownOperation(u16),
26     /// Invalid operation,
27     InvalidOperation,
28     /// Invalid number.
29     InvalidNumber,
30     /// Unexpected data in packet.
31     UnexpectedDataInPacket,
32     /// Peer has insufficient buffer space, try again later.
33     InsufficientBufferSpaceInPeer,
34     /// Recycled a wrong buffer.
35     RecycledWrongBuffer,
36 }
37 
38 impl fmt::Display for SocketError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result39     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40         match self {
41             Self::ConnectionExists => write!(
42                 f,
43                 "There is an existing connection. Please close the current connection before attempting to connect again."),
44             Self::ConnectionFailed => write!(
45                 f, "Failed to establish the connection. The packet sent may have an unknown type value"
46             ),
47             Self::NotConnected => write!(f, "The device is not connected to any peer. Please connect it to a peer first."),
48             Self::PeerSocketShutdown => write!(f, "The peer socket is shutdown."),
49             Self::NoResponseReceived => write!(f, "No response received"),
50             Self::BufferTooShort => write!(f, "The given buffer is shorter than expected"),
51             Self::BufferTooLong(actual, max) => {
52                 write!(f, "The given buffer length '{actual}' has exceeded the maximum allowed buffer length '{max}'")
53             }
54             Self::OutputBufferTooShort(expected) => {
55                 write!(f, "The given output buffer is too short. '{expected}' bytes is needed for the output buffer.")
56             }
57             Self::UnknownOperation(op) => {
58                 write!(f, "The operation code '{op}' is unknown")
59             }
60             Self::InvalidOperation => write!(f, "Invalid operation"),
61             Self::InvalidNumber => write!(f, "Invalid number"),
62             Self::UnexpectedDataInPacket => write!(f, "No data is expected in the packet"),
63             Self::InsufficientBufferSpaceInPeer => write!(f, "Peer has insufficient buffer space, try again later"),
64             Self::RecycledWrongBuffer => write!(f, "Recycled a wrong buffer"),
65         }
66     }
67 }
68 
69 pub type Result<T> = result::Result<T, SocketError>;
70