1use std::fmt::Display;
2
3use serde::{de, ser};
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Debug)]
8pub enum Error {
9 Message(String),
10 Eof,
11 UnsupportedType,
12 TrailingCharacters,
13 ExpectedBoolean,
14 ExpectedString,
15 ExpectedInteger,
16 NonSelfDescribing,
17 ExpectedArray,
18 ExpectedArrayEnd,
19 ArrayIndex,
20 ExpectedMap,
21 ExpectedMapEnd,
22 MapSyntax,
23 SeqSyntax,
24}
25
26impl ser::Error for Error {
27 fn custom<T>(msg: T) -> Self
28 where
29 T: Display,
30 {
31 Error::Message(msg.to_string())
32 }
33}
34
35impl de::Error for Error {
36 fn custom<T>(msg: T) -> Self
37 where
38 T: Display,
39 {
40 Error::Message(msg.to_string())
41 }
42}
43
44impl Display for Error {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 match self {
47 Error::Message(msg) => f.write_str(msg),
48 Error::Eof => f.write_str("unexpected end of string"),
49 Error::UnsupportedType => f.write_str("unsupported data type"),
50 _ => todo!(),
51 }
52 }
53}
54
55impl std::error::Error for Error {}