nusadb 0.1.0

Fast, stable native Rust driver and ORM for NusaDB (Nusa Wire Protocol 1.1).
Documentation
//! The driver's error type. Every fallible API returns [`Result<T, Error>`].

use std::fmt;

/// A `nusadb` driver error.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// A network / socket I/O failure.
    Io(std::io::Error),
    /// The server returned an `Error` frame: a 5-char SQLSTATE plus a human-readable message.
    Server {
        /// The 5-character SQLSTATE code (§14). The 1.0 server emits `XX000` for most errors.
        code: String,
        /// The human-readable message.
        message: String,
    },
    /// The protocol was violated by the peer (bad magic, malformed frame, unexpected message, an
    /// over-large frame, invalid UTF-8, a truncated payload, …). Carries a description.
    Protocol(String),
    /// Authentication failed (wrong password, missing user, mechanism mismatch, or a forged server
    /// signature). Deliberately coarse to avoid leaking which check failed.
    Auth(String),
    /// The connection-string / config could not be parsed.
    Config(String),
    /// A value could not be converted to or from the requested Rust type.
    Conversion(String),
    /// The connection pool is exhausted or has been closed.
    Pool(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(e) => write!(f, "io error: {e}"),
            Self::Server { code, message } => write!(f, "server error [{code}]: {message}"),
            Self::Protocol(m) => write!(f, "protocol error: {m}"),
            Self::Auth(m) => write!(f, "authentication error: {m}"),
            Self::Config(m) => write!(f, "config error: {m}"),
            Self::Conversion(m) => write!(f, "conversion error: {m}"),
            Self::Pool(m) => write!(f, "pool error: {m}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(e) => Some(e),
            _ => None,
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Self::Io(e)
    }
}

/// The crate-wide result alias.
pub type Result<T> = std::result::Result<T, Error>;