Skip to main content

json_steroids/
error.rs

1//! Error types for JSON parsing and serialization
2
3use std::fmt;
4
5/// Result type alias for JSON operations
6pub type Result<T> = std::result::Result<T, JsonError>;
7
8/// Errors that can occur during JSON parsing or serialization
9#[derive(Debug, Clone, PartialEq)]
10pub enum JsonError {
11    /// Unexpected end of input
12    UnexpectedEnd,
13    /// Unexpected character encountered
14    UnexpectedChar(char, usize),
15    /// Expected a specific character
16    ExpectedChar(char, usize),
17    /// Expected a specific token
18    ExpectedToken(&'static str, usize),
19    /// Invalid number format
20    InvalidNumber(usize),
21    /// Invalid escape sequence
22    InvalidEscape(usize),
23    /// Invalid Unicode escape
24    InvalidUnicode(usize),
25    /// Invalid UTF-8 encoding
26    InvalidUtf8,
27    /// Missing required field during deserialization
28    MissingField(String),
29    /// Unknown enum variant
30    UnknownVariant(String),
31    /// Type mismatch during deserialization
32    TypeMismatch {
33        expected: &'static str,
34        found: &'static str,
35        position: usize,
36    },
37    /// Nesting too deep
38    NestingTooDeep(usize),
39    /// Custom error message
40    Custom(String),
41}
42
43impl fmt::Display for JsonError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            JsonError::UnexpectedEnd => write!(f, "unexpected end of JSON input"),
47            JsonError::UnexpectedChar(c, pos) => {
48                write!(f, "unexpected character '{}' at position {}", c, pos)
49            }
50            JsonError::ExpectedChar(c, pos) => write!(f, "expected '{}' at position {}", c, pos),
51            JsonError::ExpectedToken(token, pos) => {
52                write!(f, "expected {} at position {}", token, pos)
53            }
54            JsonError::InvalidNumber(pos) => write!(f, "invalid number at position {}", pos),
55            JsonError::InvalidEscape(pos) => {
56                write!(f, "invalid escape sequence at position {}", pos)
57            }
58            JsonError::InvalidUnicode(pos) => {
59                write!(f, "invalid unicode escape at position {}", pos)
60            }
61            JsonError::InvalidUtf8 => write!(f, "invalid UTF-8 encoding"),
62            JsonError::MissingField(field) => write!(f, "missing required field: {}", field),
63            JsonError::UnknownVariant(variant) => write!(f, "unknown variant: {}", variant),
64            JsonError::TypeMismatch {
65                expected,
66                found,
67                position,
68            } => {
69                write!(
70                    f,
71                    "type mismatch at position {}: expected {}, found {}",
72                    position, expected, found
73                )
74            }
75            JsonError::NestingTooDeep(depth) => write!(f, "nesting too deep: {} levels", depth),
76            JsonError::Custom(msg) => write!(f, "{}", msg),
77        }
78    }
79}
80
81impl std::error::Error for JsonError {}