use std::borrow::Borrow;
use std::collections::BTreeMap;
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)
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "schema", schemars(rename = "GateOutcomes"))]
#[serde(transparent)]
pub struct GateOutcomes<K: Ord>(BTreeMap<K, GateOutcome>);
impl<K: Ord> Default for GateOutcomes<K> {
fn default() -> Self {
Self::new()
}
}
impl<K: Ord> GateOutcomes<K> {
#[must_use]
pub const fn new() -> Self {
Self(BTreeMap::new())
}
pub fn insert(&mut self, name: K, outcome: GateOutcome) {
self.0.insert(name, outcome);
}
pub fn insert_if(&mut self, name: K, outcome: Option<GateOutcome>) {
if let Some(outcome) = outcome {
self.0.insert(name, outcome);
}
}
#[must_use]
pub fn get<Q>(&self, name: &Q) -> Option<&GateOutcome>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
{
self.0.get(name)
}
#[must_use]
pub fn fails_run(&self) -> bool {
self.0.values().any(GateOutcome::fails_run)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn into_option(self) -> Option<Self> {
if self.0.is_empty() { None } else { Some(self) }
}
}
#[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());
}
}