use std::{convert::Infallible, str::Utf8Error, string::FromUtf8Error};

use mailparse::MailParseError;
#[cfg(feature = "tantivy")]
use tantivy::{query::QueryParserError, TantivyError};
use thiserror::Error;

use crate::TransformError;

#[derive(Error, Debug)]
pub enum ServerError {
    #[error("notmuch: {0}")]
    NotmuchError(#[from] letterbox_notmuch::NotmuchError),
    #[error("flatten")]
    FlattenError,
    #[error("mail parse error: {0}")]
    MailParseError(#[from] MailParseError),
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),
    #[error("attachement not found")]
    PartNotFound,
    #[error("sqlx error: {0}")]
    SQLXError(#[from] sqlx::Error),
    #[error("html transform error: {0}")]
    TransformError(#[from] TransformError),
    #[error("UTF8 error: {0}")]
    Utf8Error(#[from] Utf8Error),
    #[error("FromUTF8 error: {0}")]
    FromUtf8Error(#[from] FromUtf8Error),
    #[error("error: {0}")]
    StringError(String),
    #[error("invalid url: {0}")]
    UrlParseError(#[from] url::ParseError),
    #[cfg(feature = "tantivy")]
    #[error("tantivy error: {0}")]
    TantivyError(#[from] TantivyError),
    #[cfg(feature = "tantivy")]
    #[error("tantivy query parse error: {0}")]
    QueryParseError(#[from] QueryParserError),
    #[error("impossible: {0}")]
    InfaillibleError(#[from] Infallible),
    #[error("askama error: {0}")]
    AskamaError(#[from] askama::Error),
    #[error("xml error: {0}")]
    XmlError(#[from] quick_xml::Error),
    #[error("xml encoding error: {0}")]
    XmlEncodingError(#[from] quick_xml::encoding::EncodingError),
    #[error("html to text error: {0}")]
    Html2TextError(#[from] html2text::Error),
}
