wme-models 0.1.3

Type definitions for the Wikimedia Enterprise API
Documentation
//! Error types for model operations.
//!
//! This module provides error types for operations in the `wme-models` crate.
//! These errors cover schema validation, serialization/deserialization failures,
//! and identifier parsing errors.
//!
//! # Error Handling
//!
//! All errors implement `std::error::Error` and can be used with the `?` operator.
//! Errors are cloneable to support patterns where errors need to be stored or shared.
//!
//! # Example
//!
//! ```
//! use wme_models::SnapshotIdentifier;
//! use std::str::FromStr;
//!
//! match SnapshotIdentifier::from_str("invalid") {
//!     Ok(id) => println!("Parsed: {}", id),
//!     Err(e) => println!("Error: {}", e),
//! }
//! ```

use thiserror::Error;

/// Errors that can occur when working with models.
///
/// This enum covers all error cases for model operations including:
/// - Invalid snapshot identifier parsing
/// - Unsupported schema versions
/// - Serialization/deserialization failures
#[derive(Error, Debug, Clone, PartialEq)]
pub enum ModelError {
    /// Invalid snapshot identifier format.
    ///
    /// The snapshot identifier could not be parsed. Valid formats are:
    /// `{language}{project}_namespace_{number}` (e.g., "enwiki_namespace_0")
    #[error("invalid snapshot identifier: {0}")]
    InvalidSnapshotId(String),

    /// Unsupported schema version.
    ///
    /// The schema version in an envelope is not supported by this library.
    /// See [`crate::envelope::SUPPORTED_SCHEMA_VERSIONS`] for supported versions.
    #[error("unsupported schema version: {0}")]
    UnsupportedSchemaVersion(String),

    /// Serialization error.
    ///
    /// Failed to serialize a model to JSON.
    #[error("serialization error: {0}")]
    SerializationError(String),

    /// Deserialization error.
    ///
    /// Failed to deserialize JSON to a model.
    #[error("deserialization error: {0}")]
    DeserializationError(String),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_invalid_snapshot_id_error() {
        let err = ModelError::InvalidSnapshotId("bad_format".to_string());
        assert!(err.to_string().contains("bad_format"));
    }

    #[test]
    fn test_unsupported_schema_version_error() {
        let err = ModelError::UnsupportedSchemaVersion("1999.1".to_string());
        assert!(err.to_string().contains("1999.1"));
    }

    #[test]
    fn test_serialization_error() {
        let err = ModelError::SerializationError("json error".to_string());
        assert!(err.to_string().contains("json error"));
    }

    #[test]
    fn test_deserialization_error() {
        let err = ModelError::DeserializationError("invalid json".to_string());
        assert!(err.to_string().contains("invalid json"));
    }

    #[test]
    fn test_error_clone() {
        let err = ModelError::InvalidSnapshotId("test".to_string());
        let cloned = err.clone();
        assert_eq!(err, cloned);
    }
}