1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use std::fmt;

use serde::{de, ser};
use thiserror::Error;

use crate::de::position::{PosRange, Position};

pub type Result<T> = std::result::Result<T, JsonWithCommentsError>;
#[derive(Error, Debug)]
pub struct JsonWithCommentsError {
    #[from]
    inner: Box<dyn std::error::Error + Send + Sync + 'static>,
}
impl JsonWithCommentsError {
    pub fn new<E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>>(err: E) -> Self {
        Self { inner: err.into() }
    }
    // TODO downcast
    pub fn into_inner(self) -> Box<dyn std::error::Error + Send + Sync + 'static> {
        self.inner
    }
}
impl fmt::Display for JsonWithCommentsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.inner.fmt(f)
    }
}
impl de::Error for JsonWithCommentsError {
    fn custom<T>(msg: T) -> Self
    where
        T: fmt::Display,
    {
        JsonWithCommentsError::new(msg.to_string()) // TODO
    }
}
impl ser::Error for JsonWithCommentsError {
    fn custom<T>(msg: T) -> Self
    where
        T: fmt::Display,
    {
        JsonWithCommentsError::new(msg.to_string()) // TODO
    }
}

impl From<std::io::Error> for JsonWithCommentsError {
    fn from(value: std::io::Error) -> Self {
        JsonWithCommentsError::new(value)
    }
}
impl From<std::string::FromUtf8Error> for JsonWithCommentsError {
    fn from(value: std::string::FromUtf8Error) -> Self {
        JsonWithCommentsError::new(value)
    }
}
impl From<std::str::Utf8Error> for JsonWithCommentsError {
    fn from(value: std::str::Utf8Error) -> Self {
        JsonWithCommentsError::new(value)
    }
}
impl From<std::num::ParseIntError> for JsonWithCommentsError {
    fn from(value: std::num::ParseIntError) -> Self {
        JsonWithCommentsError::new(value)
    }
}
impl From<std::num::ParseFloatError> for JsonWithCommentsError {
    fn from(value: std::num::ParseFloatError) -> Self {
        JsonWithCommentsError::new(value)
    }
}
impl From<std::str::ParseBoolError> for JsonWithCommentsError {
    fn from(value: std::str::ParseBoolError) -> Self {
        JsonWithCommentsError::new(value)
    }
}
impl From<std::char::ParseCharError> for JsonWithCommentsError {
    fn from(value: std::char::ParseCharError) -> Self {
        JsonWithCommentsError::new(value)
    }
}

#[derive(Error, Debug)]
pub enum SyntaxError {
    #[error("{pos:?}: Expected value, but found {found:?}")]
    UnexpectedTokenWhileParsingValue { pos: Position, found: u8 },

    #[error("{pos:?}: Expected string start `\"`, but found {found:?}")]
    UnexpectedTokenWhileStartParsingString { pos: Position, found: u8 },

    #[error("{pos:?}: Expected bytes start, but found {found:?}")]
    UnexpectedTokenWhileStartParsingBytes { pos: Position, found: u8 },

    #[error("{pos:?}: Expected string end `\"`, but found {found:?}")]
    UnexpectedTokenWhileParsingString { pos: Position, found: u8 },

    #[error("{pos:?}: Expected string end `\"`, but found {found:?}")]
    UnexpectedTokenWhileEndParsingString { pos: Position, found: u8 },

    #[error("{pos:?}: Expected escape sequence start `\\`, but found {found:?}")]
    UnexpectedTokenWhileStartParsingEscapeSequence { pos: Position, found: u8 },

    #[error("{pos:?}: Expected number start `-` or 0-9 , but found {found:?}")]
    UnexpectedTokenWhileStartParsingNumber { pos: Position, found: u8 },

    #[error("{pos:?}: Expected bool, but found {found:?}")]
    UnexpectedTokenWhileParsingBool { pos: Position, found: u8 },

    #[error("{pos:?}: Expected null, but found {found:?}")]
    UnexpectedTokenWhileParsingNull { pos: Position, found: u8 },

    #[error("{pos:?}: Expected object start `{{`, but found {found:?}")]
    UnexpectedTokenWhileStartParsingObject { pos: Position, found: u8 },

    #[error("{pos:?}: Expected object end `}}`, but found {found:?}")]
    UnexpectedTokenWhileEndParsingObject { pos: Position, found: u8 },

    #[error("{pos:?}: Expected object key, but found {found:?}")]
    UnexpectedTokenWhileParsingObjectKey { pos: Position, found: u8 },

    #[error("{pos:?}: Expected object value start `:`, but found {found:?}")]
    UnexpectedTokenWhileStartParsingObjectValue { pos: Position, found: u8 },

    #[error("{pos:?}: Expected object value end `,` or `}}`, but found {found:?}")]
    UnexpectedTokenWhileEndParsingObjectValue { pos: Position, found: u8 },

    #[error("{pos:?}: Expected array start `[`, but found {found:?}")]
    UnexpectedTokenWhileStartParsingArray { pos: Position, found: u8 },

    #[error("{pos:?}: Expected array end `]`, but found {found:?}")]
    UnexpectedTokenWhileEndParsingArray { pos: Position, found: u8 },

    #[error("{pos:?}: Expected array value, but found {found:?}")]
    UnexpectedTokenWhileParsingArrayValue { pos: Position, found: u8 },

    #[error("{pos:?}: Expected enum start `{{`, but found {found:?}")]
    UnexpectedTokenWhileStartParsingEnum { pos: Position, found: u8 },

    #[error("{pos:?}: Expected enum end `}}`, but found {found:?}")]
    UnexpectedTokenWhileEndParsingEnum { pos: Position, found: u8 },

    #[error("{pos:?}: Expected enum value start `:`, but found {found:?}")]
    UnexpectedTokenWhileStartParsingEnumValue { pos: Position, found: u8 },

    #[error("{pos:?}: Expected comment start `//` or `/*`, but found {found:?}")]
    UnexpectedTokenWhileStartParsingComment { pos: Position, found: u8 },

    #[error("{pos:?}: Expected comment end `*/`, but found {found:?}")]
    UnexpectedTokenWhileEndParsingComment { pos: Position, found: u8 },

    #[error("Expected value, but got EOF")]
    EofWhileStartParsingValue,

    #[error("Expected string start `\"`, but got EOF")]
    EofWhileStartParsingString,

    #[error("Expected string end `\"`, but got EOF")]
    EofWhileEndParsingString,

    #[error("Expected bytes start, but got EOF")]
    EofWhileStartParsingBytes,

    #[error("Expected escape sequence starts with `\\`, but got EOF")]
    EofWhileParsingEscapeSequence,

    #[error("Expected number start, but got EOF")]
    EofWhileStartParsingNumber,

    #[error("Expected fraction, but got EOF")]
    EofWhileStartParsingFraction,

    #[error("Expected exponent, but got EOF")]
    EofWhileStartParsingExponent,

    #[error("Expected number, but got EOF")]
    EofWhileParsingNumber,

    #[error("Expected bool, but got EOF")]
    EofWhileStartParsingBool,

    #[error("Expected null, but got EOF")]
    EofWhileStartParsingNull,

    #[error("Expected object start `{{`, but got EOF")]
    EofWhileStartParsingObject,

    #[error("Expected object end `}}`, but got EOF")]
    EofWhileEndParsingObject,

    #[error("Expected object key, but got EOF")]
    EofWhileParsingObjectKey,

    #[error("Expected object value, but got EOF")]
    EofWhileParsingObjectValue,

    #[error("Expected array start `[`, but got EOF")]
    EofWhileStartParsingArray,

    #[error("Expected array end `]`, but got EOF")]
    EofWhileEndParsingArray,

    #[error("Expected ident, but got EOF")]
    EofWhileParsingIdent,

    #[error("Expected enum start `{{`, but got EOF")]
    EofWhileStartParsingEnum,

    #[error("Expected enum end `}}`, but got EOF")]
    EofWhileEndParsingEnum,

    #[error("Expected start comment `//` or `/*`, but got EOF")]
    EofWhileStartParsingComment,

    #[error("{pos:?}: Expected ident {expected:?}, but found {found:?}")]
    UnexpectedIdent { pos: PosRange, expected: Vec<u8>, found: Vec<u8> },

    #[error("{pos:?}: Expected EOF, but found trailing {found:?}")]
    ExpectedEof { pos: Position, found: u8 },

    #[error("{pos:?}: control character U+{c:04X} must be escaped in string")]
    ControlCharacterWhileParsingString { pos: Position, c: u8 },

    #[error("{pos:?}: invalid escape sequence \\{found:?}")]
    InvalidEscapeSequence { pos: Position, found: u8 },

    #[error("{pos:?}: invalid \\uXXXX escape, cannot parse {found:?} as hex digit")]
    InvalidUnicodeEscape { pos: Position, found: u8 },

    #[error("{pos:?}: cannot convert {char:08X} to char")]
    CannotConvertChar { pos: Position, char: u32 },

    #[error("{pos:?}: JSON with comments number does not start from `+`")]
    InvalidLeadingPlus { pos: Position },

    #[error("{pos:?}: JSON with comments number is forbidden leading `0`")]
    InvalidLeadingZeros { pos: Position },

    #[error("{pos:?}: expect exponent part, but found {found:?}")]
    MissingExponent { pos: Position, found: u8 },

    #[error("{pos:?}: expect fraction part, but found {found:?}")]
    MissingFraction { pos: Position, found: u8 },

    #[error("comment starts with `/*` must be ends with `*/`, but got EoF")]
    UnterminatedComment,
}
impl From<SyntaxError> for JsonWithCommentsError {
    fn from(err: SyntaxError) -> Self {
        JsonWithCommentsError::new(err)
    }
}

#[derive(Error, Debug)]
pub enum SemanticError {
    #[error("{pos:?}: Expected struct start with `{{` or `[`, but found {found:?}")]
    ExpectStruct { pos: Position, found: u8 },

    #[error("map key of JSON with comments must be string")]
    AnyMapKey,

    #[error("JSON with comments must not be empty")]
    EmptyJsonWithComment,

    #[error("{pos:?}: cannot convert {rep:?} to number")]
    InvalidNumber { pos: Position, rep: String },
}
impl From<SemanticError> for JsonWithCommentsError {
    fn from(err: SemanticError) -> Self {
        JsonWithCommentsError::new(err)
    }
}

#[derive(Error, Debug)]
pub enum ConvertError {
    #[error("Cannot convert float to integer")]
    CannotConvertFloatToInteger,

    #[error("Cannot convert integer to float")]
    CannotConvertIntegerToFloat,

    #[error("converted range must contain converting range")]
    InvalidIntegerConvert,

    #[error("converted range must contain converting range")]
    InvalidFloatConvert,
}
impl From<ConvertError> for JsonWithCommentsError {
    fn from(err: ConvertError) -> Self {
        JsonWithCommentsError::new(err)
    }
}

#[derive(Error, Debug)]
pub enum InvalidRepresentsValue {
    #[error("Only objects can be converted into map")]
    ShouldObject,

    #[error("Only arrays can be converted into vec")]
    ShouldArray,

    #[error("Only booleans can be converted into bool")]
    ShouldBool,

    #[error("Only nulls can be converted into unit")]
    ShouldNull,

    #[error("Only strings can be converted into string")]
    ShouldString,

    #[error("Only numbers can be converted into number")]
    ShouldNumber,
}
impl From<InvalidRepresentsValue> for JsonWithCommentsError {
    fn from(err: InvalidRepresentsValue) -> Self {
        JsonWithCommentsError::new(err)
    }
}

#[derive(Error, Debug)]
pub enum IndexError {
    #[error("{value} value cannot be indexed by string")]
    StringIndex { value: String },

    #[error("{value} value cannot be indexed by usize")]
    UsizeIndex { value: String },

    #[error("{value} value cannot be indexed by slice index")]
    SliceIndex { value: String },

    #[error("not exist key {key:?}")]
    NotExistKey { key: String },
}
impl From<IndexError> for JsonWithCommentsError {
    fn from(err: IndexError) -> Self {
        JsonWithCommentsError::new(err)
    }
}

#[derive(Error, Debug)]
pub enum Ensure {
    #[error("next should return peeked value")]
    NextAfterPeek,

    #[error("previous peek ensure this eat does not return None")]
    EatAfterLook,

    #[error("next value must exist")]
    NextValue,

    #[error("returns Result for interface reasons, but does not actually fail")]
    EmptyError,

    #[error("unescaped string should be owned because of lifetime")]
    OwnedString,

    #[error("same type conversion should be always possible")]
    CanConvertAlways,

    #[error("unit variant has no value")]
    UnitVariant,

    #[error("ensure seq like variant")]
    SeqLikeVariant,

    #[error("ensure map like variant")]
    MapLikeVariant,
}
impl From<Ensure> for JsonWithCommentsError {
    fn from(err: Ensure) -> Self {
        JsonWithCommentsError::new(err)
    }
}