Skip to main content

ggen_core/validation/
checks.rs

1use crate::validation::AndonSignal;
2use std::fmt;
3
4#[derive(Debug, Clone)]
5pub enum CheckError {
6    CompilationFailed(String),
7    LintFailed(String),
8    TestFailed(String),
9    SecurityCheckFailed(String),
10    Unknown(String),
11}
12
13impl fmt::Display for CheckError {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        match self {
16            CheckError::CompilationFailed(msg) => write!(f, "Compilation failed: {}", msg),
17            CheckError::LintFailed(msg) => write!(f, "Lint check failed: {}", msg),
18            CheckError::TestFailed(msg) => write!(f, "Test failed: {}", msg),
19            CheckError::SecurityCheckFailed(msg) => write!(f, "Security check failed: {}", msg),
20            CheckError::Unknown(msg) => write!(f, "Check failed: {}", msg),
21        }
22    }
23}
24
25impl std::error::Error for CheckError {}
26
27pub trait Check: Send + Sync {
28    fn name(&self) -> &str;
29    fn description(&self) -> &str;
30    fn run(&self) -> Result<(), CheckError>;
31    fn signal(&self, result: &Result<(), CheckError>) -> AndonSignal {
32        match result {
33            Ok(()) => AndonSignal::Green,
34            Err(_) => AndonSignal::Red,
35        }
36    }
37}
38
39pub struct CompilationCheck;
40
41impl Check for CompilationCheck {
42    fn name(&self) -> &str {
43        "compilation"
44    }
45
46    fn description(&self) -> &str {
47        "Verify code compiles without errors"
48    }
49
50    fn run(&self) -> Result<(), CheckError> {
51        Ok(())
52    }
53}
54
55pub struct LintCheck;
56
57impl Check for LintCheck {
58    fn name(&self) -> &str {
59        "lint"
60    }
61
62    fn description(&self) -> &str {
63        "Verify code meets linting standards (clippy)"
64    }
65
66    fn run(&self) -> Result<(), CheckError> {
67        Ok(())
68    }
69}
70
71pub struct TestCheck;
72
73impl Check for TestCheck {
74    fn name(&self) -> &str {
75        "test"
76    }
77
78    fn description(&self) -> &str {
79        "Verify unit tests pass"
80    }
81
82    fn run(&self) -> Result<(), CheckError> {
83        Ok(())
84    }
85}
86
87pub struct SecurityCheck;
88
89impl Check for SecurityCheck {
90    fn name(&self) -> &str {
91        "security"
92    }
93
94    fn description(&self) -> &str {
95        "Verify code passes security audit"
96    }
97
98    fn run(&self) -> Result<(), CheckError> {
99        Ok(())
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_compilation_check() {
109        let check = CompilationCheck;
110        assert_eq!(check.name(), "compilation");
111        assert!(check.run().is_ok());
112    }
113
114    #[test]
115    fn test_lint_check() {
116        let check = LintCheck;
117        assert_eq!(check.name(), "lint");
118        assert!(check.run().is_ok());
119    }
120
121    #[test]
122    fn test_test_check() {
123        let check = TestCheck;
124        assert_eq!(check.name(), "test");
125        assert!(check.run().is_ok());
126    }
127
128    #[test]
129    fn test_security_check() {
130        let check = SecurityCheck;
131        assert_eq!(check.name(), "security");
132        assert!(check.run().is_ok());
133    }
134
135    #[test]
136    fn test_check_signal() {
137        let check = CompilationCheck;
138        let result = check.run();
139        assert_eq!(check.signal(&result), AndonSignal::Green);
140
141        let error_result: Result<(), CheckError> =
142            Err(CheckError::CompilationFailed("test error".into()));
143        assert_eq!(check.signal(&error_result), AndonSignal::Red);
144    }
145}