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
|
// Copyright 2022-2024 Gabriel Bjørnager Jensen.
use std::error::Error as StdError;
use std::fmt::{Display, Formatter};
use std::process::{ExitCode, Termination};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
ConfigError { message: String },
IllegalArgument { arg: String },
IllegalFieldValue { field: &'static str, value: String, source: Box<dyn StdError> },
MissingConfig,
MissingField { field: &'static str },
NetworkError { source: std::io::Error },
SerialiseError { source: bzipper::Error },
}
impl Display for Error {
#[inline]
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
use Error::*;
match *self {
ConfigError { ref message }
=> write!(f, "unable to load configuration: \"{message}\""),
IllegalArgument { ref arg }
=> write!(f, "illegal argument `{arg}` provided"),
IllegalFieldValue { field, ref value, ref source }
=> write!(f, "illegal configuration value {value:?} for field `{field}`: \"{source}\""),
MissingConfig
=> write!(f, "no configuration provided"),
MissingField { field }
=> write!(f, "missing configuration field `{field}`"),
NetworkError { ref source }
=> write!(f, "network error: \"{source}\""),
SerialiseError { ref source }
=> write!(f, "error when serialising response: \"{source}\""),
}
}
}
impl StdError for Error {
#[allow(clippy::match_same_arms)]
#[inline]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
use Error::*;
match *self {
IllegalFieldValue { ref source, .. } => Some(source.as_ref()),
NetworkError { ref source } => Some(source),
SerialiseError { ref source } => Some(source),
_ => None,
}
}
}
impl Termination for Error {
#[inline(always)]
fn report(self) -> ExitCode {
use Error::*;
match self {
| ConfigError { .. }
| IllegalArgument { .. }
| IllegalFieldValue { .. }
| MissingConfig
| MissingField { .. }
=> 0x2,
_ => 0x1,
}.into()
}
}
|