error.rs (2017B)
1 use std::fmt; 2 3 #[derive(Debug)] 4 pub enum Error { 5 Io(std::io::Error), 6 Ureq(ureq::Error), 7 ParseInt(std::num::ParseIntError), 8 HttpClientError(u16), 9 DownloadFailed { url: String, tries: u32 }, 10 MissingElement(&'static str), 11 EmptyFile(String), 12 NoPosts, 13 UnknownExporter(String), 14 ChannelSend, 15 } 16 17 pub type Result<T> = std::result::Result<T, Error>; 18 19 impl fmt::Display for Error { 20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 21 match self { 22 Error::Io(e) => write!(f, "I/O error: {e}"), 23 Error::Ureq(e) => write!(f, "network error: {e}"), 24 Error::ParseInt(e) => write!(f, "parse error: {e}"), 25 Error::HttpClientError(code) => write!(f, "client error: {code}"), 26 Error::DownloadFailed { url, tries } => { 27 write!(f, "failed to download {url} after {tries} tries") 28 } 29 Error::MissingElement(name) => write!(f, "missing element: {name}"), 30 Error::EmptyFile(url) => write!(f, "empty file: {url}"), 31 Error::NoPosts => write!(f, "no posts to export"), 32 Error::UnknownExporter(name) => write!(f, "unknown exporter: {name}"), 33 Error::ChannelSend => write!(f, "failed to send event"), 34 } 35 } 36 } 37 38 impl std::error::Error for Error { 39 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 40 match self { 41 Error::Io(e) => Some(e), 42 Error::Ureq(e) => Some(e), 43 Error::ParseInt(e) => Some(e), 44 _ => None, 45 } 46 } 47 } 48 49 impl From<std::io::Error> for Error { 50 fn from(e: std::io::Error) -> Self { Error::Io(e) } 51 } 52 53 impl From<ureq::Error> for Error { 54 fn from(e: ureq::Error) -> Self { Error::Ureq(e) } 55 } 56 57 impl From<std::num::ParseIntError> for Error { 58 fn from(e: std::num::ParseIntError) -> Self { Error::ParseInt(e) } 59 } 60 61 impl<T> From<std::sync::mpsc::SendError<T>> for Error { 62 fn from(_: std::sync::mpsc::SendError<T>) -> Self { Error::ChannelSend } 63 }