rtdlib/
errors.rs

1
2use std::{io, fmt, error};
3
4#[derive(Debug)]
5pub enum RTDError {
6  Io(io::Error),
7  SerdeJson(serde_json::Error),
8  Custom(String),
9}
10
11pub type RTDResult<T> = Result<T, RTDError>;
12
13impl RTDError {
14  pub fn custom(msg: String) -> Self { RTDError::Custom(msg) }
15}
16
17impl fmt::Display for RTDError {
18  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19    match self {
20      RTDError::Io(ref err) => write!(f, "IO error: {}", err),
21      RTDError::SerdeJson(ref err) => write!(f, "Serde json error: {}", err),
22      RTDError::Custom(msg) => write!(f, "{}", msg),
23    }
24  }
25}
26
27impl error::Error for RTDError {
28  fn cause(&self) -> Option<&dyn error::Error> {
29    match *self {
30      RTDError::Io(ref err) => Some(err),
31      RTDError::SerdeJson(ref err) => Some(err),
32      RTDError::Custom(_) => None
33    }
34  }
35}
36
37impl From<io::Error> for RTDError {
38  fn from(err: io::Error) -> RTDError {
39    RTDError::Io(err)
40  }
41}
42
43impl From<serde_json::Error> for RTDError {
44  fn from(err: serde_json::Error) -> RTDError {
45    RTDError::SerdeJson(err)
46  }
47}
48
49