jzon/
error.rs

1use std::{ char, error, fmt };
2
3/// Error type of this crate.
4///
5///
6/// *Note:* Since `0.9.0` using `JsonError` is deprecated. Always use
7/// `jzon::Error` instead!
8#[derive(Debug, PartialEq, Eq)]
9pub enum Error {
10    UnexpectedCharacter {
11        ch: char,
12        line: usize,
13        column: usize,
14    },
15    UnexpectedEndOfJson,
16    ExceededDepthLimit,
17    FailedUtf8Parsing,
18    WrongType(String),
19}
20
21impl Error {
22    pub fn wrong_type(expected: &str) -> Self {
23        Error::WrongType(expected.into())
24    }
25}
26
27impl fmt::Display for Error {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        use Error::*;
30
31        match *self {
32            UnexpectedCharacter {
33                ref ch,
34                ref line,
35                ref column,
36            } => write!(f, "Unexpected character: {} at ({}:{})", ch, line, column),
37
38            UnexpectedEndOfJson   => write!(f, "Unexpected end of JSON"),
39            ExceededDepthLimit    => write!(f, "Exceeded depth limit"),
40            FailedUtf8Parsing     => write!(f, "Failed to parse UTF-8 bytes"),
41            WrongType(ref s)      => write!(f, "Wrong type, expected: {}", s),
42        }
43    }
44}
45
46impl error::Error for Error {
47    fn description(&self) -> &str {
48        use Error::*;
49
50        match *self {
51            UnexpectedCharacter { .. } => "Unexpected character",
52            UnexpectedEndOfJson        => "Unexpected end of JSON",
53            ExceededDepthLimit         => "Exceeded depth limit",
54            FailedUtf8Parsing          => "Failed to read bytes as UTF-8 from JSON",
55            WrongType(_)               => "Wrong type",
56        }
57    }
58}