Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit 7ea4775

Browse files
committed
Factor out error trait impls to work without outer Error wrapper
1 parent b16a401 commit 7ea4775

1 file changed

Lines changed: 54 additions & 44 deletions

File tree

‎src/error.rs‎

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,51 @@ pub(crate) fn fix_marker(mut error: Error, marker: Marker, path: Path) -> Error
143143

144144
impl error::Error for Error {
145145
// TODO: deprecated, remove in next major version.
146+
fn description(&self) -> &str {
147+
self.0.description()
148+
}
149+
150+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
151+
self.0.source()
152+
}
153+
}
154+
155+
impl Display for Error {
156+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
157+
self.0.display(f)
158+
}
159+
}
160+
161+
// Remove two layers of verbosity from the debug representation. Humans often
162+
// end up seeing this representation because it is what unwrap() shows.
163+
impl Debug for Error {
164+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
165+
self.0.debug(f)
166+
}
167+
}
168+
169+
impl ser::Error for Error {
170+
fn custom<T: Display>(msg: T) -> Self {
171+
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
172+
}
173+
}
174+
175+
impl de::Error for Error {
176+
fn custom<T: Display>(msg: T) -> Self {
177+
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
178+
}
179+
}
180+
181+
impl ErrorImpl {
146182
#[allow(deprecated)]
147183
fn description(&self) -> &str {
148-
match self.0.as_ref() {
184+
match self {
149185
ErrorImpl::Message(msg, _) => msg,
150186
ErrorImpl::Emit(_) => "emit error",
151187
ErrorImpl::Scan(_) => "scan error",
152-
ErrorImpl::Io(err) => err.description(),
153-
ErrorImpl::Utf8(err) => err.description(),
154-
ErrorImpl::FromUtf8(err) => err.description(),
188+
ErrorImpl::Io(err) => error::Error::description(err),
189+
ErrorImpl::Utf8(err) => error::Error::description(err),
190+
ErrorImpl::FromUtf8(err) => error::Error::description(err),
155191
ErrorImpl::EndOfStream => "EOF while parsing a value",
156192
ErrorImpl::MoreThanOneDocument => {
157193
"deserializing from YAML containing more than one document is not supported"
@@ -161,19 +197,17 @@ impl error::Error for Error {
161197
}
162198

163199
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
164-
match self.0.as_ref() {
200+
match self {
165201
ErrorImpl::Scan(err) => Some(err),
166202
ErrorImpl::Io(err) => Some(err),
167203
ErrorImpl::Utf8(err) => Some(err),
168204
ErrorImpl::FromUtf8(err) => Some(err),
169205
_ => None,
170206
}
171207
}
172-
}
173208

174-
impl Display for Error {
175-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
176-
match self.0.as_ref() {
209+
fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {
210+
match self {
177211
ErrorImpl::Message(msg, None) => Display::fmt(msg, f),
178212
ErrorImpl::Message(msg, Some(Pos { marker, path })) => {
179213
if path == "." {
@@ -195,42 +229,18 @@ impl Display for Error {
195229
ErrorImpl::RecursionLimitExceeded => f.write_str("recursion limit exceeded"),
196230
}
197231
}
198-
}
199232

200-
// Remove two layers of verbosity from the debug representation. Humans often
201-
// end up seeing this representation because it is what unwrap() shows.
202-
impl Debug for Error {
203-
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
204-
match self.0.as_ref() {
205-
ErrorImpl::Message(msg, pos) => formatter
206-
.debug_tuple("Message")
207-
.field(msg)
208-
.field(pos)
209-
.finish(),
210-
ErrorImpl::Emit(emit) => formatter.debug_tuple("Emit").field(emit).finish(),
211-
ErrorImpl::Scan(scan) => formatter.debug_tuple("Scan").field(scan).finish(),
212-
ErrorImpl::Io(io) => formatter.debug_tuple("Io").field(io).finish(),
213-
ErrorImpl::Utf8(utf8) => formatter.debug_tuple("Utf8").field(utf8).finish(),
214-
ErrorImpl::FromUtf8(from_utf8) => {
215-
formatter.debug_tuple("FromUtf8").field(from_utf8).finish()
216-
}
217-
ErrorImpl::EndOfStream => formatter.debug_tuple("EndOfStream").finish(),
218-
ErrorImpl::MoreThanOneDocument => formatter.debug_tuple("MoreThanOneDocument").finish(),
219-
ErrorImpl::RecursionLimitExceeded => {
220-
formatter.debug_tuple("RecursionLimitExceeded").finish()
221-
}
233+
fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {
234+
match self {
235+
ErrorImpl::Message(msg, pos) => f.debug_tuple("Message").field(msg).field(pos).finish(),
236+
ErrorImpl::Emit(emit) => f.debug_tuple("Emit").field(emit).finish(),
237+
ErrorImpl::Scan(scan) => f.debug_tuple("Scan").field(scan).finish(),
238+
ErrorImpl::Io(io) => f.debug_tuple("Io").field(io).finish(),
239+
ErrorImpl::Utf8(utf8) => f.debug_tuple("Utf8").field(utf8).finish(),
240+
ErrorImpl::FromUtf8(from_utf8) => f.debug_tuple("FromUtf8").field(from_utf8).finish(),
241+
ErrorImpl::EndOfStream => f.debug_tuple("EndOfStream").finish(),
242+
ErrorImpl::MoreThanOneDocument => f.debug_tuple("MoreThanOneDocument").finish(),
243+
ErrorImpl::RecursionLimitExceeded => f.debug_tuple("RecursionLimitExceeded").finish(),
222244
}
223245
}
224246
}
225-
226-
impl ser::Error for Error {
227-
fn custom<T: Display>(msg: T) -> Self {
228-
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
229-
}
230-
}
231-
232-
impl de::Error for Error {
233-
fn custom<T: Display>(msg: T) -> Self {
234-
Error(Box::new(ErrorImpl::Message(msg.to_string(), None)))
235-
}
236-
}

0 commit comments

Comments
 (0)