uri-register 0.3.0

A high-performance PostgreSQL-backed URI dictionary service for assigning unique integer IDs to URIs
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 URI register service

use std::fmt;

/// Result type alias for URI register operations
pub type Result<T> = std::result::Result<T, Error>;

/// Configuration error types with specific variants
#[derive(Debug, Clone, thiserror::Error)]
pub enum ConfigurationError {
    /// Cache size is invalid (must be greater than 0)
    #[error("cache_size must be greater than 0, got {0}")]
    InvalidCacheSize(usize),

    /// Max connections is invalid (must be greater than 0)
    #[error("max_connections must be greater than 0, got {0}")]
    InvalidMaxConnections(u32),

    /// Table name is invalid (must be a valid SQL identifier)
    #[error("table_name is invalid: {0}")]
    InvalidTableName(String),

    /// Backoff configuration is invalid
    #[error("backoff configuration is invalid: {0}")]
    InvalidBackoff(String),
}

/// Error types for URI register operations
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Database operation failed (error message is sanitized to remove passwords)
    #[error("Database error: {0}")]
    Database(String),

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

    /// Cache operation failed
    #[error("Cache error: {0}")]
    Cache(String),

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

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

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

    /// Create a cache error
    pub fn cache(msg: impl fmt::Display) -> Self {
        Self::Cache(msg.to_string())
    }

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