use std::{borrow::Cow, error::Error, fmt::Display};
use serde::Deserialize;
use sqlx_core::error::{self, ErrorKind};
#[derive(Debug, Deserialize)]
pub struct ExaDatabaseError {
    text: String,
    #[serde(rename = "sqlCode")]
    code: String,
}
impl Display for ExaDatabaseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Exasol error {}: {}", self.code, self.text)
    }
}
impl Error for ExaDatabaseError {}
impl Error for &mut ExaDatabaseError {}
impl error::DatabaseError for ExaDatabaseError {
    fn message(&self) -> &str {
        &self.text
    }
    fn code(&self) -> Option<Cow<'_, str>> {
        Some(Cow::Borrowed(&self.code))
    }
    fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {
        self
    }
    fn as_error_mut(&mut self) -> &mut (dyn Error + Send + Sync + 'static) {
        self
    }
    fn into_error(self: Box<Self>) -> Box<dyn Error + Send + Sync + 'static> {
        self
    }
    fn kind(&self) -> ErrorKind {
        match self.code.as_str() {
            "27001" => ErrorKind::NotNullViolation,
            "42X91" if self.text.contains("primary key") => ErrorKind::UniqueViolation,
            "42X91" if self.text.contains("foreign key") => ErrorKind::ForeignKeyViolation,
            _ => ErrorKind::Other,
        }
    }
}