elo_rust/codegen/
errors.rs1use std::fmt;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum CodeGenError {
23 UnsupportedFeature(String),
27
28 TypeMismatch(String),
33
34 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}