pax_designtime/serde_pax/
error.rs

1use std;
2use std::fmt::{self, Display};
3
4use serde::{de, ser};
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8// This is a bare-bones implementation. A real library would provide additional
9// information in its error type, for example the line and column at which the
10// error occurred, the byte offset into the input, or the current key being
11// processed.
12#[derive(Debug)]
13pub enum Error {
14    // One or more variants that can be created by data structures through the
15    // `ser::Error` and `de::Error` traits. For example the Serialize impl for
16    // Mutex<T> might return an error because the mutex is poisoned, or the
17    // Deserialize impl for a struct may return an error because a required
18    // field is missing.
19    Message(String),
20
21    // Zero or more variants that can be created directly by the Serializer and
22    // Deserializer without going through `ser::Error` and `de::Error`. These
23    // are specific to the format, in this case JSON.
24    UnsupportedType(String),
25    TrailingCharacters,
26    UnsupportedMethod,
27}
28
29impl ser::Error for Error {
30    fn custom<T: Display>(msg: T) -> Self {
31        Error::Message(msg.to_string())
32    }
33}
34
35impl 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 fmt::Formatter) -> fmt::Result {
43        match self {
44            Error::Message(msg) => formatter.write_str(msg),
45            Error::UnsupportedType(t) => {
46                formatter.write_str(format!("unsupported type: {}", t).as_str())
47            }
48            Error::TrailingCharacters => {
49                formatter.write_str("trailing characters after deserialization")
50            }
51            _ => formatter.write_str("unknown error"),
52        }
53    }
54}
55
56impl std::error::Error for Error {}