cuprate_epee_encoding/
error.rs

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