evidentsource-client 1.0.0-rc1

Rust client for the EvidentSource event sourcing platform
Documentation
//! Conversion error types.

use evidentsource_core::domain::{ConstraintError, IdentifierError};
use thiserror::Error;

/// Errors that can occur during proto-to-domain type conversions.
#[derive(Error, Debug, Clone)]
pub enum ConversionError {
    /// A required field was missing in the proto message.
    #[error("missing required field '{field}' in {message_type}")]
    MissingField {
        message_type: &'static str,
        field: &'static str,
    },

    /// A oneof field had no variant set.
    #[error("missing oneof variant '{oneof_name}' in {message_type}")]
    MissingOneof {
        message_type: &'static str,
        oneof_name: &'static str,
    },

    /// An identifier failed validation.
    #[error("invalid identifier: {0}")]
    InvalidIdentifier(#[from] IdentifierError),

    /// A constraint was invalid.
    #[error("invalid constraint: {0}")]
    InvalidConstraint(#[from] ConstraintError),

    /// A nested conversion failed.
    #[error("nested conversion error in {context}: {source}")]
    Nested {
        context: &'static str,
        #[source]
        source: Box<ConversionError>,
    },

    /// An enum variant was unrecognized.
    #[error("unknown enum variant {value} for {enum_name}")]
    UnknownEnumVariant { enum_name: &'static str, value: i32 },

    /// Invalid range (min > max).
    #[error("invalid range: min ({min}) > max ({max})")]
    InvalidRange { min: u64, max: u64 },

    /// Invalid timestamp.
    #[error("invalid timestamp")]
    InvalidTimestamp,
}

impl ConversionError {
    /// Create a MissingField error.
    pub fn missing_field(message_type: &'static str, field: &'static str) -> Self {
        ConversionError::MissingField {
            message_type,
            field,
        }
    }

    /// Create a MissingOneof error.
    pub fn missing_oneof(message_type: &'static str, oneof_name: &'static str) -> Self {
        ConversionError::MissingOneof {
            message_type,
            oneof_name,
        }
    }

    /// Create a Nested error.
    pub fn nested(context: &'static str, source: ConversionError) -> Self {
        ConversionError::Nested {
            context,
            source: Box::new(source),
        }
    }
}