systemprompt-database 0.6.0

PostgreSQL infrastructure for systemprompt.io AI governance. SQLx-backed pool, generic repository traits, and compile-time query verification. Part of the systemprompt.io AI governance pipeline.
//! Typed error boundary for the database crate.
//!
//! `RepositoryError` is the canonical error returned from every public
//! signature in this crate, including the dyn-safe `DatabaseProvider` /
//! `DatabaseTransaction` trait surfaces. It composes `sqlx::Error` and
//! `serde_json::Error` via `#[from]`; runtime invariant failures are
//! routed through `RepositoryError::InvalidState`.

use thiserror::Error;

#[derive(Debug, Error)]
pub enum RepositoryError {
    #[error("Entity not found: {0}")]
    NotFound(String),

    #[error("Constraint violation: {0}")]
    Constraint(String),

    #[error("Database error: {0}")]
    Database(#[from] sqlx::Error),

    #[error("Serialization error: {0}")]
    Serialization(#[from] serde_json::Error),

    #[error("Invalid argument: {0}")]
    InvalidArgument(String),

    #[error("Invalid state: {0}")]
    InvalidState(String),

    #[error("Internal error: {0}")]
    Internal(String),
}

pub type DatabaseResult<T> = Result<T, RepositoryError>;

impl RepositoryError {
    pub fn not_found<T: std::fmt::Display>(id: T) -> Self {
        Self::NotFound(id.to_string())
    }

    pub fn constraint<T: Into<String>>(message: T) -> Self {
        Self::Constraint(message.into())
    }

    pub fn invalid_argument<T: Into<String>>(message: T) -> Self {
        Self::InvalidArgument(message.into())
    }

    pub fn internal<T: Into<String>>(message: T) -> Self {
        Self::Internal(message.into())
    }

    pub fn invalid_state<T: Into<String>>(message: T) -> Self {
        Self::InvalidState(message.into())
    }

    #[must_use]
    pub const fn is_not_found(&self) -> bool {
        matches!(self, Self::NotFound(_))
    }

    #[must_use]
    pub const fn is_constraint(&self) -> bool {
        matches!(self, Self::Constraint(_))
    }
}

impl From<RepositoryError> for systemprompt_traits::RepositoryError {
    fn from(err: RepositoryError) -> Self {
        Self::Database(Box::new(err))
    }
}