Skip to main content

elo_rust/codegen/
errors.rs

1//! Code generation errors
2//!
3//! Provides error types for code generation failures, including unsupported features,
4//! type mismatches, and invalid expressions.
5
6use std::fmt;
7
8/// Errors that can occur during ELO-to-Rust code generation
9///
10/// This enum represents the different kinds of errors that can occur when compiling
11/// ELO validation expressions to Rust code.
12///
13/// # Examples
14///
15/// ```ignore
16/// use elo_rust::codegen::CodeGenError;
17///
18/// let err = CodeGenError::UnsupportedFeature("custom functions".to_string());
19/// println!("{}", err); // "Unsupported feature: custom functions"
20/// ```
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum CodeGenError {
23    /// Feature is not yet supported in this implementation
24    ///
25    /// Contains the name of the unsupported feature (e.g., "async validators", "custom functions")
26    UnsupportedFeature(String),
27
28    /// Type mismatch detected during code generation
29    ///
30    /// This occurs when operands don't match expected types for an operation.
31    /// Contains a descriptive message about the mismatch.
32    TypeMismatch(String),
33
34    /// Invalid or malformed ELO expression
35    ///
36    /// This occurs when the expression cannot be parsed or compiled.
37    /// Contains details about what makes the expression invalid.
38    InvalidExpression(String),
39}
40
41impl fmt::Display for CodeGenError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Self::UnsupportedFeature(feature) => {
45                write!(f, "Unsupported feature: {}", feature)
46            }
47            Self::TypeMismatch(msg) => {
48                write!(f, "Type mismatch: {}", msg)
49            }
50            Self::InvalidExpression(msg) => {
51                write!(f, "Invalid expression: {}", msg)
52            }
53        }
54    }
55}
56
57impl std::error::Error for CodeGenError {}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_unsupported_feature_creation() {
65        let err = CodeGenError::UnsupportedFeature("async".to_string());
66        assert_eq!(err.to_string(), "Unsupported feature: async");
67    }
68
69    #[test]
70    fn test_type_mismatch_creation() {
71        let err = CodeGenError::TypeMismatch("expected string".to_string());
72        assert_eq!(err.to_string(), "Type mismatch: expected string");
73    }
74
75    #[test]
76    fn test_invalid_expression_creation() {
77        let err = CodeGenError::InvalidExpression("malformed".to_string());
78        assert_eq!(err.to_string(), "Invalid expression: malformed");
79    }
80
81    #[test]
82    fn test_error_equality() {
83        let err1 = CodeGenError::UnsupportedFeature("test".to_string());
84        let err2 = CodeGenError::UnsupportedFeature("test".to_string());
85        assert_eq!(err1, err2);
86    }
87
88    #[test]
89    fn test_error_debug() {
90        let err = CodeGenError::TypeMismatch("test".to_string());
91        let debug_str = format!("{:?}", err);
92        assert!(debug_str.contains("TypeMismatch"));
93    }
94
95    #[test]
96    fn test_error_is_error_trait() {
97        use std::error::Error;
98        let err: Box<dyn Error> = Box::new(CodeGenError::InvalidExpression("test".to_string()));
99        assert!(!err.to_string().is_empty());
100    }
101}