vdf_serde_format/
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, PartialEq)]
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    ExpectedMap,
32    ExpectedMapKey,
33    ExpectedMapEnd,
34    ExpectedEnum,
35    CannotSupportedTupleType,
36    TrailingCharacters,
37
38    ExpectedStringOrBlock,
39    UnsupportedEnums,
40    UnsupportedSelfDiscribing,
41}
42
43impl ser::Error for Error {
44    fn custom<T: Display>(msg: T) -> Self {
45        Error::Message(msg.to_string())
46    }
47}
48
49impl de::Error for Error {
50    fn custom<T: Display>(msg: T) -> Self {
51        Error::Message(msg.to_string())
52    }
53}
54
55impl Display for Error {
56    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
57        match self {
58            Error::Message(msg) => formatter.write_str(msg),
59            Error::Eof => formatter.write_str("unexpected end of input"),
60            Error::Syntax => formatter.write_str("syntax error"),
61            Error::ExpectedBoolean => formatter.write_str("expected boolean"),
62            Error::ExpectedInteger => formatter.write_str("expected integer"),
63            Error::ExpectedString => formatter.write_str("expected string"),
64            Error::ExpectedNull => formatter.write_str("expected null"),
65            Error::ExpectedArray => formatter.write_str("expected array"),
66            Error::ExpectedMap => formatter.write_str("expected map"),
67            Error::ExpectedMapKey => formatter.write_str("expected map key"),
68            Error::ExpectedMapEnd => formatter.write_str("expected map end"),
69            Error::ExpectedEnum => formatter.write_str("expected enum"),
70            Error::CannotSupportedTupleType => formatter.write_str("cannot supported tuple type, make a Tuple Struct instead, i.e. enum { Example { x: i32, y: i32 } } or "),
71            Error::TrailingCharacters => formatter.write_str("trailing characters"),
72            Error::ExpectedStringOrBlock => formatter.write_str("expected string or block"),
73            Error::UnsupportedEnums => formatter.write_str("unsupported enums"),
74            Error::UnsupportedSelfDiscribing => formatter.write_str("unsupported self-discribing [ANY]"),
75            /* and so forth */
76            // #[allow(unreachable_patterns)]
77            // _ => formatter.write_str("unknown parse error"),
78        }
79    }
80}
81
82impl std::error::Error for Error {}
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn test_display_message() {
89        let error = Error::Message("custom error".to_string());
90        assert_eq!(format!("{}", error), "custom error");
91    }
92
93    #[test]
94    fn test_display_eof() {
95        let error = Error::Eof;
96        assert_eq!(format!("{}", error), "unexpected end of input");
97    }
98
99    #[test]
100    fn test_display_syntax() {
101        let error = Error::Syntax;
102        assert_eq!(format!("{}", error), "syntax error");
103    }
104
105    #[test]
106    fn test_display_expected_boolean() {
107        let error = Error::ExpectedBoolean;
108        assert_eq!(format!("{}", error), "expected boolean");
109    }
110
111    #[test]
112    fn test_display_expected_integer() {
113        let error = Error::ExpectedInteger;
114        assert_eq!(format!("{}", error), "expected integer");
115    }
116
117    #[test]
118    fn test_display_expected_string() {
119        let error = Error::ExpectedString;
120        assert_eq!(format!("{}", error), "expected string");
121    }
122
123    #[test]
124    fn test_display_expected_null() {
125        let error = Error::ExpectedNull;
126        assert_eq!(format!("{}", error), "expected null");
127    }
128
129    #[test]
130    fn test_display_expected_array() {
131        let error = Error::ExpectedArray;
132        assert_eq!(format!("{}", error), "expected array");
133    }
134
135    #[test]
136    fn test_display_expected_map() {
137        let error = Error::ExpectedMap;
138        assert_eq!(format!("{}", error), "expected map");
139    }
140
141    #[test]
142    fn test_display_expected_map_key() {
143        let error = Error::ExpectedMapKey;
144        assert_eq!(format!("{}", error), "expected map key");
145    }
146
147    #[test]
148    fn test_display_expected_map_end() {
149        let error = Error::ExpectedMapEnd;
150        assert_eq!(format!("{}", error), "expected map end");
151    }
152
153    #[test]
154    fn test_display_expected_enum() {
155        let error = Error::ExpectedEnum;
156        assert_eq!(format!("{}", error), "expected enum");
157    }
158
159    #[test]
160    fn test_display_cannot_supported_tuple_type() {
161        let error = Error::CannotSupportedTupleType;
162        assert_eq!(format!("{}", error), "cannot supported tuple type, make a Tuple Struct instead, i.e. enum { Example { x: i32, y: i32 } } or ");
163    }
164
165    #[test]
166    fn test_display_trailing_characters() {
167        let error = Error::TrailingCharacters;
168        assert_eq!(format!("{}", error), "trailing characters");
169    }
170
171    #[test]
172    fn test_display_expected_string_or_block() {
173        let error = Error::ExpectedStringOrBlock;
174        assert_eq!(format!("{}", error), "expected string or block");
175    }
176
177    #[test]
178    fn test_display_unsupported_enums() {
179        let error = Error::UnsupportedEnums;
180        assert_eq!(format!("{}", error), "unsupported enums");
181    }
182
183    #[test]
184    fn test_display_unsupported_self_discribing() {
185        let error = Error::UnsupportedSelfDiscribing;
186        assert_eq!(format!("{}", error), "unsupported self-discribing [ANY]");
187    }
188
189    #[test]
190    fn test_display_unknown() {
191        let error = Error::Message("unknown error".to_string());
192        assert_eq!(format!("{}", error), "unknown error");
193    }
194
195    #[test]
196    fn test_ser_error() {
197        let error: Error = ser::Error::custom("serialization error");
198        assert_eq!(error, Error::Message("serialization error".to_string()));
199    }
200
201    #[test]
202    fn test_de_error() {
203        let error: Error = de::Error::custom("deserialization error");
204        assert_eq!(error, Error::Message("deserialization error".to_string()));
205    }
206}