1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6pub enum Error {
7 ValidationError(String),
9
10 ParseError(String),
12
13 ExecutionError(String),
15
16 HashError(String),
18
19 ProvenanceError(String),
21
22 BudgetExceeded(String),
24
25 StateError(String),
27
28 NotFound(String),
30
31 SerializationError(String),
33
34 Unknown(String),
36}
37
38impl fmt::Display for Error {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 match self {
41 Error::ValidationError(msg) => write!(f, "Validation error: {}", msg),
42 Error::ParseError(msg) => write!(f, "Parse error: {}", msg),
43 Error::ExecutionError(msg) => write!(f, "Execution error: {}", msg),
44 Error::HashError(msg) => write!(f, "Hash error: {}", msg),
45 Error::ProvenanceError(msg) => write!(f, "Provenance error: {}", msg),
46 Error::BudgetExceeded(msg) => write!(f, "Budget exceeded: {}", msg),
47 Error::StateError(msg) => write!(f, "State error: {}", msg),
48 Error::NotFound(msg) => write!(f, "Not found: {}", msg),
49 Error::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
50 Error::Unknown(msg) => write!(f, "Unknown error: {}", msg),
51 }
52 }
53}
54
55impl std::error::Error for Error {}
56
57pub type Result<T> = std::result::Result<T, Error>;
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_error_display() {
66 let err = Error::ValidationError("test".to_string());
67 assert_eq!(err.to_string(), "Validation error: test");
68 }
69
70 #[test]
71 fn test_error_serialization() {
72 let err = Error::ExecutionError("algorithm failed".to_string());
73 let json = serde_json::to_string(&err).unwrap();
74 assert!(json.contains("ExecutionError"));
75 assert!(json.contains("algorithm failed"));
76 }
77}