rson_core/
error.rs

1//! Error types for RSON parsing and processing.
2
3use core::fmt;
4
5/// Result type alias for RSON operations.
6pub type RsonResult<T> = Result<T, RsonError>;
7
8/// Errors that can occur during RSON parsing and processing.
9#[derive(Debug, Clone, PartialEq)]
10pub enum RsonError {
11    /// Syntax error in RSON input
12    SyntaxError {
13        message: String,
14        line: usize,
15        column: usize,
16    },
17    
18    /// Unexpected end of input
19    UnexpectedEof,
20    
21    /// Invalid character in input
22    InvalidCharacter {
23        found: char,
24        expected: String,
25        position: usize,
26    },
27    
28    /// Invalid number format
29    InvalidNumber(String),
30    
31    /// Invalid string escape sequence
32    InvalidEscape(String),
33    
34    /// Invalid Unicode codepoint
35    InvalidUnicode(String),
36    
37    /// Invalid identifier
38    InvalidIdentifier(String),
39    
40    /// Type mismatch during conversion
41    TypeMismatch {
42        expected: String,
43        found: String,
44    },
45    
46    /// Index out of bounds
47    IndexOutOfBounds(usize),
48    
49    /// Key not found
50    KeyNotFound(String),
51    
52    /// Custom error message
53    Custom(String),
54}
55
56impl RsonError {
57    /// Create a new syntax error.
58    pub fn syntax_error<S: Into<String>>(message: S, line: usize, column: usize) -> Self {
59        RsonError::SyntaxError {
60            message: message.into(),
61            line,
62            column,
63        }
64    }
65    
66    /// Create a new invalid character error.
67    pub fn invalid_character<S: Into<String>>(found: char, expected: S, position: usize) -> Self {
68        RsonError::InvalidCharacter {
69            found,
70            expected: expected.into(),
71            position,
72        }
73    }
74    
75    /// Create a new type mismatch error.
76    pub fn type_mismatch<S: Into<String>>(expected: S, found: S) -> Self {
77        RsonError::TypeMismatch {
78            expected: expected.into(),
79            found: found.into(),
80        }
81    }
82    
83    /// Create a new custom error.
84    pub fn custom<S: Into<String>>(message: S) -> Self {
85        RsonError::Custom(message.into())
86    }
87}
88
89impl fmt::Display for RsonError {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        match self {
92            RsonError::SyntaxError { message, line, column } => {
93                write!(f, "Syntax error at line {}, column {}: {}", line, column, message)
94            }
95            RsonError::UnexpectedEof => {
96                write!(f, "Unexpected end of input")
97            }
98            RsonError::InvalidCharacter { found, expected, position } => {
99                write!(f, "Invalid character '{}' at position {}, expected {}", found, position, expected)
100            }
101            RsonError::InvalidNumber(msg) => {
102                write!(f, "Invalid number: {}", msg)
103            }
104            RsonError::InvalidEscape(msg) => {
105                write!(f, "Invalid escape sequence: {}", msg)
106            }
107            RsonError::InvalidUnicode(msg) => {
108                write!(f, "Invalid Unicode: {}", msg)
109            }
110            RsonError::InvalidIdentifier(msg) => {
111                write!(f, "Invalid identifier: {}", msg)
112            }
113            RsonError::TypeMismatch { expected, found } => {
114                write!(f, "Type mismatch: expected {}, found {}", expected, found)
115            }
116            RsonError::IndexOutOfBounds(index) => {
117                write!(f, "Index {} out of bounds", index)
118            }
119            RsonError::KeyNotFound(key) => {
120                write!(f, "Key '{}' not found", key)
121            }
122            RsonError::Custom(msg) => {
123                write!(f, "{}", msg)
124            }
125        }
126    }
127}
128
129#[cfg(feature = "std")]
130impl std::error::Error for RsonError {}
131
132// Convert from nom errors
133impl From<nom::Err<nom::error::Error<&str>>> for RsonError {
134    fn from(err: nom::Err<nom::error::Error<&str>>) -> Self {
135        match err {
136            nom::Err::Incomplete(_) => RsonError::UnexpectedEof,
137            nom::Err::Error(e) | nom::Err::Failure(e) => {
138                RsonError::Custom(format!("Parse error: {:?}", e))
139            }
140        }
141    }
142}
143
144impl From<nom::Err<nom::error::VerboseError<&str>>> for RsonError {
145    fn from(err: nom::Err<nom::error::VerboseError<&str>>) -> Self {
146        match err {
147            nom::Err::Incomplete(_) => RsonError::UnexpectedEof,
148            nom::Err::Error(e) | nom::Err::Failure(e) => {
149                let msg = nom::error::convert_error("", e);
150                RsonError::Custom(format!("Parse error: {}", msg))
151            }
152        }
153    }
154}