footing 0.1.12

Feature-gated foundations for Rust backend services
Documentation
use thiserror::Error;

use super::EndpointMethod;
use crate::result::{ClassifyError, ErrorKind};

#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
/// Errors that can occur while handling endpoint contract validation.
pub enum EndpointContractValidationError {
    #[error("endpoint contract at index {index} has an empty path")]
    /// The path is empty.
    EmptyPath {
        #[doc = "The zero-based item index."]
        index: usize,
    },
    #[error("endpoint contract at index {index} path must start with '/'")]
    /// Indicates relative path.
    RelativePath {
        #[doc = "The zero-based item index."]
        index: usize,
        #[doc = "The affected path."]
        path: &'static str,
    },
    #[error("endpoint contract at index {index} has an invalid path: {path:?}")]
    /// The path is invalid.
    InvalidPath {
        #[doc = "The zero-based item index."]
        index: usize,
        #[doc = "The affected path."]
        path: &'static str,
    },
    #[error("endpoint contract at index {index} has an invalid template segment")]
    /// The template is invalid.
    InvalidTemplate {
        #[doc = "The zero-based item index."]
        index: usize,
        #[doc = "The affected path."]
        path: &'static str,
    },
    #[error("endpoint contract at index {index} has an invalid custom rate limit label: {value:?}")]
    /// The rate limit label is invalid.
    InvalidRateLimitLabel {
        #[doc = "The zero-based item index."]
        index: usize,
        #[doc = "The offending value."]
        value: &'static str,
    },
    #[error("endpoint contract at index {index} has empty {mode} permissions")]
    /// The permissions is empty.
    EmptyPermissions {
        #[doc = "The zero-based item index."]
        index: usize,
        #[doc = "The configured mode."]
        mode: &'static str,
    },
    #[error("public endpoint contract at index {index} must not require permissions")]
    /// Indicates public endpoint requires permissions.
    PublicEndpointRequiresPermissions {
        #[doc = "The zero-based item index."]
        index: usize,
    },
    #[error("private endpoint contract at index {index} must require permissions")]
    /// Indicates private endpoint without permissions.
    PrivateEndpointWithoutPermissions {
        #[doc = "The zero-based item index."]
        index: usize,
    },
    #[error(
        "duplicate endpoint contract {method} {path} at indexes {first_index} and {second_index}"
    )]
    /// A duplicate route was encountered.
    DuplicateRoute {
        /// The first index value.
        first_index: usize,
        /// The second index value.
        second_index: usize,
        /// The method value.
        method: EndpointMethod,
        /// The affected path.
        path: &'static str,
    },
}

impl ClassifyError for EndpointContractValidationError {
    fn error_kind(&self) -> ErrorKind {
        ErrorKind::Validation
    }

    fn error_code(&self) -> crate::result::ErrorCode {
        crate::result::ErrorCode::internal(match self {
            Self::EmptyPath { .. } => "endpoint_contract_empty_path",
            Self::RelativePath { .. } => "endpoint_contract_relative_path",
            Self::InvalidPath { .. } => "endpoint_contract_invalid_path",
            Self::InvalidTemplate { .. } => "endpoint_contract_invalid_template",
            Self::InvalidRateLimitLabel { .. } => "endpoint_contract_invalid_rate_limit_label",
            Self::EmptyPermissions { .. } => "endpoint_contract_empty_permissions",
            Self::PublicEndpointRequiresPermissions { .. } => {
                "endpoint_contract_public_requires_permissions"
            }
            Self::PrivateEndpointWithoutPermissions { .. } => {
                "endpoint_contract_private_without_permissions"
            }
            Self::DuplicateRoute { .. } => "endpoint_contract_duplicate_route",
        })
    }
}