Skip to main content

ggen_core/lifecycle/
validation.rs

1//! Lifecycle validation module
2//!
3//! This module provides validation functionality for lifecycle operations.
4//! Validates production readiness for lifecycle operations.
5
6use super::state::LifecycleState;
7use super::state_validation::ValidatedLifecycleState;
8use serde::{Deserialize, Serialize};
9
10/// Validation severity levels
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12pub enum ValidationSeverity {
13    Error,
14    Warning,
15    Info,
16}
17
18/// A single validation issue
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct ValidationIssue {
21    pub severity: ValidationSeverity,
22    pub message: String,
23    pub field: Option<String>,
24}
25
26/// Validation result containing multiple issues
27#[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
72/// Validator for production readiness
73pub struct ReadinessValidator;
74
75impl ReadinessValidator {
76    pub fn new() -> Self {
77        Self
78    }
79
80    /// Validate lifecycle state for production readiness
81    ///
82    /// Performs basic validation checks:
83    /// - State structure validity (using ValidatedLifecycleState)
84    /// - Critical phase prerequisites (deploy requires test)
85    /// - Cache key validity
86    pub fn validate(&self, state: &LifecycleState) -> ValidationResult {
87        let mut result = ValidationResult::new();
88
89        // Validate state structure using ValidatedLifecycleState
90        match ValidatedLifecycleState::new(state.clone()) {
91            Ok(_) => {
92                // State structure is valid
93            }
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        // Check critical production readiness requirements
104        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        // Critical: deploy phase requires test phase to have completed
112        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        // Warning: check for phases that should typically run before deploy
120        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        // Validate cache keys reference valid phases
133        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}