Skip to main content

xchecker_gate/
types.rs

1//! Gate result types for policy evaluation
2//!
3//! This module provides types for representing gate evaluation results.
4
5use serde::{Deserialize, Serialize};
6
7/// Result of gate evaluation
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct GateResult {
10    /// Schema version for the result
11    pub schema_version: String,
12
13    /// ID of the spec being evaluated
14    pub spec_id: String,
15
16    /// Whether spec passed all gate checks
17    pub passed: bool,
18
19    /// Human-readable summary of result
20    pub summary: String,
21
22    /// Individual conditions evaluated
23    pub conditions: Vec<GateCondition>,
24
25    /// Reasons for failure (if any)
26    pub failure_reasons: Vec<String>,
27}
28
29/// Individual condition evaluated by gate
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct GateCondition {
32    /// Name of condition
33    pub name: String,
34
35    /// Description of what the condition checks
36    pub description: String,
37
38    /// Whether the condition passed
39    pub passed: bool,
40
41    /// Actual value observed
42    pub actual: Option<String>,
43
44    /// Expected value for passing
45    pub expected: Option<String>,
46}
47
48/// Summary of pending fixups for a spec
49#[derive(Debug, Clone, Default, Serialize, Deserialize)]
50pub struct PendingFixupsStats {
51    /// Number of target files with pending changes
52    pub targets: u32,
53    /// Estimated lines to be added
54    pub est_added: u32,
55    /// Estimated lines to be removed
56    pub est_removed: u32,
57}
58
59/// Result of attempting to determine pending fixups state
60///
61/// This tri-state result allows callers to distinguish between:
62/// - No fixups needed (None)
63/// - Fixups found (Some(stats))
64/// - Unable to determine (Unknown) - e.g., corrupted review artifact
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub enum PendingFixupsResult {
67    /// No fixups are pending (review completed, no markers, or review not done yet)
68    None,
69    /// Fixups are pending with the given statistics
70    Some(PendingFixupsStats),
71    /// Unable to determine fixup state (e.g., markers present but parse failed)
72    Unknown {
73        /// Reason why fixups state couldn't be determined
74        reason: String,
75    },
76}
77
78impl PendingFixupsResult {
79    /// Check if there are definitely no pending fixups
80    #[must_use]
81    pub fn is_none(&self) -> bool {
82        matches!(self, Self::None)
83    }
84
85    /// Check if fixups are definitely pending
86    #[must_use]
87    pub fn is_some(&self) -> bool {
88        matches!(self, Self::Some(_))
89    }
90
91    /// Check if fixup state is unknown/indeterminate
92    #[must_use]
93    pub fn is_unknown(&self) -> bool {
94        matches!(self, Self::Unknown { .. })
95    }
96
97    /// Get the stats if fixups are pending
98    #[must_use]
99    pub fn stats(&self) -> Option<&PendingFixupsStats> {
100        match self {
101            Self::Some(stats) => Some(stats),
102            _ => None,
103        }
104    }
105
106    /// Get the target count, or 0 if none/unknown
107    ///
108    /// Note: Use this only for display purposes. For gate checks,
109    /// use `is_unknown()` to detect indeterminate states.
110    #[must_use]
111    pub fn targets_or_zero(&self) -> u32 {
112        match self {
113            Self::Some(stats) => stats.targets,
114            _ => 0,
115        }
116    }
117
118    /// Convert to legacy PendingFixupsStats (for backward compatibility)
119    ///
120    /// Returns default stats (zeros) for None and Unknown states.
121    #[must_use]
122    pub fn into_stats(self) -> PendingFixupsStats {
123        match self {
124            Self::Some(stats) => stats,
125            _ => PendingFixupsStats::default(),
126        }
127    }
128}
129
130/// Trait for types that can provide spec data for gate evaluation
131///
132/// This trait abstracts data sources needed for gate checks, allowing
133/// gate evaluation to work with different data providers (e.g., OrchestratorHandle,
134/// direct file system access, etc.).
135pub trait SpecDataProvider {
136    /// Get the base path for the spec
137    fn base_path(&self) -> &std::path::Path;
138
139    /// Get the spec ID
140    fn spec_id(&self) -> &str;
141
142    /// Get the receipt manager
143    fn receipt_manager(&self) -> &xchecker_receipt::ReceiptManager;
144
145    /// Check if a phase is completed
146    fn phase_completed(&self, phase: xchecker_utils::types::PhaseId) -> bool;
147
148    /// Get the pending fixups result
149    fn pending_fixups_result(&self) -> PendingFixupsResult;
150}