ggen_core/lifecycle/
validation.rs1use super::state::LifecycleState;
7use super::state_validation::ValidatedLifecycleState;
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12pub enum ValidationSeverity {
13 Error,
14 Warning,
15 Info,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct ValidationIssue {
21 pub severity: ValidationSeverity,
22 pub message: String,
23 pub field: Option<String>,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct ValidationResult {
29 pub issues: Vec<ValidationIssue>,
30 pub is_valid: bool,
31}
32
33impl ValidationResult {
34 pub fn new() -> Self {
35 Self {
36 issues: Vec::new(),
37 is_valid: true,
38 }
39 }
40
41 pub fn add_issue(&mut self, severity: ValidationSeverity, message: String) {
42 self.issues.push(ValidationIssue {
43 severity,
44 message,
45 field: None,
46 });
47 if severity == ValidationSeverity::Error {
48 self.is_valid = false;
49 }
50 }
51
52 pub fn add_issue_with_field(
53 &mut self, severity: ValidationSeverity, message: String, field: String,
54 ) {
55 self.issues.push(ValidationIssue {
56 severity,
57 message,
58 field: Some(field),
59 });
60 if severity == ValidationSeverity::Error {
61 self.is_valid = false;
62 }
63 }
64}
65
66impl Default for ValidationResult {
67 fn default() -> Self {
68 Self::new()
69 }
70}
71
72pub struct ReadinessValidator;
74
75impl ReadinessValidator {
76 pub fn new() -> Self {
77 Self
78 }
79
80 pub fn validate(&self, state: &LifecycleState) -> ValidationResult {
87 let mut result = ValidationResult::new();
88
89 match ValidatedLifecycleState::new(state.clone()) {
91 Ok(_) => {
92 }
94 Err(e) => {
95 result.add_issue(
96 ValidationSeverity::Error,
97 format!("Lifecycle state validation failed: {}", e),
98 );
99 return result;
100 }
101 }
102
103 let completed_phases: std::collections::HashSet<&str> = state
105 .phase_history
106 .iter()
107 .filter(|r| r.success)
108 .map(|r| r.phase.as_str())
109 .collect();
110
111 if completed_phases.contains("deploy") && !completed_phases.contains("test") {
113 result.add_issue(
114 ValidationSeverity::Error,
115 "Deploy phase executed without test phase completion".to_string(),
116 );
117 }
118
119 if completed_phases.contains("deploy") {
121 let recommended_phases = ["build", "test"];
122 for phase in recommended_phases {
123 if !completed_phases.contains(phase) {
124 result.add_issue(
125 ValidationSeverity::Warning,
126 format!("Deploy phase executed without {} phase completion", phase),
127 );
128 }
129 }
130 }
131
132 for cache_key in &state.cache_keys {
134 if !completed_phases.contains(cache_key.phase.as_str()) {
135 result.add_issue_with_field(
136 ValidationSeverity::Error,
137 format!(
138 "Cache key references non-existent phase: {}",
139 cache_key.phase
140 ),
141 cache_key.phase.clone(),
142 );
143 }
144 }
145
146 result
147 }
148}
149
150impl Default for ReadinessValidator {
151 fn default() -> Self {
152 Self::new()
153 }
154}