prefix-register 0.2.2

A PostgreSQL-backed namespace prefix registry for CURIE expansion and prefix management
Documentation
// Copyright TELICENT LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Error types for the prefix registry service.

use std::fmt;

/// Result type alias for prefix registry operations.
pub type Result<T> = std::result::Result<T, Error>;

/// Configuration error types with specific variants.
#[derive(Debug, Clone, thiserror::Error)]
pub enum ConfigurationError {
    /// Max connections is invalid (must be greater than 0).
    #[error("max_connections must be greater than 0, got {0}")]
    InvalidMaxConnections(usize),

    /// Database URL is invalid or empty.
    #[error("database_url is invalid: {0}")]
    InvalidDatabaseUrl(String),
}

/// Error types for prefix registry operations.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Database operation failed.
    #[error("Database error: {0}")]
    Database(String),

    /// Database connection pool error.
    #[error("Connection pool error: {0}")]
    ConnectionPool(String),

    /// Invalid configuration.
    #[error("Configuration error: {0}")]
    Configuration(#[from] ConfigurationError),

    /// Prefix validation failed.
    #[error("Invalid prefix: {0}")]
    InvalidPrefix(String),

    /// URI validation failed.
    #[error("Invalid URI: {0}")]
    InvalidUri(String),
}

impl Error {
    /// Create a database error from any Display type.
    pub fn database(msg: impl fmt::Display) -> Self {
        Self::Database(msg.to_string())
    }

    /// Create a connection pool error.
    pub fn connection_pool(msg: impl fmt::Display) -> Self {
        Self::ConnectionPool(msg.to_string())
    }

    /// Create an invalid prefix error.
    pub fn invalid_prefix(msg: impl fmt::Display) -> Self {
        Self::InvalidPrefix(msg.to_string())
    }

    /// Create an invalid URI error.
    pub fn invalid_uri(msg: impl fmt::Display) -> Self {
        Self::InvalidUri(msg.to_string())
    }
}

impl From<tokio_postgres::Error> for Error {
    fn from(e: tokio_postgres::Error) -> Self {
        Self::Database(e.to_string())
    }
}

impl From<deadpool_postgres::PoolError> for Error {
    fn from(e: deadpool_postgres::PoolError) -> Self {
        Self::ConnectionPool(e.to_string())
    }
}

impl From<deadpool_postgres::BuildError> for Error {
    fn from(e: deadpool_postgres::BuildError) -> Self {
        Self::ConnectionPool(format!("Pool build error: {}", e))
    }
}

impl From<deadpool_postgres::CreatePoolError> for Error {
    fn from(e: deadpool_postgres::CreatePoolError) -> Self {
        Self::ConnectionPool(format!("Pool creation error: {}", e))
    }
}