lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

error.rs (1949B)


      1 use core::fmt;
      2 
      3 #[derive(Debug)]
      4 pub enum EventParseError {
      5     MissingTag(&'static str),
      6     InvalidTag(&'static str),
      7     InvalidKind { expected: &'static str, got: u32 },
      8     InvalidNumber(&'static str, core::num::ParseIntError),
      9     InvalidJson(&'static str),
     10 }
     11 
     12 impl fmt::Display for EventParseError {
     13     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     14         match self {
     15             EventParseError::MissingTag(t) => write!(f, "missing tag: {}", t),
     16             EventParseError::InvalidTag(t) => write!(f, "invalid tag structure for '{}'", t),
     17             EventParseError::InvalidKind { expected, got } => {
     18                 write!(f, "invalid kind {} (expected {})", got, expected)
     19             }
     20             EventParseError::InvalidNumber(t, e) => write!(f, "invalid number in '{}': {}", t, e),
     21             EventParseError::InvalidJson(ctx) => write!(f, "invalid JSON in '{}'", ctx),
     22         }
     23     }
     24 }
     25 
     26 #[cfg(feature = "std")]
     27 impl std::error::Error for EventParseError {
     28     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
     29         match self {
     30             EventParseError::InvalidNumber(_, e) => Some(e),
     31             _ => None,
     32         }
     33     }
     34 }
     35 
     36 #[derive(Debug)]
     37 pub enum EventEncodeError {
     38     InvalidKind(u32),
     39     EmptyRequiredField(&'static str),
     40     InvalidField(&'static str),
     41     Json,
     42 }
     43 
     44 impl fmt::Display for EventEncodeError {
     45     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     46         match self {
     47             EventEncodeError::InvalidKind(kind) => write!(f, "invalid event kind: {}", kind),
     48             EventEncodeError::EmptyRequiredField(field) => {
     49                 write!(f, "empty required field: {}", field)
     50             }
     51             EventEncodeError::InvalidField(field) => write!(f, "invalid field: {}", field),
     52             EventEncodeError::Json => write!(f, "failed to serialize JSON"),
     53         }
     54     }
     55 }
     56 
     57 #[cfg(feature = "std")]
     58 impl std::error::Error for EventEncodeError {}