Skip to main content

entrenar/storage/preflight/
types.rs

1//! Core types for preflight validation system.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use thiserror::Error;
6
7/// Preflight validation errors
8#[derive(Debug, Error)]
9pub enum PreflightError {
10    #[error("Data integrity check failed: {0}")]
11    DataIntegrity(String),
12
13    #[error("Environment check failed: {0}")]
14    Environment(String),
15
16    #[error("Validation failed: {checks_failed} of {total_checks} checks failed")]
17    ValidationFailed { checks_failed: usize, total_checks: usize },
18
19    #[error("IO error: {0}")]
20    Io(#[from] std::io::Error),
21}
22
23/// Metadata for a preflight check (without the check function)
24#[derive(Debug, Clone)]
25pub struct CheckMetadata {
26    /// Name of the check
27    pub name: String,
28    /// Type of check
29    pub check_type: CheckType,
30    /// Description of what this check validates
31    pub description: String,
32    /// Whether this check is required
33    pub required: bool,
34}
35
36/// Context for preflight checks
37#[derive(Debug, Clone, Default)]
38pub struct PreflightContext {
39    /// Minimum required samples
40    pub min_samples: Option<usize>,
41    /// Minimum required features
42    pub min_features: Option<usize>,
43    /// Minimum required disk space in MB
44    pub min_disk_space_mb: Option<u64>,
45    /// Minimum required memory in MB
46    pub min_memory_mb: Option<u64>,
47    /// Expected label range
48    pub label_range: Option<(f64, f64)>,
49    /// Custom parameters
50    pub params: HashMap<String, String>,
51}
52
53impl PreflightContext {
54    /// Create a new context
55    pub fn new() -> Self {
56        Self::default()
57    }
58
59    /// Set minimum samples
60    pub fn with_min_samples(mut self, min: usize) -> Self {
61        self.min_samples = Some(min);
62        self
63    }
64
65    /// Set minimum features
66    pub fn with_min_features(mut self, min: usize) -> Self {
67        self.min_features = Some(min);
68        self
69    }
70
71    /// Set minimum disk space
72    pub fn with_min_disk_space_mb(mut self, mb: u64) -> Self {
73        self.min_disk_space_mb = Some(mb);
74        self
75    }
76
77    /// Set minimum memory
78    pub fn with_min_memory_mb(mut self, mb: u64) -> Self {
79        self.min_memory_mb = Some(mb);
80        self
81    }
82
83    /// Set expected label range
84    pub fn with_label_range(mut self, min: f64, max: f64) -> Self {
85        self.label_range = Some((min, max));
86        self
87    }
88}
89
90/// Type of preflight check
91#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
92pub enum CheckType {
93    /// Data integrity checks
94    DataIntegrity,
95    /// Environment checks
96    Environment,
97    /// Resource availability checks
98    Resources,
99    /// Configuration validation
100    Configuration,
101    /// Custom check
102    Custom(String),
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    // =========================================================================
110    // PreflightContext Tests
111    // =========================================================================
112
113    #[test]
114    fn test_context_default() {
115        let ctx = PreflightContext::new();
116        assert!(ctx.min_samples.is_none());
117        assert!(ctx.min_features.is_none());
118    }
119
120    #[test]
121    fn test_context_builder() {
122        let ctx = PreflightContext::new()
123            .with_min_samples(100)
124            .with_min_features(10)
125            .with_min_disk_space_mb(1024)
126            .with_min_memory_mb(512)
127            .with_label_range(0.0, 1.0);
128
129        assert_eq!(ctx.min_samples, Some(100));
130        assert_eq!(ctx.min_features, Some(10));
131        assert_eq!(ctx.min_disk_space_mb, Some(1024));
132        assert_eq!(ctx.min_memory_mb, Some(512));
133        assert_eq!(ctx.label_range, Some((0.0, 1.0)));
134    }
135}