use super::{SessionErrorKind, TelegramErrorKind};
use anyhow;
use thiserror;
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct Error {
#[from]
source: anyhow::Error,
}
impl Error {
pub fn new(err: impl Into<anyhow::Error>) -> Self {
Self {
source: err.into(),
}
}
pub fn from_display(info: impl std::fmt::Display) -> Self {
Self::new(anyhow::anyhow!("{info}"))
}
pub fn from_debug(info: impl std::fmt::Debug) -> Self {
Self::new(anyhow::anyhow!("{info:?}"))
}
}
impl From<TelegramErrorKind> for Error {
fn from(err: TelegramErrorKind) -> Self {
Self::new(err)
}
}
impl From<SessionErrorKind> for Error {
fn from(err: SessionErrorKind) -> Self {
Self::new(err)
}
}
impl From<std::convert::Infallible> for Error {
fn from(_: std::convert::Infallible) -> Self {
unreachable!("Infallible error type should never be constructed")
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::new(err)
}
}
impl From<std::fmt::Error> for Error {
fn from(err: std::fmt::Error) -> Self {
Self::new(err)
}
}
impl From<std::num::ParseIntError> for Error {
fn from(err: std::num::ParseIntError) -> Self {
Self::new(err)
}
}
impl From<std::num::ParseFloatError> for Error {
fn from(err: std::num::ParseFloatError) -> Self {
Self::new(err)
}
}
impl<T> From<Box<T>> for Error
where
T: std::error::Error + Send + Sync + 'static,
{
fn from(err: Box<T>) -> Self {
Self::new(err)
}
}