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