Skip to main content

wasm4pm_types/
error.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Error types for pm4wasm operations
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6pub enum Error {
7    /// Configuration or input validation failed
8    ValidationError(String),
9
10    /// Event log parsing failed
11    ParseError(String),
12
13    /// Algorithm execution failed
14    ExecutionError(String),
15
16    /// Hash computation failed
17    HashError(String),
18
19    /// Provenance validation failed
20    ProvenanceError(String),
21
22    /// Budget constraint violated (latency, memory)
23    BudgetExceeded(String),
24
25    /// Internal state error
26    StateError(String),
27
28    /// Resource not found
29    NotFound(String),
30
31    /// Serialization/deserialization error
32    SerializationError(String),
33
34    /// Unknown error
35    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
57/// Result type for pm4wasm operations
58pub 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}