1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq)]
5pub enum Error {
6 UnexpectedEof,
8 InvalidCharacter { ch: char, pos: usize },
10 InvalidNumber { text: String, pos: usize },
12 InvalidEscape { seq: String, pos: usize },
14 InvalidHeader { text: String, pos: usize },
16 ArrayLengthMismatch { declared: usize, found: usize, pos: usize },
18 FieldCountMismatch { expected: usize, found: usize, pos: usize },
20 InvalidIndentation { expected: usize, found: usize, pos: usize },
22 MissingColon { pos: usize },
24 InvalidUtf8,
26 Message(String),
28}
29
30impl fmt::Display for Error {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Error::UnexpectedEof => write!(f, "unexpected end of input"),
34 Error::InvalidCharacter { ch, pos } => {
35 write!(f, "invalid character '{}' at position {}", ch, pos)
36 }
37 Error::InvalidNumber { text, pos } => {
38 write!(f, "invalid number '{}' at position {}", text, pos)
39 }
40 Error::InvalidEscape { seq, pos } => {
41 write!(f, "invalid escape sequence '{}' at position {}", seq, pos)
42 }
43 Error::InvalidHeader { text, pos } => {
44 write!(f, "invalid array header '{}' at position {}", text, pos)
45 }
46 Error::ArrayLengthMismatch { declared, found, pos } => {
47 write!(
48 f,
49 "array length mismatch: declared {} but found {} elements at position {}",
50 declared, found, pos
51 )
52 }
53 Error::FieldCountMismatch { expected, found, pos } => {
54 write!(
55 f,
56 "field count mismatch: expected {} but found {} at position {}",
57 expected, found, pos
58 )
59 }
60 Error::InvalidIndentation { expected, found, pos } => {
61 write!(
62 f,
63 "invalid indentation: expected depth {} but found {} at position {}",
64 expected, found, pos
65 )
66 }
67 Error::MissingColon { pos } => {
68 write!(f, "missing colon after key at position {}", pos)
69 }
70 Error::InvalidUtf8 => write!(f, "invalid UTF-8 in input"),
71 Error::Message(msg) => write!(f, "{}", msg),
72 }
73 }
74}
75
76impl From<std::io::Error> for Error {
77 fn from(err: std::io::Error) -> Self {
78 Error::Message(err.to_string())
79 }
80}
81
82impl From<std::fmt::Error> for Error {
83 fn from(err: std::fmt::Error) -> Self {
84 Error::Message(err.to_string())
85 }
86}
87
88impl std::error::Error for Error {}
89
90pub type Result<T> = std::result::Result<T, Error>;