use hyper::StatusCode;
use std::{
error::Error,
fmt::{self, Display, Formatter},
};
#[derive(Debug)]
pub enum Download {
NoPath,
Network(hyper::Error),
InvalidStatusCode(StatusCode),
}
impl Download {
pub fn is_no_path(&self) -> bool {
match self {
Download::NoPath => true,
_ => false,
}
}
pub fn is_network(&self) -> bool {
match self {
Download::Network(..) => true,
_ => false,
}
}
pub fn is_invalid_status_code(&self) -> bool {
match self {
Download::InvalidStatusCode(..) => true,
_ => false,
}
}
}
impl Display for Download {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
Download::NoPath => write!(
formatter,
"A file could not be downloaded because of missing `path`.",
),
Download::Network(error) => write!(
formatter,
"A file could not be downloaded because of a network error: {}",
error,
),
Download::InvalidStatusCode(code) => write!(
formatter,
"A file could not be downloaded because Telegram responded \
with {} instead of 200 OK.",
code,
),
}
}
}
impl Error for Download {}
impl From<hyper::Error> for Download {
fn from(error: hyper::Error) -> Self {
Download::Network(error)
}
}
impl From<StatusCode> for Download {
fn from(error: StatusCode) -> Self {
Download::InvalidStatusCode(error)
}
}