ctrlc_async/
error.rs

1use crate::platform;
2use std::fmt;
3
4/// Ctrl-C error.
5#[derive(Debug)]
6pub 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    /// The handler has exited.
12    NoHandler,
13    /// Unexpected system error.
14    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}