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

use std::{io, fmt, error};

#[derive(Debug)]
pub enum RTDError {
  Io(io::Error),
  SerdeJson(serde_json::Error),
  Custom(&'static str),
}

pub type RTDResult<T> = Result<T, RTDError>;

impl RTDError {
  pub fn custom(msg: &'static str) -> Self { RTDError::Custom(msg) }
}

impl fmt::Display for RTDError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match *self {
      RTDError::Io(ref err) => write!(f, "IO error: {}", err),
      RTDError::SerdeJson(ref err) => write!(f, "Serde json error: {}", err),
      RTDError::Custom(msg) => write!(f, "{}", msg),
    }
  }
}

impl error::Error for RTDError {
  fn description(&self) -> &str {
    match *self {
      RTDError::Io(ref err) => err.description(),
      RTDError::SerdeJson(ref err) => err.description(),
      RTDError::Custom(msg) => msg,
    }
  }

  fn cause(&self) -> Option<&error::Error> {
    match *self {
      RTDError::Io(ref err) => Some(err),
      RTDError::SerdeJson(ref err) => Some(err),
      RTDError::Custom(_) => None
    }
  }
}

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

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

//impl From<str> for