Skip to main content

apr_qa_gen/
error.rs

1//! Error types for apr-qa-gen
2
3use thiserror::Error;
4
5/// Result type alias for apr-qa-gen operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during scenario generation
9#[derive(Debug, Error)]
10pub enum Error {
11    /// Model not found in registry
12    #[error("Model not found: {0}")]
13    ModelNotFound(String),
14
15    /// Invalid scenario configuration
16    #[error("Invalid scenario: {0}")]
17    InvalidScenario(String),
18
19    /// Oracle evaluation failed
20    #[error("Oracle error: {0}")]
21    OracleError(String),
22
23    /// Serialization error
24    #[error("Serialization error: {0}")]
25    SerializationError(#[from] serde_json::Error),
26
27    /// YAML serialization error
28    #[error("YAML error: {0}")]
29    YamlError(#[from] serde_yaml::Error),
30
31    /// IO error
32    #[error("IO error: {0}")]
33    IoError(#[from] std::io::Error),
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn test_error_display() {
42        let err = Error::ModelNotFound("test-model".to_string());
43        assert_eq!(err.to_string(), "Model not found: test-model");
44    }
45
46    #[test]
47    fn test_error_from_io() {
48        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
49        let err: Error = io_err.into();
50        assert!(matches!(err, Error::IoError(_)));
51    }
52
53    #[test]
54    fn test_error_invalid_scenario() {
55        let err = Error::InvalidScenario("bad config".to_string());
56        assert_eq!(err.to_string(), "Invalid scenario: bad config");
57    }
58
59    #[test]
60    fn test_error_oracle_error() {
61        let err = Error::OracleError("oracle failed".to_string());
62        assert_eq!(err.to_string(), "Oracle error: oracle failed");
63    }
64
65    #[test]
66    fn test_error_from_serde_json() {
67        let json_err: serde_json::Error = serde_json::from_str::<i32>("not json").unwrap_err();
68        let err: Error = json_err.into();
69        assert!(matches!(err, Error::SerializationError(_)));
70        assert!(err.to_string().contains("Serialization error"));
71    }
72
73    #[test]
74    fn test_error_from_serde_yaml() {
75        let yaml_err: serde_yaml::Error = serde_yaml::from_str::<i32>("not: [yaml").unwrap_err();
76        let err: Error = yaml_err.into();
77        assert!(matches!(err, Error::YamlError(_)));
78        assert!(err.to_string().contains("YAML error"));
79    }
80
81    #[test]
82    fn test_error_debug() {
83        let err = Error::ModelNotFound("test".to_string());
84        let debug_str = format!("{err:?}");
85        assert!(debug_str.contains("ModelNotFound"));
86    }
87}