1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::error::Error;
use std::fmt;
use std::num::ParseIntError;

/// Custom [`Result<T, E>`] containing a possible [`ParseError`].
pub type ParseResult<T> = Result<T, ParseError>;

/// Error returned during RPSL text to AST parsing failures.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct ParseError {
    msg: String,
    inner: Option<Box<dyn Error + Send + Sync + 'static>>,
}

impl ParseError {
    pub(crate) fn new<S, E>(msg: S, err: Option<E>) -> Self
    where
        S: AsRef<str>,
        E: Error + Send + Sync + 'static,
    {
        let inner = err.map(Into::into);
        Self {
            msg: msg.as_ref().to_string(),
            inner,
        }
    }

    pub(crate) fn from_msg<S>(msg: S) -> Self
    where
        S: AsRef<str>,
    {
        Self {
            msg: msg.as_ref().to_string(),
            inner: None,
        }
    }
}

macro_rules! err {
    ( $msg:literal $(,)? ) => {
        $crate::error::ParseError::from_msg($msg)
    };
    ( $fmt:expr, $( $arg:tt )* ) => {
        $crate::error::ParseError::from_msg(format!($fmt, $($arg)*))
    };
}
pub(crate) use err;

impl Error for ParseError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.inner.as_ref().map(|boxed_err| {
            let err: &(dyn Error) = boxed_err.as_ref();
            err
        })
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(inner_err) = self.source() {
            write!(f, "{}: {}", self.msg, inner_err)
        } else {
            write!(f, "{}", self.msg)
        }
    }
}

impl<R> From<pest::error::Error<R>> for ParseError
where
    R: pest::RuleType + Send + Sync + 'static,
{
    fn from(err: pest::error::Error<R>) -> Self {
        Self::new("failed to parse expression", Some(err))
    }
}

impl From<ParseIntError> for ParseError {
    fn from(err: ParseIntError) -> Self {
        Self::new("failed to parse integer value", Some(err))
    }
}

impl From<ip::Error> for ParseError {
    fn from(err: ip::Error) -> Self {
        Self::new("failed to parse IP address or prefix", Some(err))
    }
}

impl From<time::error::Parse> for ParseError {
    fn from(err: time::error::Parse) -> Self {
        Self::new("failed to parse date string", Some(err))
    }
}

impl From<ValidationError> for ParseError {
    fn from(err: ValidationError) -> Self {
        Self::new("failed to validate object attributes", Some(err))
    }
}

/// Custom [`Result<T, E>`] containing a possible [`ValidationError`].
pub type ValidationResult<T> = Result<T, ValidationError>;

/// Error returned during RPSL object attribute validation failure.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct ValidationError(String);

impl From<String> for ValidationError {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl Error for ValidationError {}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}