1use serde::{de, ser};
2use std::fmt::Display;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
7pub enum Error {
8 #[error("Unexpected character '{0}'")]
9 UnexpectedChar(char),
10 #[error("Unterminated string")]
11 UnclosedString,
12 #[error("Unterminated data block")]
13 UnclosedData,
14 #[error("Data block did not contain valid paired hex digits")]
15 BadData,
16 #[error("Unknown escape code")]
17 UnknownEscape,
18 #[error("Invalid unicode escape sequence: '{0}'")]
19 InvalidUnicodeEscape(String),
20 #[error("Expected string, found '{token_name}")]
21 NotAString { token_name: &'static str },
22 #[error("Missing '='")]
23 ExpectedEquals,
24 #[error("Missing ','")]
25 ExpectedComma,
26 #[error("Missing ';'")]
27 ExpectedSemicolon,
28 #[error("Missing '{{'")]
29 ExpectedOpenBrace,
30 #[error("Missing '}}'")]
31 ExpectedCloseBrace,
32 #[error("Missing '('")]
33 ExpectedOpenParen,
34 #[error("Missing ')'")]
35 ExpectedCloseParen,
36 #[error("Expected character '{0}'")]
37 ExpectedChar(char),
38 #[error("Expected numeric value")]
39 ExpectedNumber,
40 #[error("Expected string value")]
41 ExpectedString,
42 #[error("Expected '{expected}', found '{found}'")]
43 UnexpectedDataType {
44 expected: &'static str,
45 found: &'static str,
46 },
47 #[error("Unexpected token '{name}'")]
48 UnexpectedToken { name: &'static str },
49 #[error("parsing failed: '{0}'")]
50 Parse(String),
51 #[error("serializing failed: '{0}'")]
52 Serialize(String),
53}
54
55impl ser::Error for Error {
56 fn custom<T: Display>(msg: T) -> Self {
57 Error::Serialize(msg.to_string())
58 }
59}
60
61impl de::Error for Error {
62 fn custom<T: Display>(msg: T) -> Self {
63 Error::Parse(msg.to_string())
64 }
65}