serde_intermediate/
error.rs

1use crate::value::intermediate::Intermediate;
2use std::fmt::Display;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, Clone)]
7pub enum Error {
8    Message(String),
9    ExpectedMapEntry,
10    ExpectedStructField,
11    ExpectedUnitVariant,
12    ExpectedNewTypeVariant,
13    ExpectedTupleVariant,
14    ExpectedStructVariant,
15    NotPartial(Intermediate),
16    NotSeq(Intermediate),
17    NotTuple(Intermediate),
18    NotMap(Intermediate),
19    NotStruct(Intermediate),
20    CannotAdd(Intermediate),
21    CannotRemove(Intermediate),
22    /// (value, old size, new size)
23    InvalidSize(Intermediate, usize, usize),
24    NoNextTokens,
25    InvalidTokens(String),
26    CannotParse(String),
27}
28
29impl serde::ser::Error for Error {
30    fn custom<T: Display>(msg: T) -> Self {
31        Error::Message(msg.to_string())
32    }
33}
34
35impl serde::de::Error for Error {
36    fn custom<T: Display>(msg: T) -> Self {
37        Error::Message(msg.to_string())
38    }
39}
40
41impl Display for Error {
42    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
43        match self {
44            Error::Message(msg) => formatter.write_str(msg),
45            Error::ExpectedMapEntry => formatter.write_str("expected map entry"),
46            Error::ExpectedStructField => formatter.write_str("expected struct field"),
47            Error::ExpectedUnitVariant => formatter.write_str("expected unit variant"),
48            Error::ExpectedNewTypeVariant => formatter.write_str("expected newtype variant"),
49            Error::ExpectedTupleVariant => formatter.write_str("expected tuple variant"),
50            Error::ExpectedStructVariant => formatter.write_str("expected struct variant"),
51            Error::NotPartial(_) => formatter.write_str("value is not a partial"),
52            Error::NotSeq(_) => formatter.write_str("value is not a sequence"),
53            Error::NotTuple(_) => formatter.write_str("value is not a tuple"),
54            Error::NotMap(_) => formatter.write_str("value is not a map"),
55            Error::NotStruct(_) => formatter.write_str("value is not a struct"),
56            Error::CannotAdd(_) => formatter.write_str("cannot add value here"),
57            Error::CannotRemove(_) => formatter.write_str("cannot remove value here"),
58            Error::InvalidSize(_, _, _) => formatter.write_str("invalid size here"),
59            Error::NoNextTokens => formatter.write_str("no next tokens"),
60            Error::InvalidTokens(_) => formatter.write_str("invalid tokens"),
61            Error::CannotParse(_) => formatter.write_str("cannot parse"),
62        }
63    }
64}
65
66impl std::error::Error for Error {}