| 1 | use crate::platform; |
| 2 | use std::fmt; |
| 3 | |
| 4 | /// Ctrl-C error. |
| 5 | #[derive (Debug)] |
| 6 | pub enum Error { |
| 7 | /// Signal could not be found from the system. |
| 8 | NoSuchSignal(crate::SignalType), |
| 9 | /// Ctrl-C signal handler already registered. |
| 10 | MultipleHandlers, |
| 11 | /// Unexpected system error. |
| 12 | System(std::io::Error), |
| 13 | } |
| 14 | |
| 15 | impl Error { |
| 16 | fn describe(&self) -> &str { |
| 17 | match *self { |
| 18 | Error::NoSuchSignal(_) => "Signal could not be found from the system" , |
| 19 | Error::MultipleHandlers => "Ctrl-C signal handler already registered" , |
| 20 | Error::System(_) => "Unexpected system error" , |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl From<platform::Error> for Error { |
| 26 | fn from(e: platform::Error) -> Error { |
| 27 | #[cfg (not(windows))] |
| 28 | if e == platform::Error::EEXIST { |
| 29 | return Error::MultipleHandlers; |
| 30 | } |
| 31 | |
| 32 | let system_error: Error = std::io::Error::new(kind:std::io::ErrorKind::Other, error:e); |
| 33 | Error::System(system_error) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl fmt::Display for Error { |
| 38 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 39 | write!(f, "Ctrl-C error: {}" , self.describe()) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl std::error::Error for Error { |
| 44 | fn description(&self) -> &str { |
| 45 | self.describe() |
| 46 | } |
| 47 | |
| 48 | fn cause(&self) -> Option<&dyn std::error::Error> { |
| 49 | match *self { |
| 50 | Error::System(ref e: &Error) => Some(e), |
| 51 | _ => None, |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |