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