epee_encoding/
error.rs

1use core::fmt::{Debug, Formatter};
2use core::num::TryFromIntError;
3
4pub type Result<T> = core::result::Result<T, Error>;
5
6#[cfg_attr(feature = "std", derive(thiserror::Error))]
7pub enum Error {
8    #[cfg_attr(feature = "std", error("IO error: {0}"))]
9    IO(&'static str),
10    #[cfg_attr(feature = "std", error("Format error: {0}"))]
11    Format(&'static str),
12    #[cfg_attr(feature = "std", error("Value error: {0}"))]
13    Value(&'static str),
14}
15
16impl Error {
17    fn field_name(&self) -> &'static str {
18        match self {
19            Error::IO(_) => "io",
20            Error::Format(_) => "format",
21            Error::Value(_) => "value",
22        }
23    }
24
25    fn field_data(&self) -> &'static str {
26        match self {
27            Error::IO(data) => data,
28            Error::Format(data) => data,
29            Error::Value(data) => data,
30        }
31    }
32}
33
34impl Debug for Error {
35    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
36        f.debug_struct("Error")
37            .field(self.field_name(), &self.field_data())
38            .finish()
39    }
40}
41
42impl From<TryFromIntError> for Error {
43    fn from(_: TryFromIntError) -> Self {
44        Error::Value("Int is too large")
45    }
46}