1use core::str::Utf8Error;
2
3#[derive(Debug)]
5#[non_exhaustive]
6pub enum Error {
7 SocketRead,
9 SocketWrite,
11 BufferOverflow,
13 InvalidUtf8(Utf8Error),
15 Json(serde_json::Error),
17 #[cfg(feature = "std")]
19 Io(std::io::Error),
20 UnexpectedEof,
22 #[cfg(feature = "idl-parse")]
24 IdlParse(alloc::string::String),
25 MissingParameters,
27 VarlinkService(crate::varlink_service::OwnedError),
29 EmptyChain,
31}
32
33pub 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
64impl core::fmt::Display for Error {
65 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
66 match self {
67 Error::SocketRead => write!(f, "An error occurred while reading from the socket"),
68 Error::SocketWrite => write!(f, "An error occurred while writing to the socket"),
69 Error::BufferOverflow => write!(f, "Buffer overflow"),
70 Error::InvalidUtf8(e) => write!(f, "Invalid UTF-8 data: {e}"),
71 Error::Json(e) => write!(f, "Error serializing or deserializing to/from JSON: {e}"),
72 #[cfg(feature = "std")]
73 Error::Io(e) => write!(f, "I/O error: {e}"),
74 Error::UnexpectedEof => write!(f, "Unexpected end of file/stream"),
75 #[cfg(feature = "idl-parse")]
76 Error::IdlParse(e) => write!(f, "IDL parse error: {e}"),
77 Error::MissingParameters => write!(f, "Missing required parameters"),
78 Error::VarlinkService(e) => write!(f, "{e}"),
79 Error::EmptyChain => write!(f, "Cannot create a chain from an empty iterator or slice"),
80 }
81 }
82}
83
84#[cfg(feature = "defmt")]
85impl defmt::Format for Error {
86 fn format(&self, fmt: defmt::Formatter<'_>) {
87 match self {
88 Error::SocketRead => {
89 defmt::write!(fmt, "An error occurred while reading from the socket")
90 }
91 Error::SocketWrite => {
92 defmt::write!(fmt, "An error occurred while writing to the socket")
93 }
94 Error::BufferOverflow => defmt::write!(fmt, "Buffer overflow"),
95 Error::InvalidUtf8(_) => defmt::write!(fmt, "Invalid UTF-8 data"),
96 Error::Json(_) => {
97 defmt::write!(fmt, "Error serializing or deserializing to/from JSON")
98 }
99 #[cfg(feature = "std")]
100 Error::Io(_) => defmt::write!(fmt, "I/O error"),
101 Error::UnexpectedEof => defmt::write!(fmt, "I/O error"),
102 #[cfg(feature = "idl-parse")]
103 Error::IdlParse(_) => defmt::write!(fmt, "IDL parse error"),
104 Error::MissingParameters => defmt::write!(fmt, "Missing required parameters"),
105 Error::VarlinkService(_) => defmt::write!(fmt, "Varlink service error"),
106 Error::EmptyChain => {
107 defmt::write!(fmt, "Cannot create a chain from an empty iterator or slice")
108 }
109 }
110 }
111}