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

Commit 3562a13

Browse files
committed
Split Error display into message_no_mark + mark
1 parent 790becc commit 3562a13

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

‎src/error.rs‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,33 +196,40 @@ impl ErrorImpl {
196196
}
197197
}
198198

199-
fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {
199+
fn mark(&self) -> Option<libyaml::Mark> {
200200
match self {
201-
ErrorImpl::Message(msg, None) => Display::fmt(msg, f),
202-
ErrorImpl::Message(msg, Some(Pos { mark, path })) => {
203-
if path == "." {
204-
write!(f, "{} at {}", msg, mark)
205-
} else {
206-
write!(f, "{}: {} at {}", path, msg, mark)
201+
ErrorImpl::Message(_, Some(Pos { mark, path: _ }))
202+
| ErrorImpl::RecursionLimitExceeded(mark)
203+
| ErrorImpl::UnknownAnchor(mark) => Some(*mark),
204+
ErrorImpl::Shared(err) => err.mark(),
205+
_ => None,
206+
}
207+
}
208+
209+
fn message_no_mark(&self, f: &mut fmt::Formatter) -> fmt::Result {
210+
match self {
211+
ErrorImpl::Message(msg, None) => f.write_str(msg),
212+
ErrorImpl::Message(msg, Some(Pos { mark: _, path })) => {
213+
if path != "." {
214+
write!(f, "{}: ", path)?;
207215
}
216+
f.write_str(msg)
208217
}
209-
ErrorImpl::Libyaml(err) => Display::fmt(err, f),
218+
ErrorImpl::Libyaml(_) => unreachable!(),
210219
ErrorImpl::Io(err) => Display::fmt(err, f),
211220
ErrorImpl::FromUtf8(err) => Display::fmt(err, f),
212221
ErrorImpl::EndOfStream => f.write_str("EOF while parsing a value"),
213222
ErrorImpl::MoreThanOneDocument => f.write_str(
214223
"deserializing from YAML containing more than one document is not supported",
215224
),
216-
ErrorImpl::RecursionLimitExceeded(mark) => {
217-
write!(f, "recursion limit exceeded at {}", mark)
218-
}
225+
ErrorImpl::RecursionLimitExceeded(_mark) => f.write_str("recursion limit exceeded"),
219226
ErrorImpl::RepetitionLimitExceeded => f.write_str("repetition limit exceeded"),
220227
ErrorImpl::BytesUnsupported => {
221228
f.write_str("serialization and deserialization of bytes in YAML is not implemented")
222229
}
223-
ErrorImpl::UnknownAnchor(mark) => write!(f, "unknown anchor at {}", mark),
230+
ErrorImpl::UnknownAnchor(_mark) => f.write_str("unknown anchor"),
224231
ErrorImpl::SerializeNestedEnum => {
225-
write!(f, "serializing nested enums in YAML is not supported yet")
232+
f.write_str("serializing nested enums in YAML is not supported yet")
226233
}
227234
ErrorImpl::ScalarInMerge => {
228235
f.write_str("expected a mapping or list of mappings for merging, but found scalar")
@@ -234,7 +241,21 @@ impl ErrorImpl {
234241
ErrorImpl::SequenceInMergeElement => {
235242
f.write_str("expected a mapping for merging, but found sequence")
236243
}
244+
ErrorImpl::Shared(_) => unreachable!(),
245+
}
246+
}
247+
248+
fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {
249+
match self {
250+
ErrorImpl::Libyaml(err) => Display::fmt(err, f),
237251
ErrorImpl::Shared(err) => err.display(f),
252+
_ => {
253+
self.message_no_mark(f)?;
254+
if let Some(mark) = self.mark() {
255+
write!(f, " at {}", mark)?;
256+
}
257+
Ok(())
258+
}
238259
}
239260
}
240261

0 commit comments

Comments
 (0)