rust_prelude_plus/
error.rs1use thiserror::Error;
4
5#[derive(Error, Debug, Clone, PartialEq)]
7pub enum KeyPathError {
8 #[error("Invalid keypath access: {message}")]
10 InvalidAccess { message: String },
11
12 #[error("Type mismatch: expected {expected}, found {found}")]
14 TypeMismatch { expected: String, found: String },
15
16 #[error("Runtime failure: {message}")]
18 RuntimeFailure { message: String },
19
20 #[error("Collection operation failed: {message}")]
22 CollectionError { message: String },
23
24 #[error("Async operation failed: {message}")]
26 AsyncError { message: String },
27
28 #[error("Parallel operation failed: {message}")]
30 ParallelError { message: String },
31
32 #[error("Network operation failed: {message}")]
34 NetworkError { message: String },
35
36 #[error("Serialization error: {message}")]
38 SerializationError { message: String },
39}
40
41pub type KeyPathResult<T> = Result<T, KeyPathError>;
43
44pub 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#[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
74pub mod validation {
76 use super::*;
77
78 pub fn validate_keypath_access<T>(_data: &T) -> KeyPathResult<()> {
80 Ok(())
84 }
85
86 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}