use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum GateStatus {
Pass,
Warn,
Fail,
Skipped,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct GateOutcome {
pub status: GateStatus,
pub enforced: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub observed: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub threshold: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub threshold_label: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub files: Vec<GateFile>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct GateFile {
pub path: String,
pub error_count: u32,
pub panicked: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<GateFileReason>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum GateFileReason {
NestingTooDeep,
}
impl GateOutcome {
#[must_use]
pub const fn new(status: GateStatus, enforced: bool) -> Self {
Self {
status,
enforced,
observed: None,
threshold: None,
threshold_label: None,
files: Vec::new(),
}
}
#[must_use]
pub const fn measured(
status: GateStatus,
enforced: bool,
observed: f64,
threshold: f64,
) -> Self {
Self {
status,
enforced,
observed: Some(observed),
threshold: Some(threshold),
threshold_label: None,
files: Vec::new(),
}
}
#[must_use]
pub fn counted(
status: GateStatus,
enforced: bool,
observed: f64,
threshold_label: &str,
) -> Self {
Self {
status,
enforced,
observed: Some(observed),
threshold: None,
threshold_label: Some(threshold_label.to_owned()),
files: Vec::new(),
}
}
#[must_use]
pub fn with_files(status: GateStatus, enforced: bool, files: Vec<GateFile>) -> Self {
#[expect(
clippy::cast_precision_loss,
reason = "a file count never approaches the f64 integer limit"
)]
let observed = files.len() as f64;
Self {
status,
enforced,
observed: Some(observed),
threshold: None,
threshold_label: None,
files,
}
}
#[must_use]
pub const fn fails_run(&self) -> bool {
self.enforced && matches!(self.status, GateStatus::Fail)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_unenforced_failure_does_not_fail_the_run() {
let outcome = GateOutcome::new(GateStatus::Fail, false);
assert!(!outcome.fails_run());
let enforced = GateOutcome::new(GateStatus::Fail, true);
assert!(enforced.fails_run());
}
#[test]
fn a_skipped_gate_never_fails_the_run() {
assert!(!GateOutcome::new(GateStatus::Skipped, true).fails_run());
assert!(!GateOutcome::new(GateStatus::Warn, true).fails_run());
}
}