Skip to main content

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    Json(serde_json::Error),
17    /// An I/O error.
18    #[cfg(feature = "std")]
19    Io(std::io::Error),
20    /// Unexpected end of file/stream.
21    UnexpectedEof,
22    /// An error occurred while parsing IDL.
23    #[cfg(feature = "idl-parse")]
24    IdlParse(alloc::string::String),
25    /// Missing required parameters.
26    MissingParameters,
27    /// A general service error.
28    VarlinkService(crate::varlink_service::OwnedError),
29    /// Empty chain creation attempt.
30    EmptyChain,
31}
32
33/// The Result type for the zlink crate.
34pub type Result<T> = core::result::Result<T, Error>;
35
36impl core::error::Error for Error {
37    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
38        match self {
39            Error::Json(e) => Some(e),
40            #[cfg(feature = "std")]
41            Error::Io(e) => Some(e),
42            Error::InvalidUtf8(e) => Some(e),
43            #[cfg(feature = "idl-parse")]
44            Error::IdlParse(_) => None,
45            Error::VarlinkService(e) => Some(e),
46            _ => None,
47        }
48    }
49}
50
51impl From<serde_json::Error> for Error {
52    fn from(e: serde_json::Error) -> Self {
53        Error::Json(e)
54    }
55}
56
57#[cfg(feature = "std")]
58impl From<std::io::Error> for Error {
59    fn from(e: std::io::Error) -> Self {
60        Error::Io(e)
61    }
62}
63
64#[cfg(feature = "idl-parse")]
65impl From<zlink_idl::parse::Error> for Error {
66    fn from(e: zlink_idl::parse::Error) -> Self {
67        use alloc::string::ToString;
68
69        Error::IdlParse(e.to_string())
70    }
71}
72
73impl core::fmt::Display for Error {
74    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75        match self {
76            Error::SocketRead => write!(f, "An error occurred while reading from the socket"),
77            Error::SocketWrite => write!(f, "An error occurred while writing to the socket"),
78            Error::BufferOverflow => write!(f, "Buffer overflow"),
79            Error::InvalidUtf8(e) => write!(f, "Invalid UTF-8 data: {e}"),
80            Error::Json(e) => write!(f, "Error serializing or deserializing to/from JSON: {e}"),
81            #[cfg(feature = "std")]
82            Error::Io(e) => write!(f, "I/O error: {e}"),
83            Error::UnexpectedEof => write!(f, "Unexpected end of file/stream"),
84            #[cfg(feature = "idl-parse")]
85            Error::IdlParse(e) => write!(f, "IDL parse error: {e}"),
86            Error::MissingParameters => write!(f, "Missing required parameters"),
87            Error::VarlinkService(e) => write!(f, "{e}"),
88            Error::EmptyChain => write!(f, "Cannot create a chain from an empty iterator or slice"),
89        }
90    }
91}
92
93#[cfg(feature = "defmt")]
94impl defmt::Format for Error {
95    fn format(&self, fmt: defmt::Formatter<'_>) {
96        match self {
97            Error::SocketRead => {
98                defmt::write!(fmt, "An error occurred while reading from the socket")
99            }
100            Error::SocketWrite => {
101                defmt::write!(fmt, "An error occurred while writing to the socket")
102            }
103            Error::BufferOverflow => defmt::write!(fmt, "Buffer overflow"),
104            Error::InvalidUtf8(_) => defmt::write!(fmt, "Invalid UTF-8 data"),
105            Error::Json(_) => {
106                defmt::write!(fmt, "Error serializing or deserializing to/from JSON")
107            }
108            #[cfg(feature = "std")]
109            Error::Io(_) => defmt::write!(fmt, "I/O error"),
110            Error::UnexpectedEof => defmt::write!(fmt, "I/O error"),
111            #[cfg(feature = "idl-parse")]
112            Error::IdlParse(_) => defmt::write!(fmt, "IDL parse error"),
113            Error::MissingParameters => defmt::write!(fmt, "Missing required parameters"),
114            Error::VarlinkService(_) => defmt::write!(fmt, "Varlink service error"),
115            Error::EmptyChain => {
116                defmt::write!(fmt, "Cannot create a chain from an empty iterator or slice")
117            }
118        }
119    }
120}