walletkit-db 0.20.0

Encrypted on-device storage primitives for WalletKit (SQLCipher wrapper, vault, content-addressed blobs, key envelope).
//! Error types for the safe `SQLite` wrapper.

use std::fmt;

/// Error code returned by `SQLite` operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ErrorCode(pub i32);

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

/// Error returned by database operations.
#[derive(Debug, PartialEq, Eq)]
pub struct Error {
    /// `SQLite` result code.
    pub code: ErrorCode,
    /// Human-readable error message (from `sqlite3_errmsg` when available).
    pub message: String,
}

impl Error {
    /// Creates a new database error.
    pub(crate) fn new(code: i32, message: impl Into<String>) -> Self {
        Self {
            code: ErrorCode(code),
            message: message.into(),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "sqlite error {}: {}", self.code, self.message)
    }
}

impl std::error::Error for Error {}

/// Result alias for [`Error`].
pub type DbResult<T> = std::result::Result<T, Error>;