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