Skip to main content

entrenar/storage/preflight/checks/
mod.rs

1//! Preflight check definitions and built-in checks.
2
3mod data_integrity;
4mod environment;
5
6#[cfg(test)]
7mod tests;
8
9use super::{CheckMetadata, CheckResult, CheckType, PreflightContext};
10
11/// Check function type
12pub(crate) type CheckFn = Box<dyn Fn(&[Vec<f64>], &PreflightContext) -> CheckResult + Send + Sync>;
13
14/// A single preflight check
15pub struct PreflightCheck {
16    /// Name of the check
17    pub name: String,
18    /// Type of check
19    pub check_type: CheckType,
20    /// Description of what this check validates
21    pub description: String,
22    /// Whether this check is required (failure blocks training)
23    pub required: bool,
24    /// The check function
25    pub(crate) check_fn: CheckFn,
26}
27
28impl From<&PreflightCheck> for CheckMetadata {
29    fn from(check: &PreflightCheck) -> Self {
30        Self {
31            name: check.name.clone(),
32            check_type: check.check_type.clone(),
33            description: check.description.clone(),
34            required: check.required,
35        }
36    }
37}
38
39impl std::fmt::Debug for PreflightCheck {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.debug_struct("PreflightCheck")
42            .field("name", &self.name)
43            .field("check_type", &self.check_type)
44            .field("description", &self.description)
45            .field("required", &self.required)
46            .finish_non_exhaustive()
47    }
48}
49
50impl PreflightCheck {
51    /// Create a new check
52    pub fn new<F>(
53        name: impl Into<String>,
54        check_type: CheckType,
55        description: impl Into<String>,
56        check_fn: F,
57    ) -> Self
58    where
59        F: Fn(&[Vec<f64>], &PreflightContext) -> CheckResult + Send + Sync + 'static,
60    {
61        Self {
62            name: name.into(),
63            check_type,
64            description: description.into(),
65            required: true,
66            check_fn: Box::new(check_fn),
67        }
68    }
69
70    /// Make this check optional (warning only)
71    pub fn optional(mut self) -> Self {
72        self.required = false;
73        self
74    }
75
76    /// Run this check
77    pub fn run(&self, data: &[Vec<f64>], context: &PreflightContext) -> CheckResult {
78        (self.check_fn)(data, context)
79    }
80}