footing 0.6.1

Feature-gated foundations for Rust backend services
Documentation
//! Diagnostics descriptor and registry validation errors.

use thiserror::Error;

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

/// Registry metadata validation errors.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum ResourceRegistryError {
    #[error(
        "resource diagnostic service name must be non-empty and contain only ASCII letters, digits, '-', '_', '.', or ':'"
    )]
    /// The service name is invalid.
    InvalidServiceName,
    #[error("resource diagnostic service name must be less than or equal to {max} bytes")]
    /// The service name exceeds the supported length.
    ServiceNameTooLong {
        #[doc = "The configured upper bound."]
        max: usize,
    },
    #[error(
        "resource diagnostic probe timeout must be greater than zero and fit the runtime timer range"
    )]
    /// The per-probe timeout is invalid.
    InvalidProbeTimeout,
    #[error(
        "resource diagnostics run timeout must be greater than zero and fit the runtime timer range"
    )]
    /// The overall run timeout is invalid.
    InvalidRunTimeout,
    #[error("resource diagnostic probe concurrency must be greater than zero")]
    /// The maximum number of concurrent probes is zero.
    InvalidMaxConcurrency,
    #[error("resource diagnostics report must contain at most {max} resources")]
    /// The registry or report contains too many resources.
    TooManyResources {
        #[doc = "The configured upper bound."]
        max: usize,
    },
    #[error("duplicate resource diagnostic id {id} at indexes {first_index} and {second_index}")]
    /// A duplicate resource identifier was encountered.
    DuplicateResourceId {
        /// The identifier value.
        id: String,
        /// The first index value.
        first_index: usize,
        /// The second index value.
        second_index: usize,
    },
}

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

    fn error_code(&self) -> crate::result::ErrorCode {
        crate::result::ErrorCode::internal(match self {
            Self::InvalidServiceName => "resource_diagnostics_invalid_service_name",
            Self::ServiceNameTooLong { .. } => "resource_diagnostics_service_name_too_long",
            Self::InvalidProbeTimeout => "resource_diagnostics_invalid_probe_timeout",
            Self::InvalidRunTimeout => "resource_diagnostics_invalid_run_timeout",
            Self::InvalidMaxConcurrency => "resource_diagnostics_invalid_max_concurrency",
            Self::TooManyResources { .. } => "resource_diagnostics_too_many_resources",
            Self::DuplicateResourceId { .. } => "resource_diagnostics_duplicate_resource_id",
        })
    }
}

/// Resource descriptor validation errors.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum ResourceDescriptorError {
    #[error("resource diagnostic id must not be empty")]
    /// The identifier is empty.
    EmptyId,
    #[error("resource diagnostic id must be less than or equal to {max} bytes")]
    /// The identifier exceeds the supported length.
    IdTooLong {
        #[doc = "The configured upper bound."]
        max: usize,
    },
    #[error(
        "resource diagnostic id must contain only ASCII letters, digits, '-', '_', '.', or ':'"
    )]
    /// The identifier is invalid.
    InvalidId,
    #[error("resource diagnostic name must not be empty")]
    /// The name is empty.
    EmptyName,
    #[error("resource diagnostic name must be less than or equal to {max} bytes")]
    /// The name exceeds the supported length.
    NameTooLong {
        #[doc = "The configured upper bound."]
        max: usize,
    },
    #[error("resource diagnostic name must not contain control characters")]
    /// The name is invalid.
    InvalidName,
    #[error("resource diagnostic label key must not be empty")]
    /// The label key is empty.
    EmptyLabelKey,
    #[error("resource diagnostic label key must be less than or equal to {max} bytes")]
    /// The label key exceeds the supported length.
    LabelKeyTooLong {
        #[doc = "The configured upper bound."]
        max: usize,
    },
    #[error(
        "resource diagnostic label key must contain only ASCII letters, digits, '-', '_', '.', or ':'"
    )]
    /// The label key is invalid.
    InvalidLabelKey,
    #[error("resource diagnostic label {key} value must not be empty")]
    /// The label value is empty.
    EmptyLabelValue {
        #[doc = "The associated key."]
        key: String,
    },
    #[error("resource diagnostic label {key} value must be less than or equal to {max} bytes")]
    /// The label value exceeds the supported length.
    LabelValueTooLong {
        #[doc = "The associated key."]
        key: String,
        #[doc = "The configured upper bound."]
        max: usize,
    },
    #[error("resource diagnostic label {key} value must not contain control characters")]
    /// The label value is invalid.
    InvalidLabelValue {
        #[doc = "The associated key."]
        key: String,
    },
    #[error("resource descriptor must contain at most {max} labels")]
    /// The descriptor contains too many labels.
    TooManyLabels {
        #[doc = "The configured upper bound."]
        max: usize,
    },
}

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

    fn error_code(&self) -> crate::result::ErrorCode {
        crate::result::ErrorCode::internal(match self {
            Self::EmptyId => "resource_descriptor_empty_id",
            Self::IdTooLong { .. } => "resource_descriptor_id_too_long",
            Self::InvalidId => "resource_descriptor_invalid_id",
            Self::EmptyName => "resource_descriptor_empty_name",
            Self::NameTooLong { .. } => "resource_descriptor_name_too_long",
            Self::InvalidName => "resource_descriptor_invalid_name",
            Self::EmptyLabelKey => "resource_descriptor_empty_label_key",
            Self::LabelKeyTooLong { .. } => "resource_descriptor_label_key_too_long",
            Self::InvalidLabelKey => "resource_descriptor_invalid_label_key",
            Self::EmptyLabelValue { .. } => "resource_descriptor_empty_label_value",
            Self::LabelValueTooLong { .. } => "resource_descriptor_label_value_too_long",
            Self::InvalidLabelValue { .. } => "resource_descriptor_invalid_label_value",
            Self::TooManyLabels { .. } => "resource_descriptor_too_many_labels",
        })
    }
}

/// Resource diagnostic validation errors.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum ResourceDiagnosticError {
    #[error("resource diagnostic must contain at most {max} details")]
    /// The diagnostic contains too many details.
    TooManyDetails {
        #[doc = "The configured upper bound."]
        max: usize,
    },
    #[error("resource diagnostic must contain at most {max} remediations")]
    /// The diagnostic contains too many remediation hints.
    TooManyRemediations {
        #[doc = "The configured upper bound."]
        max: usize,
    },
}

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

    fn error_code(&self) -> crate::result::ErrorCode {
        crate::result::ErrorCode::internal(match self {
            Self::TooManyDetails { .. } => "resource_diagnostic_too_many_details",
            Self::TooManyRemediations { .. } => "resource_diagnostic_too_many_remediations",
        })
    }
}