json_fixer/jsonfixer/
jsonfixer_error.rs

1use std::fmt::{self};
2
3use super::json_tokenizer::Position;
4/// Errors that may occur while fixing a malformed JSON.
5#[derive(Debug)]
6pub enum JsonFixerError {
7    Syntax(SyntaxError),
8    Format(JsonFormatError),
9    IO(std::fmt::Error),
10    /// Serde error
11    #[cfg( feature = "serde")]
12    SerdeError(String),
13}
14
15#[derive(Debug)]
16pub enum JsonFormatError {
17    LineTooLong {
18        line: usize,
19        length: usize,
20        max: usize,
21    },
22    InvalidIndentation {
23        line: usize,
24    },
25}
26impl fmt::Display for JsonFormatError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::LineTooLong { line, length, max } => write!(
30                f,
31                "Line {} is too long of length: {} \nExpeced max length: {}",
32                line, length, max
33            ),
34            Self::InvalidIndentation { line } => write!(f, "Invalid Indentation at line: {}", line),
35        }
36    }
37}
38
39impl std::error::Error for JsonFixerError {}
40
41impl fmt::Display for JsonFixerError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Self::Syntax(err) => write!(f, "Syntax error: {}", err),
45            Self::Format(err) => write!(f, "Format error: {}", err),
46            Self::IO(err) => write!(f, "IO error: {}", err),
47            #[cfg(feature = "serde")]
48            Self::SerdeError(err) => write!(f, "Serde error: {}", err),
49        }
50    }
51}
52
53#[derive(Debug)]
54pub enum SyntaxError {
55    /// Unexpected character found in input.
56    UnexpectedCharacter(char, Position),
57    /// Unmatched quotes in a string.
58    UnmatchedQuotes(Position),
59    /// Input ended unexpectedly.
60    UnexpectedEndOfInput(Position),
61    /// Comma is missing between JSON elements.
62    MissingComma(Position),
63    /// Invalid number format encountered.
64    InvalidNumber(String, Position),
65    /// Unexpected token in the input.
66    UnexpectedToken(String, Position),
67}
68
69impl fmt::Display for SyntaxError {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Self::UnexpectedToken(token, pos) => write!(
73                f,
74                "Unexpected Token : '{}' at line {}, column {}",
75                token, pos.line, pos.column
76            ),
77            Self::UnexpectedCharacter(ch, pos) => write!(
78                f,
79                "Unexpected character '{}' at line {}, column {}",
80                ch, pos.line, pos.column
81            ),
82            Self::UnmatchedQuotes(pos) => write!(
83                f,
84                "Unmatched quotes at line {}, column {}",
85                pos.line, pos.column
86            ),
87            Self::UnexpectedEndOfInput(pos) => write!(
88                f,
89                "Unexpected end of input at line {}, column {}",
90                pos.line, pos.column
91            ),
92            Self::MissingComma(pos) => write!(
93                f,
94                "Missing comma at line {}, column {}",
95                pos.line, pos.column
96            ),
97            Self::InvalidNumber(ch, pos) => write!(
98                f,
99                "Invalid number '{}' at line {}, column {}",
100                ch, pos.line, pos.column
101            ),
102        }
103    }
104}