Skip to main content

fixed_json/
error.rs

1pub type Result<T> = core::result::Result<T, Error>;
2
3#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4#[repr(i32)]
5pub enum Error {
6    ObStart = 1,
7    AttrStart = 2,
8    BadAttr = 3,
9    AttrLen = 4,
10    NoArray = 5,
11    NoBrak = 6,
12    StrLong = 7,
13    TokLong = 8,
14    BadTrail = 9,
15    ArrayStart = 10,
16    ObjArr = 11,
17    SubTooLong = 12,
18    BadSubTrail = 13,
19    SubType = 14,
20    BadString = 15,
21    CheckFail = 16,
22    NoParStr = 17,
23    BadEnum = 18,
24    QNonString = 19,
25    NonQString = 20,
26    Misc = 21,
27    BadNum = 22,
28    NullPtr = 23,
29    NoCurly = 24,
30    Empty = 25,
31}
32
33impl Error {
34    #[inline]
35    pub const fn message(self) -> &'static str {
36        match self {
37            Error::ObStart => "non-whitespace when expecting object start",
38            Error::AttrStart => "non-whitespace when expecting attribute start",
39            Error::BadAttr => "unknown attribute name",
40            Error::AttrLen => "attribute name too long",
41            Error::NoArray => "saw [ when not expecting array",
42            Error::NoBrak => "array element specified, but no [",
43            Error::StrLong => "string value too long",
44            Error::TokLong => "token value too long",
45            Error::BadTrail => "garbage while expecting comma or } or ]",
46            Error::ArrayStart => "didn't find expected array start",
47            Error::ObjArr => "error while parsing object array",
48            Error::SubTooLong => "too many array elements",
49            Error::BadSubTrail => "garbage while expecting array comma",
50            Error::SubType => "unsupported array element type",
51            Error::BadString => "error while string parsing",
52            Error::CheckFail => "check attribute not matched",
53            Error::NoParStr => "can't support strings in parallel arrays",
54            Error::BadEnum => "invalid enumerated value",
55            Error::QNonString => "saw quoted value when expecting nonstring",
56            Error::NonQString => "didn't see quoted value when expecting string",
57            Error::Misc => "other data conversion error",
58            Error::BadNum => "error while parsing a numerical argument",
59            Error::NullPtr => "unexpected null value or attribute pointer",
60            Error::NoCurly => "object element specified, but no {",
61            Error::Empty => "input was empty or white-space only",
62        }
63    }
64}
65
66impl core::fmt::Display for Error {
67    #[inline]
68    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
69        f.write_str(self.message())
70    }
71}
72
73#[cfg(feature = "std")]
74impl std::error::Error for Error {}
75
76#[inline]
77pub fn error_string(err: Error) -> &'static str {
78    err.message()
79}
80
81#[cfg(test)]
82mod tests {
83    extern crate std;
84
85    use self::std::string::ToString;
86    use super::{Error, error_string};
87
88    #[test]
89    fn error_helpers_return_human_readable_messages() {
90        assert_eq!(
91            Error::BadNum.message(),
92            "error while parsing a numerical argument"
93        );
94        assert_eq!(
95            error_string(Error::NoCurly),
96            "object element specified, but no {"
97        );
98        assert_eq!(Error::BadAttr.to_string(), "unknown attribute name");
99    }
100}