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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110

use std::error::Error;
use std::io::Error as IoError;

use futures::Canceled;
use futures::sync::mpsc::SendError;

use std::fmt;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum WomboErrorKind {
  /// An error sending a message over an mpsc sender.
  SendError,
  /// See `futures::Canceled`.
  Canceled,
  /// An error indicating the Wombo instance is not initialized.
  NotInitialized,
  /// An unknown error.
  Unknown
}

impl WomboErrorKind {

  pub fn to_string(&self) -> &'static str {
    match *self {
      WomboErrorKind::Canceled       => "Canceled",
      WomboErrorKind::Unknown        => "Unknown",
      WomboErrorKind::SendError      => "Send Error",
      WomboErrorKind::NotInitialized => "Not Initialized"
    }
  }

}

/// A `Wombo` error.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WomboError {
  kind: WomboErrorKind,
  details: String
}

impl WomboError {

  pub fn new<S: Into<String>>(kind: WomboErrorKind, details: S) -> WomboError {
    WomboError { kind, details: details.into() }
  }

  pub fn to_string(&self) -> String {
    format!("{}: {}", self.kind.to_string(), self.details)
  }

  pub fn new_canceled() -> WomboError {
    WomboError {
      kind: WomboErrorKind::Canceled,
      details: String::new()
    }
  }

  pub fn new_not_initialized() -> WomboError {
    WomboError {
      kind: WomboErrorKind::NotInitialized,
      details: String::new()
    }
  }

  pub fn kind(&self) -> &WomboErrorKind {
    &self.kind
  }

}

impl fmt::Display for WomboError {

  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}: {}", self.kind.to_string(), self.details)
  }

}

impl Error for WomboError {

  fn description(&self) -> &str {
    &self.details
  }

}

impl From<()> for WomboError {
  fn from(_: ()) -> Self {
    WomboError::new(WomboErrorKind::Unknown, "Empty error.")
  }
}

impl From<Canceled> for WomboError {
  fn from(e: Canceled) -> Self {
    WomboError::new(WomboErrorKind::Canceled, format!("{}", e))
  }
}

impl<T: Into<WomboError>> From<SendError<T>> for WomboError {
  fn from(e: SendError<T>) -> Self {
    WomboError::new(WomboErrorKind::SendError, format!("{}", e))
  }
}

impl From<IoError> for WomboError {
  fn from(e: IoError) -> Self {
    WomboError::new(WomboErrorKind::Unknown, format!("{}", e))
  }
}