1use core::fmt;
4
5pub type RsonResult<T> = Result<T, RsonError>;
7
8#[derive(Debug, Clone, PartialEq)]
10pub enum RsonError {
11 SyntaxError {
13 message: String,
14 line: usize,
15 column: usize,
16 },
17
18 UnexpectedEof,
20
21 InvalidCharacter {
23 found: char,
24 expected: String,
25 position: usize,
26 },
27
28 InvalidNumber(String),
30
31 InvalidEscape(String),
33
34 InvalidUnicode(String),
36
37 InvalidIdentifier(String),
39
40 TypeMismatch {
42 expected: String,
43 found: String,
44 },
45
46 IndexOutOfBounds(usize),
48
49 KeyNotFound(String),
51
52 Custom(String),
54}
55
56impl RsonError {
57 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 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 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 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
132impl 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}