sqlx-turso-driver 0.0.1

An asynchronous SQLx driver for embedded Turso databases
use sqlx_core::error::{DatabaseError, Error, ErrorKind};
use std::{error::Error as StdError, fmt};

pub(crate) fn unsupported(operation: &str) -> Error {
    Error::Protocol(format!("sqlx-turso: {operation} is not supported"))
}

/// Preserves the actual Turso error as its source; does not guess constraint kinds.
#[derive(Debug)]
pub struct TursoDatabaseError {
    source: turso::Error,
    message: String,
}
impl fmt::Display for TursoDatabaseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}
impl StdError for TursoDatabaseError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        Some(&self.source)
    }
}
impl DatabaseError for TursoDatabaseError {
    fn message(&self) -> &str {
        &self.message
    }
    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 {
        // Turso exposes Constraint(String), not a structured UNIQUE,
        // foreign-key, NOT NULL or CHECK subtype/code. Preserve the variant as
        // the source, but do not classify SQLx constraint kinds from its text.
        ErrorKind::Other
    }
}
pub(crate) fn engine_error(source: turso::Error) -> Error {
    TursoDatabaseError {
        message: source.to_string(),
        source,
    }
    .into()
}