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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use crate::configuration::{CompoundKey, Key, NodeType};
use serde::de;
use std::{convert::From, fmt::Display, ops::Deref};
#[derive(Debug)]
pub struct ConfigurationError {
inner: Box<ErrorImpl>,
}
#[derive(Debug)]
pub struct ErrorImpl {
code: ErrorCode,
context: Option<Vec<String>>,
path: Option<Vec<Key>>,
}
#[derive(Debug)]
pub enum ErrorCode {
WrongNodeType(NodeType, NodeType),
WrongValueType(String, String),
IndexOutOfRange(usize),
WrongKeyType(NodeType, String),
KeyNotFound(String),
BadNodeMerge(NodeType, NodeType),
IoError(std::io::Error),
DeserializationError(String),
EmptyConfiguration,
NullValue,
ParsingError(String),
}
impl ConfigurationError {
pub fn inner(&self) -> &ErrorCode {
&self.inner.code
}
pub fn enrich_with_context<T: Display>(mut self, message: T) -> Self {
match self.inner.context {
Some(ref mut context) => context.push(message.to_string()),
None => {
self.inner.context = Some(vec![message.to_string()]);
}
}
self
}
pub fn enrich_with_key(mut self, key: Key) -> Self {
match self.inner.path {
Some(ref mut path) => {
path.push(key);
}
None => {
self.inner.path = Some(vec![key]);
}
}
self
}
pub fn enrich_with_keys(mut self, keys: &CompoundKey) -> Self {
if self.inner.path.is_none() {
self.inner.path = Some(Vec::new());
}
let path = self.inner.path.as_mut().unwrap();
for key in keys.iter().rev() {
path.push(key.clone());
}
self
}
pub fn pretty_display(&self) -> PrettyConfigurationDisplay {
PrettyConfigurationDisplay(self)
}
}
impl ErrorImpl {
pub fn get_code(&self) -> &ErrorCode {
&self.code
}
pub fn get_path(&self) -> Option<&[Key]> {
if let Some(ref path) = self.path {
Some(path)
} else {
None
}
}
pub fn get_context(&self) -> Option<&[String]> {
if let Some(ref context) = self.context {
Some(context)
} else {
None
}
}
}
struct KeyVec<'v>(&'v [Key]);
impl<'v> Display for KeyVec<'v> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut first = true;
for key in self.0.iter().rev() {
if first {
write!(f, "{}", key)?;
first = false;
continue;
}
write!(f, "-->{}", key)?;
}
Ok(())
}
}
impl Display for ConfigurationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.inner.code)?;
if let Some(ref path) = self.inner.path {
write!(f, ". Path : {}", KeyVec(path))?;
}
if let Some(ref context) = self.inner.context {
write!(f, ". Context: ")?;
for msg in context.iter() {
write!(f, "| {} |", msg)?;
}
}
Ok(())
}
}
pub struct PrettyConfigurationDisplay<'e>(&'e ConfigurationError);
impl<'e> Display for PrettyConfigurationDisplay<'e> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", &self.0.inner.code)?;
if let Some(ref path) = self.0.inner.path {
writeln!(f, "Path : {}", KeyVec(path))?;
}
if let Some(ref context) = self.0.inner.context {
writeln!(f, "Context: ")?;
for msg in context.iter() {
writeln!(f, "\t{}", msg)?;
}
}
Ok(())
}
}
impl Display for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ErrorCode::WrongNodeType(exp, act) => {
write!(f, "Unexpected node type. Expected {}, got {}", exp, act)
}
ErrorCode::WrongValueType(exp, act) => {
write!(f, "Unexpected value type. Expected {}, got {}", exp, act)
}
ErrorCode::IndexOutOfRange(i) => write!(f, "Index {} exceeds bounds of the array.", i),
ErrorCode::WrongKeyType(ntype, k) => {
write!(f, "Cannot access {} with key `{}`", ntype, k)
}
ErrorCode::KeyNotFound(k) => write!(f, "Unable to find key {}.", k),
ErrorCode::BadNodeMerge(a, b) => {
write!(f, "It is forbidden to substitute {} for {}", b, a)
}
ErrorCode::IoError(e) => write!(f, "I/O error occurred. {}", e),
ErrorCode::DeserializationError(e) => write!(f, "Deserialization error occured. {}", e),
ErrorCode::NullValue => write!(f, "Expected non-null value"),
ErrorCode::EmptyConfiguration => write!(f, "Expected non-empty configuration"),
ErrorCode::ParsingError(msg) => write!(f, "Parsing error. {}", msg),
}
}
}
impl Deref for ConfigurationError {
type Target = ErrorImpl;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::error::Error for ConfigurationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.inner.code)
}
}
impl std::error::Error for ErrorCode {}
impl From<std::io::Error> for ConfigurationError {
fn from(e: std::io::Error) -> Self {
ConfigurationError::from(ErrorCode::IoError(e))
}
}
impl From<ErrorCode> for ConfigurationError {
fn from(e: ErrorCode) -> Self {
ConfigurationError {
inner: Box::new(ErrorImpl {
code: e,
context: None,
path: None,
}),
}
}
}
impl de::Error for ConfigurationError {
fn custom<T>(msg: T) -> Self
where
T: Display,
{
ConfigurationError::from(ErrorCode::DeserializationError(msg.to_string()))
}
}