1use core::fmt::{Display, Formatter, Result};
4
5use serde::de;
6
7#[derive(Debug)]
9pub struct Error(Box<Kind>);
10
11#[derive(Debug, PartialEq)]
13pub enum Kind {
14 Message(Box<str>),
16
17 Eof,
19 ExpectedColon,
21 UnexpectedToken,
23 UnclosedString,
25 ControlCharacter,
27 InvalidEscapeSequnce,
29 InvalidLiteral,
31 TrailingComma,
33 LeadingDecimal,
35 TrailingDecimal,
37 LeadingZero,
39 NumberOverflow,
41}
42
43impl Error {
44 pub fn kind(&self) -> &Kind {
46 &self.0
47 }
48}
49
50impl Into<Error> for Kind {
51 #[cold]
52 fn into(self) -> Error {
53 Error(Box::new(self))
54 }
55}
56
57impl Display for Error {
58 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
59 f.write_str(match &*self.0 {
60 Kind::Message(data) => data,
61 Kind::Eof => "eof while parsing",
62 Kind::ExpectedColon => "expected colon",
63 Kind::UnexpectedToken => "unexpected token",
64 Kind::UnclosedString => "unclosed string",
65 Kind::ControlCharacter => "control character inside string",
66 Kind::InvalidEscapeSequnce => "invalid escape sequence",
67 Kind::InvalidLiteral => "invalid literal",
68 Kind::TrailingComma => "trailing comma",
69 Kind::LeadingDecimal => "leading decimal in number",
70 Kind::TrailingDecimal => "trailing decimal in number",
71 Kind::LeadingZero => "leading zero in number",
72 Kind::NumberOverflow => "number too large",
73 })
74 }
75}
76
77impl de::Error for Error {
78 fn custom<T: Display>(msg: T) -> Self {
79 Error(Box::new(Kind::Message(msg.to_string().into_boxed_str())))
80 }
81}
82
83impl core::error::Error for Error {}