zlink_core/
error.rs

1use core::str::Utf8Error;
2
3/// The Error type for the zlink crate.
4#[derive(Debug)]
5#[non_exhaustive]
6pub enum Error {
7    /// An error occurred while reading from the socket.
8    SocketRead,
9    /// An error occurred while writing to the socket.
10    SocketWrite,
11    /// Buffer overflow.
12    BufferOverflow,
13    /// Invalid UTF-8 data.
14    InvalidUtf8(Utf8Error),
15    /// Error serializing or deserializing to/from JSON.
16    #[cfg(feature = "std")]
17    Json(serde_json::Error),
18    /// Error serialization to JSON.
19    #[cfg(not(feature = "std"))]
20    JsonSerialize(serde_json_core::ser::Error),
21    /// Error deserialization from JSON.
22    #[cfg(not(feature = "std"))]
23    JsonDeserialize(serde_json_core::de::Error),
24    /// An I/O error.
25    #[cfg(feature = "std")]
26    Io(std::io::Error),
27    /// An error occurred while parsing IDL.
28    #[cfg(feature = "idl-parse")]
29    IdlParse(String),
30    /// Missing required parameters.
31    MissingParameters,
32    /// A general service error.
33    VarlinkService(crate::varlink_service::Error),
34}
35
36/// The Result type for the zlink crate.
37pub type Result<T> = core::result::Result<T, Error>;
38
39impl core::error::Error for Error {
40    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
41        match self {
42            #[cfg(feature = "std")]
43            Error::Json(e) => Some(e),
44            #[cfg(not(feature = "std"))]
45            Error::JsonSerialize(e) => Some(e),
46            #[cfg(not(feature = "std"))]
47            Error::JsonDeserialize(e) => Some(e),
48            #[cfg(feature = "std")]
49            Error::Io(e) => Some(e),
50            Error::InvalidUtf8(e) => Some(e),
51            #[cfg(feature = "idl-parse")]
52            Error::IdlParse(_) => None,
53            Error::VarlinkService(e) => Some(e),
54            _ => None,
55        }
56    }
57}
58
59#[cfg(feature = "std")]
60impl From<serde_json::Error> for Error {
61    fn from(e: serde_json::Error) -> Self {
62        Error::Json(e)
63    }
64}
65
66#[cfg(not(feature = "std"))]
67impl From<serde_json_core::ser::Error> for Error {
68    fn from(e: serde_json_core::ser::Error) -> Self {
69        Error::JsonSerialize(e)
70    }
71}
72
73#[cfg(not(feature = "std"))]
74impl From<serde_json_core::de::Error> for Error {
75    fn from(e: serde_json_core::de::Error) -> Self {
76        Error::JsonDeserialize(e)
77    }
78}
79
80#[cfg(feature = "std")]
81impl From<std::io::Error> for Error {
82    fn from(e: std::io::Error) -> Self {
83        Error::Io(e)
84    }
85}
86
87impl From<mayheap::Error> for Error {
88    fn from(e: mayheap::Error) -> Self {
89        match e {
90            mayheap::Error::BufferOverflow => Error::BufferOverflow,
91            mayheap::Error::Utf8Error(e) => Error::InvalidUtf8(e),
92        }
93    }
94}
95
96impl core::fmt::Display for Error {
97    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
98        match self {
99            Error::SocketRead => write!(f, "An error occurred while reading from the socket"),
100            Error::SocketWrite => write!(f, "An error occurred while writing to the socket"),
101            Error::BufferOverflow => write!(f, "Buffer overflow"),
102            Error::InvalidUtf8(e) => write!(f, "Invalid UTF-8 data: {e}"),
103            #[cfg(feature = "std")]
104            Error::Json(e) => write!(f, "Error serializing or deserializing to/from JSON: {e}"),
105            #[cfg(not(feature = "std"))]
106            Error::JsonSerialize(e) => write!(f, "Error serializing to JSON: {e}"),
107            #[cfg(not(feature = "std"))]
108            Error::JsonDeserialize(e) => write!(f, "Error deserializing from JSON: {e}"),
109            #[cfg(feature = "std")]
110            Error::Io(e) => write!(f, "I/O error: {e}"),
111            #[cfg(feature = "idl-parse")]
112            Error::IdlParse(e) => write!(f, "IDL parse error: {e}"),
113            Error::MissingParameters => write!(f, "Missing required parameters"),
114            Error::VarlinkService(e) => write!(f, "{e}"),
115        }
116    }
117}
118
119#[cfg(feature = "defmt")]
120impl defmt::Format for Error {
121    fn format(&self, fmt: defmt::Formatter<'_>) {
122        match self {
123            Error::SocketRead => {
124                defmt::write!(fmt, "An error occurred while reading from the socket")
125            }
126            Error::SocketWrite => {
127                defmt::write!(fmt, "An error occurred while writing to the socket")
128            }
129            Error::BufferOverflow => defmt::write!(fmt, "Buffer overflow"),
130            Error::InvalidUtf8(_) => defmt::write!(fmt, "Invalid UTF-8 data"),
131            #[cfg(feature = "std")]
132            Error::Json(_) => {
133                defmt::write!(fmt, "Error serializing or deserializing to/from JSON")
134            }
135            #[cfg(not(feature = "std"))]
136            Error::JsonSerialize(_) => defmt::write!(fmt, "Error serializing to JSON"),
137            #[cfg(not(feature = "std"))]
138            Error::JsonDeserialize(_) => defmt::write!(fmt, "Error deserializing from JSON"),
139            #[cfg(feature = "std")]
140            Error::Io(_) => defmt::write!(fmt, "I/O error"),
141            #[cfg(feature = "idl-parse")]
142            Error::IdlParse(_) => defmt::write!(fmt, "IDL parse error"),
143            Error::MissingParameters => defmt::write!(fmt, "Missing required parameters"),
144            Error::VarlinkService(_) => defmt::write!(fmt, "Varlink service error"),
145        }
146    }
147}