sqlx-firebird 0.1.0-beta.1

sqlx firebird driver
Documentation
//
// Copyright © 2023, RedSoft
// License: MIT
//

use std::borrow::Cow;
use std::error::Error as StdError;
use std::fmt::{self, Debug, Display, Formatter};

use sqlx_core::error::*;

pub struct FbError(rsfbclient::FbError);

impl Debug for FbError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

impl Display for FbError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl StdError for FbError {}

impl DatabaseError for FbError {
    #[inline]
    fn message(&self) -> &str {
        use rsfbclient::FbError as Error;
        match &self.0 {
            Error::Sql { msg, .. } => &msg,
            Error::Io(_) => "io error",
            Error::Other(msg) => &msg,
        }
    }

    #[inline]
    fn code(&self) -> Option<Cow<'_, str>> {
        use rsfbclient::FbError as Error;
        let code = match &self.0 {
            Error::Sql { code, .. } => *code,
            Error::Io(e) => e.raw_os_error().unwrap_or(-1),
            Error::Other(_) => -1,
        };
        Some(format!("{}", code).into())
    }

    fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
        self
    }

    fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {
        self
    }

    fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static> {
        self
    }

    fn kind(&self) -> ErrorKind {
        ErrorKind::Other
    }
}

impl From<rsfbclient::FbError> for FbError {
    fn from(error: rsfbclient::FbError) -> Self {
        Self(error)
    }
}