gbdt/
errors.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3use std::io;
4use std::num;
5
6pub type Result<T> = std::result::Result<T, GbdtError>;
7
8#[derive(Debug)]
9pub enum GbdtError {
10    NotSupportExtraMissingNode,
11    ChildrenNotFound,
12    IO(io::Error),
13    ParseInt(num::ParseIntError),
14    ParseFloat(num::ParseFloatError),
15    SerdeJson(serde_json::Error),
16}
17
18impl From<&str> for GbdtError {
19    fn from(err: &str) -> GbdtError {
20        GbdtError::IO(io::Error::new(io::ErrorKind::Other, err))
21    }
22}
23
24impl From<serde_json::Error> for GbdtError {
25    fn from(err: serde_json::Error) -> GbdtError {
26        GbdtError::SerdeJson(err)
27    }
28}
29
30impl From<num::ParseFloatError> for GbdtError {
31    fn from(err: num::ParseFloatError) -> GbdtError {
32        GbdtError::ParseFloat(err)
33    }
34}
35
36impl From<num::ParseIntError> for GbdtError {
37    fn from(err: num::ParseIntError) -> GbdtError {
38        GbdtError::ParseInt(err)
39    }
40}
41
42impl From<io::Error> for GbdtError {
43    fn from(err: io::Error) -> GbdtError {
44        GbdtError::IO(err)
45    }
46}
47
48impl Display for GbdtError {
49    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
50        match *self {
51            GbdtError::NotSupportExtraMissingNode => write!(f, "Not support extra missing node"),
52            GbdtError::ChildrenNotFound => write!(f, "Children not found"),
53            GbdtError::IO(ref e) => write!(f, "IO error: {}", e),
54            GbdtError::ParseInt(ref e) => write!(f, "ParseInt error: {}", e),
55            GbdtError::ParseFloat(ref e) => write!(f, "ParseFloat error: {}", e),
56            GbdtError::SerdeJson(ref e) => write!(f, "SerdeJson error: {}", e),
57        }
58    }
59}
60
61impl Error for GbdtError {
62    fn source(&self) -> Option<&(dyn Error + 'static)> {
63        match *self {
64            GbdtError::NotSupportExtraMissingNode => None,
65            GbdtError::ChildrenNotFound => None,
66            GbdtError::IO(ref e) => Some(e),
67            GbdtError::ParseInt(ref e) => Some(e),
68            GbdtError::ParseFloat(ref e) => Some(e),
69            GbdtError::SerdeJson(ref e) => Some(e),
70        }
71    }
72}