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
use crate::path::Path;
use serde::{de, ser};
use std::error;
use std::fmt::{self, Debug, Display};
use std::io;
use std::result;
use std::str;
use std::string;
use yaml_rust::emitter;
use yaml_rust::scanner::{self, Marker, ScanError};
pub struct Error(Box<ErrorImpl>);
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum ErrorImpl {
Message(String, Option<Pos>),
Emit(emitter::EmitError),
Scan(scanner::ScanError),
Io(io::Error),
Utf8(str::Utf8Error),
FromUtf8(string::FromUtf8Error),
EndOfStream,
MoreThanOneDocument,
RecursionLimitExceeded,
}
#[derive(Debug)]
pub struct Pos {
marker: Marker,
path: String,
}
#[derive(Debug)]
pub struct Location {
index: usize,
line: usize,
column: usize,
}
impl Location {
pub fn index(&self) -> usize {
self.index
}
pub fn line(&self) -> usize {
self.line
}
pub fn column(&self) -> usize {
self.column
}
#[doc(hidden)]
fn from_marker(marker: &Marker) -> Self {
Location {
column: marker.col() + 1,
index: marker.index(),
line: marker.line(),
}
}
}
impl Error {
pub fn location(&self) -> Option<Location> {
match self.0.as_ref() {
ErrorImpl::Message(_, Some(pos)) => Some(Location::from_marker(&pos.marker)),
ErrorImpl::Scan(scan) => Some(Location::from_marker(scan.marker())),
_ => None,
}
}
}
pub(crate) fn end_of_stream() -> Error {
Error(Box::new(ErrorImpl::EndOfStream))
}
pub(crate) fn more_than_one_document() -> Error {
Error(Box::new(ErrorImpl::MoreThanOneDocument))
}
pub(crate) fn io(err: io::Error) -> Error {
Error(Box::new(ErrorImpl::Io(err)))
}
pub(crate) fn emitter(err: emitter::EmitError) -> Error {
Error(Box::new(ErrorImpl::Emit(err)))
}
pub(crate) fn scanner(err: scanner::ScanError) -> Error {
Error(Box::new(ErrorImpl::Scan(err)))
}
pub(crate) fn str_utf8(err: str::Utf8Error) -> Error {
Error(Box::new(ErrorImpl::Utf8(err)))
}
pub(crate) fn string_utf8(err: string::FromUtf8Error) -> Error {
Error(Box::new(ErrorImpl::FromUtf8(err)))
}
pub(crate) fn recursion_limit_exceeded() -> Error {
Error(Box::new(ErrorImpl::RecursionLimitExceeded))
}
pub(crate) fn fix_marker(mut error: Error, marker: Marker, path: Path) -> Error {
if let ErrorImpl::Message(_, none @ None) = error.0.as_mut() {
*none = Some(Pos {
marker,
path: path.to_string(),
});
}
error
}
impl error::Error for Error {
#[allow(deprecated)]
fn description(&self) -> &str {
match self.0.as_ref() {
ErrorImpl::Message(msg, _) => msg,
ErrorImpl::Emit(_) => "emit error",
ErrorImpl::Scan(_) => "scan error",
ErrorImpl::Io(err) => err.description(),
ErrorImpl::Utf8(err) => err.description(),
ErrorImpl::FromUtf8(err) => err.description(),
ErrorImpl::EndOfStream => "EOF while parsing a value",
ErrorImpl::MoreThanOneDocument => {
"deserializing from YAML containing more than one document is not supported"
}
ErrorImpl::RecursionLimitExceeded => "recursion limit exceeded",
}
}
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self.0.as_ref() {
ErrorImpl::Scan(err) => Some(err),
ErrorImpl::Io(err) => Some(err),
ErrorImpl::Utf8(err) => Some(err),
ErrorImpl::FromUtf8(err) => Some(err),
_ => None,
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0.as_ref() {
ErrorImpl::Message(msg, None) => Display::fmt(msg, f),
ErrorImpl::Message(msg, Some(Pos { marker, path })) => {
if path == "." {
write!(f, "{}", ScanError::new(*marker, msg))
} else {
write!(f, "{}: {}", path, ScanError::new(*marker, msg))
}
}
ErrorImpl::Emit(emitter::EmitError::FmtError(_)) => f.write_str("yaml-rust fmt error"),
ErrorImpl::Emit(emitter::EmitError::BadHashmapKey) => f.write_str("bad hash map key"),
ErrorImpl::Scan(err) => Display::fmt(err, f),
ErrorImpl::Io(err) => Display::fmt(err, f),
ErrorImpl::Utf8(err) => Display::fmt(err, f),
ErrorImpl::FromUtf8(err) => Display::fmt(err, f),
ErrorImpl::EndOfStream => f.write_str("EOF while parsing a value"),
ErrorImpl::MoreThanOneDocument => f.write_str(
"deserializing from YAML containing more than one document is not supported",
),
ErrorImpl::RecursionLimitExceeded => f.write_str("recursion limit exceeded"),
}
}
}
impl Debug for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.0.as_ref() {
ErrorImpl::Message(msg, pos) => formatter
.debug_tuple("Message")
.field(msg)
.field(pos)
.finish(),
ErrorImpl::Emit(emit) => formatter.debug_tuple("Emit").field(emit).finish(),
ErrorImpl::Scan(scan) => formatter.debug_tuple("Scan").field(scan).finish(),
ErrorImpl::Io(io) => formatter.debug_tuple("Io").field(io).finish(),
ErrorImpl::Utf8(utf8) => formatter.debug_tuple("Utf8").field(utf8).finish(),
ErrorImpl::FromUtf8(from_utf8) => {
formatter.debug_tuple("FromUtf8").field(from_utf8).finish()
}
ErrorImpl::EndOfStream => formatter.debug_tuple("EndOfStream").finish(),
ErrorImpl::MoreThanOneDocument => formatter.debug_tuple("MoreThanOneDocument").finish(),
ErrorImpl::RecursionLimitExceeded => {
formatter.debug_tuple("RecursionLimitExceeded").finish()
}
}
}
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
}
}