Skip to main content

ephemeris_documents/error/
json.rs

1//! Errors from parsing JSON and JSON5.
2
3/// Error returned while parsing a JSON or JSON5 document.
4#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
5#[error("invalid JSON at byte {offset}: {kind}")]
6pub struct JsonError {
7    /// Byte offset into the source where the problem starts.
8    pub offset: usize,
9    /// What went wrong.
10    pub kind: JsonErrorKind,
11}
12
13/// The reason a JSON or JSON5 document failed to parse.
14#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
15pub enum JsonErrorKind {
16    /// The input ended inside a value.
17    #[error("input ends inside a value")]
18    UnexpectedEnd,
19    /// A character cannot start or continue the current construct.
20    #[error("character '{found}' is unexpected")]
21    UnexpectedCharacter {
22        /// The offending character.
23        found: char,
24    },
25    /// A string has no closing quote before the end of its line or the input.
26    #[error("string is unterminated")]
27    UnterminatedString,
28    /// A block comment has no closing delimiter.
29    #[error("block comment is unterminated")]
30    UnterminatedComment,
31    /// A string or identifier escape sequence is malformed or cannot be
32    /// decoded.
33    #[error("escape sequence is invalid")]
34    InvalidEscape,
35    /// A number literal is malformed.
36    #[error("number is invalid")]
37    InvalidNumber,
38    /// An object has two members with the same decoded key.
39    #[error("key `{name}` appears more than once")]
40    DuplicateKey {
41        /// The decoded key that appears twice.
42        name: String,
43    },
44    /// Content follows the root value.
45    #[error("content follows the root value")]
46    TrailingContent,
47    /// Arrays and objects are nested deeper than the parser allows.
48    #[error("nesting is too deep")]
49    DepthExceeded,
50}