1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use core::fmt;
use std::{error, io};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Cause {
    ExpectsEnum,
    ExpectsObject,
    InvalidUtf8,
    IsMandatory,
    IsInvalid,
    MustBeArray,
    MustBeBoolean,
    MustBeObject,
    MustBeString,
    MustBeUnsignedInteger,
    MustMapToStringOrNull,
    UnexpectedEof,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Error {
    pub attribute: &'static str,
    pub cause: Cause,
}

impl Error {
    pub const fn new(attribute: &'static str, cause: Cause) -> Self {
        Self { attribute, cause }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        <Error as fmt::Debug>::fmt(self, f)
    }
}

impl error::Error for Error {}

impl From<Error> for io::Error {
    fn from(e: Error) -> Self {
        io::Error::new(io::ErrorKind::Other, e)
    }
}