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
use std::fmt;
use crate::Path;
/// Why a document could not be read or an edit could not be made. A failed
/// edit leaves the document as it was.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
/// The source is not valid YAML.
Parse {
/// 1-based line of the problem.
line: usize,
/// 1-based column of the problem.
column: usize,
/// The parser's description.
message: String,
},
/// A JSON pointer that RFC 6901 does not allow.
Pointer {
/// The pointer as given.
pointer: String,
},
/// No node sits at this path.
NoNode {
/// The path that was asked for.
path: Path,
},
/// The node at this path is not the kind the edit needs.
WrongKind {
/// The path of the node.
path: Path,
/// What the edit needed, such as "a block sequence".
expected: &'static str,
},
/// A reorder named this child more than once.
Repeated {
/// The path of the child.
path: Path,
},
/// The mapping already has this key.
KeyExists {
/// The path of the key that exists.
path: Path,
},
/// A tag kept in front of the value would retype the new value, such as
/// `!!int` in front of `abc`, or a tag whose values this version cannot
/// check, such as `!!timestamp`.
TagMismatch {
/// The path of the node.
path: Path,
/// The tag as written, such as `!!timestamp`.
tag: String,
},
/// The edit is valid YAML but outside what this version can write in
/// place.
Unsupported {
/// The path of the node.
path: Path,
/// What could not be done.
what: &'static str,
},
/// The edit would have left text that does not parse.
Invalid {
/// The parser's description of the edited text.
message: String,
},
/// The value could not be serialized.
#[cfg(feature = "serde")]
Serialize {
/// The serializer's description.
message: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse {
line,
column,
message,
} => write!(formatter, "line {line}, column {column}: {message}"),
Self::Pointer { pointer } => write!(formatter, "{pointer:?} is not a JSON pointer"),
Self::NoNode { path } => write!(formatter, "no node at {path}"),
Self::WrongKind { path, expected } => {
write!(formatter, "the node at {path} is not {expected}")
}
Self::KeyExists { path } => write!(formatter, "{path} already exists"),
Self::Repeated { path } => write!(formatter, "{path} is named twice"),
Self::TagMismatch { path, tag } => {
write!(
formatter,
"at {path}: the tag {tag} does not fit the new value"
)
}
Self::Unsupported { path, what } => write!(formatter, "at {path}: cannot {what} yet"),
Self::Invalid { message } => write!(formatter, "the edit would not parse: {message}"),
#[cfg(feature = "serde")]
Self::Serialize { message } => {
write!(formatter, "cannot serialize the value: {message}")
}
}
}
}
impl std::error::Error for Error {}