lib

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

error.rs (3688B)


      1 use core::fmt;
      2 
      3 #[derive(Debug)]
      4 pub enum EventParseError {
      5     MissingTag(&'static str),
      6     InvalidTag(&'static str),
      7     DuplicateTag(&'static str),
      8     InvalidEnvelope,
      9     InvalidKind { expected: &'static str, got: u32 },
     10     InvalidNumber(&'static str, core::num::ParseIntError),
     11     InvalidJson(&'static str),
     12 }
     13 
     14 impl EventParseError {
     15     pub const fn code(&self) -> &'static str {
     16         match self {
     17             Self::MissingTag(_) => "missing_tag",
     18             Self::InvalidTag(_) => "invalid_tag",
     19             Self::DuplicateTag(_) => "duplicate_tag",
     20             Self::InvalidEnvelope => "invalid_envelope",
     21             Self::InvalidKind { .. } => "invalid_kind",
     22             Self::InvalidNumber(_, _) => "invalid_number",
     23             Self::InvalidJson(_) => "invalid_json",
     24         }
     25     }
     26 }
     27 
     28 impl fmt::Display for EventParseError {
     29     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     30         match self {
     31             EventParseError::MissingTag(t) => write!(f, "missing tag: {}", t),
     32             EventParseError::InvalidTag(t) => write!(f, "invalid tag structure for '{}'", t),
     33             EventParseError::DuplicateTag(t) => write!(f, "duplicate tag: {}", t),
     34             EventParseError::InvalidEnvelope => write!(f, "invalid event envelope"),
     35             EventParseError::InvalidKind { expected, got } => {
     36                 write!(f, "invalid kind {} (expected {})", got, expected)
     37             }
     38             EventParseError::InvalidNumber(t, e) => write!(f, "invalid number in '{}': {}", t, e),
     39             EventParseError::InvalidJson(ctx) => write!(f, "invalid JSON in '{}'", ctx),
     40         }
     41     }
     42 }
     43 
     44 #[cfg(feature = "std")]
     45 impl std::error::Error for EventParseError {
     46     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
     47         match self {
     48             EventParseError::InvalidNumber(_, e) => Some(e),
     49             _ => None,
     50         }
     51     }
     52 }
     53 
     54 impl From<radroots_event::envelope::EventEnvelopeError> for EventParseError {
     55     fn from(_: radroots_event::envelope::EventEnvelopeError) -> Self {
     56         Self::InvalidEnvelope
     57     }
     58 }
     59 
     60 #[derive(Clone, Debug, PartialEq, Eq)]
     61 pub enum EventEncodeError {
     62     InvalidKind(u32),
     63     EmptyRequiredField(&'static str),
     64     InvalidField(&'static str),
     65     Json,
     66 }
     67 
     68 pub type RadrootsEncodeError = EventEncodeError;
     69 
     70 impl EventEncodeError {
     71     pub const fn code(&self) -> &'static str {
     72         match self {
     73             Self::InvalidKind(_) => "invalid_kind",
     74             Self::EmptyRequiredField(_) => "empty_required_field",
     75             Self::InvalidField(_) => "invalid_field",
     76             Self::Json => "json",
     77         }
     78     }
     79 }
     80 
     81 impl fmt::Display for EventEncodeError {
     82     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     83         match self {
     84             EventEncodeError::InvalidKind(kind) => write!(f, "invalid event kind: {}", kind),
     85             EventEncodeError::EmptyRequiredField(field) => {
     86                 write!(f, "empty required field: {}", field)
     87             }
     88             EventEncodeError::InvalidField(field) => write!(f, "invalid field: {}", field),
     89             EventEncodeError::Json => write!(f, "failed to serialize JSON"),
     90         }
     91     }
     92 }
     93 
     94 #[cfg(feature = "std")]
     95 impl std::error::Error for EventEncodeError {}
     96 
     97 #[cfg(test)]
     98 mod tests {
     99     use super::EventParseError;
    100     use radroots_event::envelope::EventEnvelopeError;
    101 
    102     #[test]
    103     #[cfg_attr(coverage_nightly, coverage(off))]
    104     fn invalid_envelope_conversion_preserves_public_error_contract() {
    105         let error = EventParseError::from(EventEnvelopeError::NonCanonicalId);
    106 
    107         assert_eq!(error.code(), "invalid_envelope");
    108         assert_eq!(error.to_string(), "invalid event envelope");
    109     }
    110 }