rvf 0.1.0

Rust implementation of the ValueFlows vocabulary for distributed economic networks
Documentation
//! Error types for the ValueFlows implementation

use thiserror::Error;

/// Result type alias using the library's error type
pub type Result<T> = std::result::Result<T, Error>;

/// Error types for ValueFlows operations
#[derive(Error, Debug, Clone)]
pub enum Error {
    /// A required field is missing
    #[error("Missing required field: {0}")]
    MissingField(String),

    /// An invalid value was provided
    #[error("Invalid value for {field}: {message}")]
    InvalidValue { field: String, message: String },

    /// An entity was not found
    #[error("{entity_type} not found: {id}")]
    NotFound { entity_type: String, id: String },

    /// A duplicate entity already exists
    #[error("{entity_type} already exists: {id}")]
    Duplicate { entity_type: String, id: String },

    /// An invalid action for the given context
    #[error("Invalid action {action} for {context}")]
    InvalidAction { action: String, context: String },

    /// An invalid state transition
    #[error("Invalid state transition from {from} to {to}")]
    InvalidStateTransition { from: String, to: String },

    /// Insufficient resource quantity
    #[error("Insufficient quantity: required {required}, available {available}")]
    InsufficientQuantity { required: String, available: String },

    /// Unit mismatch in quantity operations
    #[error("Unit mismatch: cannot operate on {unit1} and {unit2}")]
    UnitMismatch { unit1: String, unit2: String },

    /// Validation error
    #[error("Validation error: {0}")]
    Validation(String),

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

    /// Serialization/deserialization error
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// Generic error for custom use cases
    #[error("{0}")]
    Custom(String),
}

impl Error {
    /// Create a missing field error
    pub fn missing_field(field: impl Into<String>) -> Self {
        Error::MissingField(field.into())
    }

    /// Create an invalid value error
    pub fn invalid_value(field: impl Into<String>, message: impl Into<String>) -> Self {
        Error::InvalidValue {
            field: field.into(),
            message: message.into(),
        }
    }

    /// Create a not found error
    pub fn not_found(entity_type: impl Into<String>, id: impl Into<String>) -> Self {
        Error::NotFound {
            entity_type: entity_type.into(),
            id: id.into(),
        }
    }

    /// Create a duplicate error
    pub fn duplicate(entity_type: impl Into<String>, id: impl Into<String>) -> Self {
        Error::Duplicate {
            entity_type: entity_type.into(),
            id: id.into(),
        }
    }

    /// Create an invalid action error
    pub fn invalid_action(action: impl Into<String>, context: impl Into<String>) -> Self {
        Error::InvalidAction {
            action: action.into(),
            context: context.into(),
        }
    }

    /// Create a validation error
    pub fn validation(message: impl Into<String>) -> Self {
        Error::Validation(message.into())
    }

    /// Create a storage error
    pub fn storage(message: impl Into<String>) -> Self {
        Error::Storage(message.into())
    }

    /// Create a custom error
    pub fn custom(message: impl Into<String>) -> Self {
        Error::Custom(message.into())
    }
}

#[cfg(feature = "serde")]
impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Self {
        Error::Serialization(err.to_string())
    }
}