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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::io;
use std::num;

pub type Result<T> = std::result::Result<T, GbdtError>;

#[derive(Debug)]
pub enum GbdtError {
    NotSupportExtraMissingNode,
    ChildrenNotFound,
    IO(io::Error),
    ParseInt(num::ParseIntError),
    ParseFloat(num::ParseFloatError),
    SerdeJson(serde_json::Error),
}

impl From<&str> for GbdtError {
    fn from(err: &str) -> GbdtError {
        GbdtError::IO(io::Error::new(io::ErrorKind::Other, err))
    }
}

impl From<serde_json::Error> for GbdtError {
    fn from(err: serde_json::Error) -> GbdtError {
        GbdtError::SerdeJson(err)
    }
}

impl From<num::ParseFloatError> for GbdtError {
    fn from(err: num::ParseFloatError) -> GbdtError {
        GbdtError::ParseFloat(err)
    }
}

impl From<num::ParseIntError> for GbdtError {
    fn from(err: num::ParseIntError) -> GbdtError {
        GbdtError::ParseInt(err)
    }
}

impl From<io::Error> for GbdtError {
    fn from(err: io::Error) -> GbdtError {
        GbdtError::IO(err)
    }
}

impl Display for GbdtError {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        match *self {
            GbdtError::NotSupportExtraMissingNode => write!(f, "Not support extra missing node"),
            GbdtError::ChildrenNotFound => write!(f, "Children not found"),
            GbdtError::IO(ref e) => write!(f, "IO error: {}", e),
            GbdtError::ParseInt(ref e) => write!(f, "ParseInt error: {}", e),
            GbdtError::ParseFloat(ref e) => write!(f, "ParseFloat error: {}", e),
            GbdtError::SerdeJson(ref e) => write!(f, "SerdeJson error: {}", e),
        }
    }
}

impl Error for GbdtError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            GbdtError::NotSupportExtraMissingNode => None,
            GbdtError::ChildrenNotFound => None,
            GbdtError::IO(ref e) => Some(e),
            GbdtError::ParseInt(ref e) => Some(e),
            GbdtError::ParseFloat(ref e) => Some(e),
            GbdtError::SerdeJson(ref e) => Some(e),
        }
    }
}