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 pub enum ErrorKind { 16 IoError(io::Error), 17 FmtError(fmt::Error), 18 Utf8Error(std::string::FromUtf8Error), 19 ParseIntError(std::num::ParseIntError), 20 ResizingTerminalFailure(String), 21 SettingTerminalTitleFailure, 22 #[doc(hidden)] 23 __Nonexhaustive, 24 } 25 26 impl std::error::Error for ErrorKind { source(&self) -> Option<&(dyn std::error::Error + 'static)>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 38 impl Display for ErrorKind { fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result39 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 48 impl_from!(io::Error, ErrorKind::IoError); 49 impl_from!(fmt::Error, ErrorKind::FmtError); 50 impl_from!(std::string::FromUtf8Error, ErrorKind::Utf8Error); 51 impl_from!(std::num::ParseIntError, ErrorKind::ParseIntError); 52