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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use tor_error::{ErrorKind, HasKind};
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigBuildError {
#[error("Field was not provided: {field}")]
MissingField {
field: String,
},
#[error("Value of {field} was incorrect: {problem}")]
Invalid {
field: String,
problem: String,
},
#[error("Fields {fields:?} are inconsistent: {problem}")]
Inconsistent {
fields: Vec<String>,
problem: String,
},
}
impl From<derive_builder::UninitializedFieldError> for ConfigBuildError {
fn from(val: derive_builder::UninitializedFieldError) -> Self {
ConfigBuildError::MissingField {
field: val.field_name().to_string(),
}
}
}
impl From<derive_builder::SubfieldBuildError<ConfigBuildError>> for ConfigBuildError {
fn from(e: derive_builder::SubfieldBuildError<ConfigBuildError>) -> Self {
let (field, problem) = e.into_parts();
problem.within(field)
}
}
impl ConfigBuildError {
#[must_use]
pub fn within(&self, prefix: &str) -> Self {
use ConfigBuildError::*;
match self {
MissingField { field } => MissingField {
field: format!("{}.{}", prefix, field),
},
Invalid { field, problem } => Invalid {
field: format!("{}.{}", prefix, field),
problem: problem.clone(),
},
Inconsistent { fields, problem } => Inconsistent {
fields: fields.iter().map(|f| format!("{}.{}", prefix, f)).collect(),
problem: problem.clone(),
},
}
}
}
impl HasKind for ConfigBuildError {
fn kind(&self) -> ErrorKind {
ErrorKind::InvalidConfig
}
}
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
pub enum ReconfigureError {
#[error("Cannot change {field} on a running client.")]
CannotChange {
field: String,
},
}
impl HasKind for ReconfigureError {
fn kind(&self) -> ErrorKind {
ErrorKind::InvalidConfigTransition
}
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn within() {
let e1 = ConfigBuildError::MissingField {
field: "lettuce".to_owned(),
};
let e2 = ConfigBuildError::Invalid {
field: "tomato".to_owned(),
problem: "too crunchy".to_owned(),
};
let e3 = ConfigBuildError::Inconsistent {
fields: vec!["mayo".to_owned(), "avocado".to_owned()],
problem: "pick one".to_owned(),
};
assert_eq!(
&e1.within("sandwich").to_string(),
"Field was not provided: sandwich.lettuce"
);
assert_eq!(
&e2.within("sandwich").to_string(),
"Value of sandwich.tomato was incorrect: too crunchy"
);
assert_eq!(
&e3.within("sandwich").to_string(),
r#"Fields ["sandwich.mayo", "sandwich.avocado"] are inconsistent: pick one"#
);
}
#[derive(derive_builder::Builder, Debug, Clone)]
#[builder(build_fn(error = "ConfigBuildError"))]
#[allow(dead_code)]
struct Cephalopod {
arms: u8,
tentacles: u8,
}
#[test]
fn build_err() {
let squid = CephalopodBuilder::default().arms(8).tentacles(2).build();
let octopus = CephalopodBuilder::default().arms(8).build();
assert!(squid.is_ok());
let squid = squid.unwrap();
assert_eq!(squid.arms, 8);
assert_eq!(squid.tentacles, 2);
assert!(octopus.is_err());
assert_eq!(
&octopus.unwrap_err().to_string(),
"Field was not provided: tentacles"
);
}
#[derive(derive_builder::Builder, Debug)]
#[builder(build_fn(error = "ConfigBuildError"))]
#[allow(dead_code)]
struct Pet {
#[builder(sub_builder)]
best_friend: Cephalopod,
}
#[test]
fn build_subfield_err() {
let mut petb = PetBuilder::default();
petb.best_friend().tentacles(3);
let pet = petb.build();
assert_eq!(
pet.unwrap_err().to_string(),
"Field was not provided: best_friend.arms"
);
}
}