crossterm_utils/
error.rs

1//! Module containing error handling logic.
2
3use std::{
4    fmt::{self, Display, Formatter},
5    io,
6};
7
8use crate::impl_from;
9
10/// The `crossterm` result type.
11pub type Result<T> = std::result::Result<T, ErrorKind>;
12
13/// Wrapper for all errors that can occur in `crossterm`.
14#[derive(Debug)]
15pub enum ErrorKind {
16    IoError(io::Error),
17    FmtError(fmt::Error),
18    Utf8Error(std::string::FromUtf8Error),
19    ParseIntError(std::num::ParseIntError),
20    ResizingTerminalFailure(String),
21
22    #[doc(hidden)]
23    __Nonexhaustive,
24}
25
26impl std::error::Error for ErrorKind {
27    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28        match self {
29            ErrorKind::IoError(e) => Some(e),
30            ErrorKind::FmtError(e) => Some(e),
31            ErrorKind::Utf8Error(e) => Some(e),
32            ErrorKind::ParseIntError(e) => Some(e),
33            _ => None,
34        }
35    }
36}
37
38impl Display for ErrorKind {
39    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
40        match *self {
41            ErrorKind::IoError(_) => write!(fmt, "IO-error occurred"),
42            ErrorKind::ResizingTerminalFailure(_) => write!(fmt, "Cannot resize the terminal"),
43            _ => write!(fmt, "Some error has occurred"),
44        }
45    }
46}
47
48impl_from!(io::Error, ErrorKind::IoError);
49impl_from!(fmt::Error, ErrorKind::FmtError);
50impl_from!(std::string::FromUtf8Error, ErrorKind::Utf8Error);
51impl_from!(std::num::ParseIntError, ErrorKind::ParseIntError);