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
use std::{error::Error, fmt};
#[derive(Debug)]
pub struct VariableError {
pub content: String,
pub kind: VariableErrorKind,
}
#[derive(Debug)]
pub enum VariableErrorKind {
InvalidAddress,
InvalidDivert { address: String },
InvalidNumericValue { err: Box<dyn Error + 'static> },
}
impl Error for VariableError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.kind)
}
}
impl Error for VariableErrorKind {}
impl fmt::Display for VariableError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} in variable string '{}'", self.kind, self.content)
}
}
impl fmt::Display for VariableErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use VariableErrorKind::*;
match &self {
InvalidAddress => write!(f, "invalid address"),
InvalidDivert { address } => write!(f, "invalid divert address '{}'", address),
InvalidNumericValue { .. } => write!(f, "could not parse number"),
}
}
}