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