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 {
describe(&self) -> &str16     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 {
from(e: platform::Error) -> Error26     fn from(e: platform::Error) -> Error {
27         let system_error = std::io::Error::new(std::io::ErrorKind::Other, e);
28         Error::System(system_error)
29     }
30 }
31 
32 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result33     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34         write!(f, "Ctrl-C error: {}", self.describe())
35     }
36 }
37 
38 impl std::error::Error for Error {
description(&self) -> &str39     fn description(&self) -> &str {
40         self.describe()
41     }
42 
cause(&self) -> Option<&dyn std::error::Error>43     fn cause(&self) -> Option<&dyn std::error::Error> {
44         match *self {
45             Error::System(ref e) => Some(e),
46             _ => None,
47         }
48     }
49 }
50