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
use crate::Rule;
use std::fmt::{self, Display};
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Location {
pub line: usize,
pub column: usize,
}
#[derive(Clone, Debug, PartialEq)]
pub enum JsonError {
Syntax(String, Option<Location>),
NumberFormat(Option<Location>),
NumberRange(Option<Location>),
Unicode(Option<Location>),
}
impl From<pest::error::Error<Rule>> for JsonError {
fn from(err: pest::error::Error<Rule>) -> Self {
let (line, column) = match err.line_col {
pest::error::LineColLocation::Pos((l, c)) => (l, c),
pest::error::LineColLocation::Span((l, c), (_, _)) => (l, c),
};
JsonError::Syntax(err.to_string(), Some(Location { line, column }))
}
}
impl Display for JsonError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
JsonError::Syntax(ref msg, _) => write!(formatter, "{}", msg),
JsonError::NumberFormat(_) => write!(formatter, "bad number format"),
JsonError::NumberRange(_) => write!(formatter, "bad number range"),
JsonError::Unicode(_) => write!(formatter, "bad Unicode characters"),
}
}
}
impl std::error::Error for JsonError {}