use anyhow;
use std::{
convert::Infallible,
fmt, io,
num::{ParseFloatError, ParseIntError},
};
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 fmt::Display) -> Self {
Self::new(anyhow::anyhow!("{info}"))
}
pub fn from_debug(info: impl fmt::Debug) -> Self {
Self::new(anyhow::anyhow!("{info:?}"))
}
}
impl From<super::TelegramErrorKind> for Error {
fn from(err: super::TelegramErrorKind) -> Self {
Self::new(err)
}
}
impl From<super::SessionErrorKind> for Error {
fn from(err: super::SessionErrorKind) -> Self {
Self::new(err)
}
}
impl From<Infallible> for Error {
fn from(_: Infallible) -> Self {
unreachable!("Infallible error type should never be constructed")
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Self::new(err)
}
}
impl From<fmt::Error> for Error {
fn from(err: fmt::Error) -> Self {
Self::new(err)
}
}
impl From<ParseIntError> for Error {
fn from(err: ParseIntError) -> Self {
Self::new(err)
}
}
impl From<ParseFloatError> for Error {
fn from(err: 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)
}
}