1use std::fmt;
4
5use crate::json::quote;
6
7#[derive(Clone, Default, PartialEq, Eq, Debug)]
16#[must_use]
17pub struct Place(Vec<Step>);
18
19#[derive(Clone, PartialEq, Eq, Debug)]
21pub enum Step {
22 Field(String),
24 Element(usize),
27 Key(String),
29 Constructor(String),
31}
32
33impl Place {
34 pub(crate) fn field(&mut self, name: &str) {
35 self.0.push(Step::Field(name.to_owned()));
36 }
37
38 pub(crate) fn constructor(&mut self, name: &str) {
39 self.0.push(Step::Constructor(name.to_owned()));
40 }
41
42 pub(crate) fn index(&mut self, index: usize) {
43 self.0.push(Step::Element(index));
44 }
45
46 pub(crate) fn key(&mut self, key: &str) {
47 self.0.push(Step::Key(key.to_owned()));
48 }
49
50 pub(crate) fn push(&mut self, step: Step) {
51 self.0.push(step);
52 }
53
54 pub(crate) fn pop(&mut self) {
55 self.0.pop();
56 }
57
58 pub fn within(mut self, outer: impl IntoIterator<Item = Step>) -> Place {
60 let mut steps: Vec<Step> = outer.into_iter().collect();
61 steps.append(&mut self.0);
62 Place(steps)
63 }
64
65 pub fn steps(&self) -> &[Step] {
67 &self.0
68 }
69
70 pub fn is_empty(&self) -> bool {
72 self.0.is_empty()
73 }
74}
75
76impl fmt::Display for Place {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 for (at, step) in self.0.iter().enumerate() {
79 match step {
80 Step::Field(name) | Step::Constructor(name) if at == 0 => f.write_str(name)?,
81 Step::Field(name) | Step::Constructor(name) => write!(f, ".{name}")?,
82 Step::Element(index) => write!(f, "[{index}]")?,
83 Step::Key(key) => write!(f, "[{}]", quote(key))?,
84 }
85 }
86 Ok(())
87 }
88}
89
90impl PartialEq<&str> for Place {
92 fn eq(&self, text: &&str) -> bool {
93 struct Rest<'a>(&'a str);
96 impl fmt::Write for Rest<'_> {
97 fn write_str(&mut self, piece: &str) -> fmt::Result {
98 self.0 = self.0.strip_prefix(piece).ok_or(fmt::Error)?;
99 Ok(())
100 }
101 }
102 let mut rest = Rest(text);
103 fmt::write(&mut rest, format_args!("{self}")).is_ok() && rest.0.is_empty()
104 }
105}
106
107#[derive(Clone, PartialEq, Eq, Debug)]
112pub struct DecodeError {
113 pub path: Place,
116 pub problem: String,
118 pub kind: DecodeErrorKind,
120}
121
122#[derive(Clone, Copy, PartialEq, Eq, Debug)]
124pub enum DecodeErrorKind {
125 Syntax {
127 at: usize,
129 },
130 Missing,
132 Unknown,
134 Duplicate,
137 Mismatch,
139 TooLarge,
141}
142
143impl DecodeError {
144 pub(crate) fn of(path: &Place, kind: DecodeErrorKind, problem: String) -> Self {
145 DecodeError {
146 path: path.clone(),
147 problem,
148 kind,
149 }
150 }
151}
152
153impl DecodeError {
154 pub fn for_subject(&self, subject: &str) -> String {
158 if self.path.is_empty() {
159 format!("{subject} {}", self.problem)
160 } else {
161 format!("`{}` in {subject} {}", self.path, self.problem)
162 }
163 }
164}
165
166impl fmt::Display for DecodeError {
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168 if self.path.is_empty() {
169 write!(f, "the value {}", self.problem)
170 } else {
171 write!(f, "`{}` {}", self.path, self.problem)
172 }
173 }
174}
175
176impl std::error::Error for DecodeError {}
177
178#[derive(Clone, PartialEq, Eq, Debug)]
183pub struct EncodeError {
184 pub path: Place,
186 pub problem: String,
188 pub kind: EncodeErrorKind,
190}
191
192#[derive(Clone, Copy, PartialEq, Eq, Debug)]
194pub enum EncodeErrorKind {
195 Mismatch,
197 NoEncoding,
199 TooLarge,
201 Invalid,
203}
204
205impl EncodeError {
206 pub(crate) fn new(path: &Place, kind: EncodeErrorKind, problem: impl Into<String>) -> Self {
207 EncodeError {
208 path: path.clone(),
209 problem: problem.into(),
210 kind,
211 }
212 }
213}
214
215impl From<DecodeError> for EncodeError {
217 fn from(error: DecodeError) -> Self {
218 EncodeError {
219 path: error.path,
220 problem: error.problem,
221 kind: EncodeErrorKind::Invalid,
222 }
223 }
224}
225
226impl fmt::Display for EncodeError {
227 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228 if self.path.is_empty() {
229 write!(f, "the value {}", self.problem)
230 } else {
231 write!(f, "`{}` {}", self.path, self.problem)
232 }
233 }
234}
235
236impl std::error::Error for EncodeError {}