ockam_core/routing/
error.rs

1use crate::{
2    errcode::{Kind, Origin},
3    Error,
4};
5use core::fmt::{self, Debug, Display};
6
7/// A routing specific error type.
8#[derive(Clone, Copy, Debug)]
9pub enum RouteError {
10    /// Message had an incomplete route
11    IncompleteRoute,
12}
13
14impl From<RouteError> for Error {
15    #[track_caller]
16    fn from(err: RouteError) -> Self {
17        let kind = match err {
18            RouteError::IncompleteRoute => Kind::Misuse,
19        };
20        Error::new(Origin::Core, kind, err)
21    }
22}
23
24impl crate::compat::error::Error for RouteError {}
25impl Display for RouteError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            RouteError::IncompleteRoute => write!(f, "incomplete route"),
29        }
30    }
31}
32
33/// An error which is returned when address parsing from string fails.
34#[derive(Debug)]
35pub struct AddressParseError {
36    kind: AddressParseErrorKind,
37}
38
39/// Enum to store the cause of an address parsing failure.
40#[derive(Debug, Clone, PartialEq, Eq)]
41#[non_exhaustive]
42pub enum AddressParseErrorKind {
43    /// Unable to parse address num in the address string.
44    InvalidType(core::num::ParseIntError),
45    /// Address string has more than one '#' separator.
46    MultipleSep,
47}
48
49impl AddressParseError {
50    /// Create new address parse error instance.
51    pub fn new(kind: AddressParseErrorKind) -> Self {
52        Self { kind }
53    }
54    /// Return the cause of the address parsing failure.
55    pub fn kind(&self) -> &AddressParseErrorKind {
56        &self.kind
57    }
58}
59
60impl Display for AddressParseError {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match &self.kind {
63            AddressParseErrorKind::InvalidType(e) => {
64                write!(f, "Failed to parse address type: '{}'", e)
65            }
66            AddressParseErrorKind::MultipleSep => {
67                write!(
68                    f,
69                    "Invalid address string: more than one '#' separator found"
70                )
71            }
72        }
73    }
74}
75
76impl crate::compat::error::Error for AddressParseError {}