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
use crate::{
emitter::EmitError,
parser::Event,
scanner::{Marker, ScanError},
};
use std::{fmt, str};
/// An error type that contains all possible error variants that may occur
/// when serializing or deserializing StrictYaml data.
#[derive(Debug)]
pub enum Error {
/// Wraps errors that originate from serde.
Message(String),
/// Enhances errors from serde with a marker to the location where
/// the error occured.
MarkedMessage { msg: String, mark: Marker },
/// Raised when the deserializer calls an unsupported `deserialize_*`
/// method based on the input data.
UnsupportedType(&'static str),
/// Raised when the deserializer expected the start of a YAML stream,
/// but encounters something different.
UnexpectedStreamStart {
mark: Marker,
expected: &'static str,
},
/// Raised when the deserializer expected the end of a YAML stream,
/// but encounters something different.
UnexpectedStreamEnd {
mark: Marker,
expected: &'static str,
},
/// Raised when the deserializer expected the start of a YAML document,
/// but encounters something different.
UnexpectedDocumentStart {
mark: Marker,
expected: &'static str,
},
/// Raised when the deserializer expected the end of a YAML document,
/// but encounters something different.
UnexpectedDocumentEnd {
mark: Marker,
expected: &'static str,
},
/// Raised when the deserializer expected a scalar value, but encounters
/// something different.
UnexpectedScalar {
mark: Marker,
expected: &'static str,
value: String,
},
/// Raised when the deserializer expected the start of a YAML sequence,
/// but encounters something different.
UnexpectedSequenceStart {
mark: Marker,
expected: &'static str,
},
/// Raised when the deserializer expected the end of a YAML sequence,
/// but encounters something different.
UnexpectedSequenceEnd {
mark: Marker,
expected: &'static str,
},
/// Raised when the deserializer expected the start of a YAML map,
/// but encounters something different.
UnexpectedMappingStart {
mark: Marker,
expected: &'static str,
},
/// Raised when the deserializer expected the end of a YAML map,
/// but encounters something different.
UnexpectedMappingEnd {
mark: Marker,
expected: &'static str,
},
}
impl Error {
/// Construct the appropiate error variant based on the event
/// emitted by the parser, wich did not match the event expected
/// by the deserializer.
pub(crate) fn from_event(ev: Event, mark: Marker, expected: &'static str) -> Self {
match ev {
Event::StreamStart => Self::UnexpectedStreamStart { mark, expected },
Event::StreamEnd => Self::UnexpectedStreamEnd { mark, expected },
Event::DocumentStart => Self::UnexpectedDocumentStart { mark, expected },
Event::DocumentEnd => Self::UnexpectedDocumentEnd { mark, expected },
Event::Scalar(value, _, _) => Self::UnexpectedScalar {
mark,
expected,
value,
},
Event::SequenceStart(_) => Self::UnexpectedSequenceStart { mark, expected },
Event::SequenceEnd => Self::UnexpectedSequenceEnd { mark, expected },
Event::MappingStart(_) => Self::UnexpectedMappingStart { mark, expected },
Event::MappingEnd => Self::UnexpectedMappingEnd { mark, expected },
_ => unreachable!(),
}
}
}
impl serde::de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl serde::ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl From<ScanError> for Error {
fn from(error: ScanError) -> Self {
Error::Message(error.to_string())
}
}
impl From<EmitError> for Error {
fn from(error: EmitError) -> Self {
Error::Message(error.to_string())
}
}
impl From<fmt::Error> for Error {
fn from(error: fmt::Error) -> Self {
Error::Message(error.to_string())
}
}
impl From<str::Utf8Error> for Error {
fn from(error: str::Utf8Error) -> Self {
Error::Message(error.to_string())
}
}
impl std::ops::Add<Marker> for Error {
type Output = Self;
/// The `add` operator is overloaded to enhance a serde error
/// with a location provided by the parser.
fn add(self, mark: Marker) -> Self {
match self {
Self::Message(msg) => Self::MarkedMessage { msg, mark },
_ => self,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Message(msg) => formatter.write_str(msg),
Error::MarkedMessage { msg, mark } => {
write!(formatter, "{} at line {}", msg, mark.line())
}
Error::UnsupportedType(t) => {
write!(formatter, "(de)serialization of {} is not supported", t)
}
Error::UnexpectedStreamStart { mark, expected } => {
write!(
formatter,
"expected {}, but found the start of the stream at line {}",
expected,
mark.line()
)
}
Error::UnexpectedStreamEnd { mark, expected } => {
write!(
formatter,
"expected {}, but found the end of the stream at line {}",
expected,
mark.line()
)
}
Error::UnexpectedDocumentStart { mark, expected } => {
write!(
formatter,
"expected {}, but found the start of a document at line {}",
expected,
mark.line()
)
}
Error::UnexpectedDocumentEnd { mark, expected } => {
write!(
formatter,
"expected {}, but found the end of a document at line {}",
expected,
mark.line()
)
}
Error::UnexpectedScalar {
mark,
expected,
value,
} => {
write!(
formatter,
"expected {}, but found scalar \"{}\" at line {}",
expected,
value,
mark.line()
)
}
Error::UnexpectedSequenceStart { mark, expected } => {
write!(
formatter,
"expected {}, but found the start of a sequence at line {}",
expected,
mark.line()
)
}
Error::UnexpectedSequenceEnd { mark, expected } => {
write!(
formatter,
"expected {}, but found the end of a sequence at line {}",
expected,
mark.line()
)
}
Error::UnexpectedMappingStart { mark, expected } => {
write!(
formatter,
"expected {}, but found the start of a mapping at line {}",
expected,
mark.line()
)
}
Error::UnexpectedMappingEnd { mark, expected } => {
write!(
formatter,
"expected {}, but found the end of a mapping at line {}",
expected,
mark.line()
)
}
}
}
}
impl std::error::Error for Error {}