lib

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

error.rs (4299B)


      1 use alloc::string::String;
      2 use core::fmt;
      3 use radroots_simplex_smp_proto::prelude::RadrootsSimplexSmpProtoError;
      4 
      5 #[derive(Debug, Clone, PartialEq, Eq)]
      6 pub enum RadrootsSimplexAgentUnsupportedLinkKind {
      7     FullContactLink,
      8     ContactAddress,
      9     Group,
     10     Channel,
     11     Relay,
     12     File,
     13     Xrcp,
     14     Bot,
     15     Unknown(String),
     16 }
     17 
     18 #[derive(Debug, Clone, PartialEq, Eq)]
     19 pub enum RadrootsSimplexAgentProtoError {
     20     Proto(RadrootsSimplexSmpProtoError),
     21     UnexpectedEof,
     22     InvalidTag(String),
     23     InvalidUtf8(String),
     24     InvalidShortFieldLength(usize),
     25     InvalidLargeFieldLength(usize),
     26     InvalidBoolEncoding(u8),
     27     InvalidRatchetHeader(String),
     28     InvalidE2eParameters(String),
     29     InvalidBase64Url {
     30         field: &'static str,
     31         value: String,
     32     },
     33     InvalidLink(String),
     34     InvalidLinkFieldLength {
     35         field: &'static str,
     36         expected: usize,
     37         actual: usize,
     38     },
     39     InvalidLinkParameter {
     40         key: String,
     41         reason: String,
     42     },
     43     InvalidPort(String),
     44     UnsupportedLink(RadrootsSimplexAgentUnsupportedLinkKind),
     45     TrailingBytes,
     46 }
     47 
     48 impl From<RadrootsSimplexSmpProtoError> for RadrootsSimplexAgentProtoError {
     49     fn from(value: RadrootsSimplexSmpProtoError) -> Self {
     50         Self::Proto(value)
     51     }
     52 }
     53 
     54 impl fmt::Display for RadrootsSimplexAgentProtoError {
     55     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     56         match self {
     57             Self::Proto(error) => write!(f, "{error}"),
     58             Self::UnexpectedEof => write!(f, "unexpected end of SimpleX agent input"),
     59             Self::InvalidTag(tag) => write!(f, "invalid SimpleX agent tag `{tag}`"),
     60             Self::InvalidUtf8(error) => write!(f, "invalid UTF-8 in SimpleX agent field: {error}"),
     61             Self::InvalidShortFieldLength(length) => {
     62                 write!(f, "invalid SimpleX agent short field length {length}")
     63             }
     64             Self::InvalidLargeFieldLength(length) => {
     65                 write!(f, "invalid SimpleX agent large field length {length}")
     66             }
     67             Self::InvalidBoolEncoding(value) => {
     68                 write!(f, "invalid SimpleX agent bool encoding `{value}`")
     69             }
     70             Self::InvalidRatchetHeader(error) => {
     71                 write!(f, "invalid SimpleX agent ratchet header: {error}")
     72             }
     73             Self::InvalidE2eParameters(error) => {
     74                 write!(f, "invalid SimpleX agent E2E parameters: {error}")
     75             }
     76             Self::InvalidBase64Url { field, value } => {
     77                 write!(
     78                     f,
     79                     "invalid SimpleX agent base64url value for `{field}`: `{value}`"
     80                 )
     81             }
     82             Self::InvalidLink(link) => write!(f, "invalid SimpleX agent link: {link}"),
     83             Self::InvalidLinkFieldLength {
     84                 field,
     85                 expected,
     86                 actual,
     87             } => {
     88                 write!(
     89                     f,
     90                     "invalid SimpleX agent link `{field}` length {actual}, expected {expected}"
     91                 )
     92             }
     93             Self::InvalidLinkParameter { key, reason } => {
     94                 write!(f, "invalid SimpleX agent link parameter `{key}`: {reason}")
     95             }
     96             Self::InvalidPort(port) => write!(f, "invalid SimpleX agent link port `{port}`"),
     97             Self::UnsupportedLink(kind) => {
     98                 write!(f, "unsupported SimpleX agent link kind `{kind}`")
     99             }
    100             Self::TrailingBytes => write!(f, "trailing bytes after SimpleX agent decode"),
    101         }
    102     }
    103 }
    104 
    105 impl fmt::Display for RadrootsSimplexAgentUnsupportedLinkKind {
    106     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    107         match self {
    108             Self::FullContactLink => write!(f, "full-contact-link"),
    109             Self::ContactAddress => write!(f, "contact-address"),
    110             Self::Group => write!(f, "group"),
    111             Self::Channel => write!(f, "channel"),
    112             Self::Relay => write!(f, "relay"),
    113             Self::File => write!(f, "file"),
    114             Self::Xrcp => write!(f, "xrcp"),
    115             Self::Bot => write!(f, "bot"),
    116             Self::Unknown(value) => write!(f, "unknown:{value}"),
    117         }
    118     }
    119 }
    120 
    121 #[cfg(feature = "std")]
    122 impl std::error::Error for RadrootsSimplexAgentProtoError {}