use core::str::Utf8Error; use regex::Error as RegexError; use std::{error, fmt, io::Error as IoError, string::FromUtf8Error}; /// A custom error type encapsulating all possible errors for this library. /// `From` implementations are provided for external error types, allowing /// the `?` operator to be used on function which return `Result<_, SbotCliError>`. #[derive(Debug)] pub enum SbotCliError { // sbotcli errors Blob(String), Contact(String), GetAboutMsgs(String), Invite(String), Publish(String), WhoAmI(String), // std errors CommandIo(IoError), ConvertUtf8(FromUtf8Error), InvalidUtf8(Utf8Error), // external errors InvalidRegex(RegexError), } impl error::Error for SbotCliError {} impl fmt::Display for SbotCliError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { SbotCliError::Blob(ref err) => { write!(f, "{}", err) } SbotCliError::Contact(ref err) => { write!(f, "{}", err) } SbotCliError::GetAboutMsgs(ref err) => { write!(f, "{}", err) } SbotCliError::Invite(ref err) => { write!(f, "{}", err) } SbotCliError::Publish(ref err) => { write!(f, "{}", err) } SbotCliError::WhoAmI(ref err) => { write!(f, "{}", err) } SbotCliError::CommandIo(ref err) => { write!(f, "{}", err) } SbotCliError::ConvertUtf8(ref err) => { write!(f, "{}", err) } SbotCliError::InvalidUtf8(ref err) => { write!(f, "{}", err) } SbotCliError::InvalidRegex(ref err) => { write!(f, "{}", err) } } } } impl From for SbotCliError { fn from(err: IoError) -> SbotCliError { SbotCliError::CommandIo(err) } } impl From for SbotCliError { fn from(err: FromUtf8Error) -> SbotCliError { SbotCliError::ConvertUtf8(err) } } impl From for SbotCliError { fn from(err: Utf8Error) -> SbotCliError { SbotCliError::InvalidUtf8(err) } } impl From for SbotCliError { fn from(err: RegexError) -> SbotCliError { SbotCliError::InvalidRegex(err) } }