rust_prelude_plus/
error.rs

1//! Error types and handling for keypath operations
2
3use thiserror::Error;
4
5/// Errors that can occur during keypath operations
6#[derive(Error, Debug, Clone, PartialEq)]
7pub enum KeyPathError {
8    /// Keypath access failed due to invalid path
9    #[error("Invalid keypath access: {message}")]
10    InvalidAccess { message: String },
11    
12    /// Type mismatch during keypath operation
13    #[error("Type mismatch: expected {expected}, found {found}")]
14    TypeMismatch { expected: String, found: String },
15    
16    /// Runtime failure during keypath operation
17    #[error("Runtime failure: {message}")]
18    RuntimeFailure { message: String },
19    
20    /// Collection operation failed
21    #[error("Collection operation failed: {message}")]
22    CollectionError { message: String },
23    
24    /// Async operation failed
25    #[error("Async operation failed: {message}")]
26    AsyncError { message: String },
27    
28    /// Parallel operation failed
29    #[error("Parallel operation failed: {message}")]
30    ParallelError { message: String },
31    
32    /// Network operation failed
33    #[error("Network operation failed: {message}")]
34    NetworkError { message: String },
35    
36    /// Serialization/deserialization failed
37    #[error("Serialization error: {message}")]
38    SerializationError { message: String },
39}
40
41/// Result type for keypath operations
42pub type KeyPathResult<T> = Result<T, KeyPathError>;
43
44/// Extension trait for converting standard errors to KeyPathError
45pub trait IntoKeyPathError {
46    fn into_keypath_error(self) -> KeyPathError;
47}
48
49impl<E: std::error::Error> IntoKeyPathError for E {
50    fn into_keypath_error(self) -> KeyPathError {
51        KeyPathError::RuntimeFailure {
52            message: self.to_string(),
53        }
54    }
55}
56
57/// Helper macros for error creation
58#[macro_export]
59macro_rules! keypath_error {
60    ($variant:ident, $($field:ident: $value:expr),*) => {
61        KeyPathError::$variant {
62            $($field: $value),*
63        }
64    };
65}
66
67#[macro_export]
68macro_rules! keypath_result {
69    ($expr:expr) => {
70        $expr.map_err(|e| e.into_keypath_error())
71    };
72}
73
74/// Validation utilities for keypath operations
75pub mod validation {
76    use super::*;
77    
78    /// Validate that a keypath operation is safe to perform
79    pub fn validate_keypath_access<T>(_data: &T) -> KeyPathResult<()> {
80        // This is a placeholder for more sophisticated validation
81        // In a real implementation, this might check for null pointers,
82        // bounds checking, etc.
83        Ok(())
84    }
85    
86    /// Validate that a collection operation is safe to perform
87    pub fn validate_collection_operation<T>(collection: &[T]) -> KeyPathResult<()> {
88        if collection.is_empty() {
89            return Err(KeyPathError::CollectionError {
90                message: "Collection is empty".to_string(),
91            });
92        }
93        Ok(())
94    }
95}