wme_models/error.rs
1//! Error types for model operations.
2//!
3//! This module provides error types for operations in the `wme-models` crate.
4//! These errors cover schema validation, serialization/deserialization failures,
5//! and identifier parsing errors.
6//!
7//! # Error Handling
8//!
9//! All errors implement `std::error::Error` and can be used with the `?` operator.
10//! Errors are cloneable to support patterns where errors need to be stored or shared.
11//!
12//! # Example
13//!
14//! ```
15//! use wme_models::SnapshotIdentifier;
16//! use std::str::FromStr;
17//!
18//! match SnapshotIdentifier::from_str("invalid") {
19//! Ok(id) => println!("Parsed: {}", id),
20//! Err(e) => println!("Error: {}", e),
21//! }
22//! ```
23
24use thiserror::Error;
25
26/// Errors that can occur when working with models.
27///
28/// This enum covers all error cases for model operations including:
29/// - Invalid snapshot identifier parsing
30/// - Unsupported schema versions
31/// - Serialization/deserialization failures
32#[derive(Error, Debug, Clone, PartialEq)]
33pub enum ModelError {
34 /// Invalid snapshot identifier format.
35 ///
36 /// The snapshot identifier could not be parsed. Valid formats are:
37 /// `{language}{project}_namespace_{number}` (e.g., "enwiki_namespace_0")
38 #[error("invalid snapshot identifier: {0}")]
39 InvalidSnapshotId(String),
40
41 /// Unsupported schema version.
42 ///
43 /// The schema version in an envelope is not supported by this library.
44 /// See [`crate::envelope::SUPPORTED_SCHEMA_VERSIONS`] for supported versions.
45 #[error("unsupported schema version: {0}")]
46 UnsupportedSchemaVersion(String),
47
48 /// Serialization error.
49 ///
50 /// Failed to serialize a model to JSON.
51 #[error("serialization error: {0}")]
52 SerializationError(String),
53
54 /// Deserialization error.
55 ///
56 /// Failed to deserialize JSON to a model.
57 #[error("deserialization error: {0}")]
58 DeserializationError(String),
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_invalid_snapshot_id_error() {
67 let err = ModelError::InvalidSnapshotId("bad_format".to_string());
68 assert!(err.to_string().contains("bad_format"));
69 }
70
71 #[test]
72 fn test_unsupported_schema_version_error() {
73 let err = ModelError::UnsupportedSchemaVersion("1999.1".to_string());
74 assert!(err.to_string().contains("1999.1"));
75 }
76
77 #[test]
78 fn test_serialization_error() {
79 let err = ModelError::SerializationError("json error".to_string());
80 assert!(err.to_string().contains("json error"));
81 }
82
83 #[test]
84 fn test_deserialization_error() {
85 let err = ModelError::DeserializationError("invalid json".to_string());
86 assert!(err.to_string().contains("invalid json"));
87 }
88
89 #[test]
90 fn test_error_clone() {
91 let err = ModelError::InvalidSnapshotId("test".to_string());
92 let cloned = err.clone();
93 assert_eq!(err, cloned);
94 }
95}