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    #[expect(
31        clippy::missing_const_for_fn,
32        reason = "False-positive, `<String as Deref>::deref` is not const"
33    )]
34    fn field_data(&self) -> &str {
35        match self {
36            Self::IO(data) | Self::Format(data) => data,
37            Self::Value(data) => data,
38        }
39    }
40}
41
42impl Debug for Error {
43    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
44        f.debug_struct("Error")
45            .field(self.field_name(), &self.field_data())
46            .finish()
47    }
48}
49
50impl From<TryFromIntError> for Error {
51    fn from(_: TryFromIntError) -> Self {
52        Self::Value("Int is too large".to_string())
53    }
54}
55
56impl From<Utf8Error> for Error {
57    fn from(_: Utf8Error) -> Self {
58        Self::Value("Invalid utf8 str".to_string())
59    }
60}