Skip to main content

keri_core/error/
serializer_error.rs

1use std;
2use std::fmt::{self, Display};
3
4use serde::{de, ser, Deserialize, Serialize};
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(Clone, Debug, PartialEq, Serialize, Deserialize)]
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    Eof,
25    Syntax,
26    ExpectedBoolean,
27    ExpectedInteger,
28    ExpectedString,
29    ExpectedNull,
30    ExpectedArray,
31    ExpectedArrayComma,
32    ExpectedArrayEnd,
33    ExpectedMap,
34    ExpectedMapColon,
35    ExpectedMapComma,
36    ExpectedMapEnd,
37    ExpectedEnum,
38    TrailingCharacters,
39}
40
41impl ser::Error for Error {
42    fn custom<T: Display>(msg: T) -> Self {
43        Error::Message(msg.to_string())
44    }
45}
46
47impl de::Error for Error {
48    fn custom<T: Display>(msg: T) -> Self {
49        Error::Message(msg.to_string())
50    }
51}
52
53impl Display for Error {
54    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
55        match self {
56            Error::Message(msg) => formatter.write_str(msg),
57            Error::Eof => formatter.write_str("unexpected end of input"),
58            Error::Syntax => formatter.write_str("incorrect syntax"),
59            Error::ExpectedBoolean => formatter.write_str("incorrect input: expected boolean"),
60            Error::ExpectedInteger => formatter.write_str("incorrect input: expected integer"),
61            Error::ExpectedString => formatter.write_str("incorrect input: expected string"),
62            Error::ExpectedNull => formatter.write_str("incorrect input: expected null"),
63            Error::ExpectedArray => formatter.write_str("incorrect input: expected array"),
64            Error::ExpectedArrayComma => {
65                formatter.write_str("incorrect input: expected array comma")
66            }
67            Error::ExpectedArrayEnd => formatter.write_str("incorrect input: expected array end"),
68            Error::ExpectedMap => formatter.write_str("incorrect input: expected map"),
69            Error::ExpectedMapColon => formatter.write_str("incorrect input: expected map colon"),
70            Error::ExpectedMapComma => formatter.write_str("incorrect input: expected map comma"),
71            Error::ExpectedMapEnd => formatter.write_str("incorrect input: expected map end"),
72            Error::ExpectedEnum => formatter.write_str("incorrect input: expected enum"),
73            Error::TrailingCharacters => {
74                formatter.write_str("incorrect input: unexpected trailing characters")
75            }
76        }
77    }
78}
79
80impl std::error::Error for Error {}