use std::fmt;
use serde::Serialize;
pub mod compare;
pub mod metadata;
pub mod preflight;
pub use compare::{Baseline, ComparisonInputs, compare_against_baseline};
pub use metadata::LevelMetadata;
pub use preflight::{Bundle, Pins, load_bundle, preflight};
pub const LEVELS: [u8; 3] = [1, 2, 3];
pub const REQUIRED_ARTIFACTS: [&str; 6] = [
"metadata.json",
"tcode_report.json",
"prompt.txt",
"stderr.log",
"solution.diff",
"verifier.json",
];
pub const DISPOSITIONS_FILE: &str = "dispositions.json";
pub const CLEAN_STATUS: &str = "success";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Rule {
IncompleteCoverage,
MissingArtifact,
MalformedMetadata,
MockEvidence,
MissingProvenance,
DirtyCheckout,
StaleRunner,
BuildMismatch,
CorrectnessRegression,
MissingDeliverable,
UndispositionedChange,
}
impl Rule {
pub fn as_str(self) -> &'static str {
match self {
Rule::IncompleteCoverage => "incomplete_coverage",
Rule::MissingArtifact => "missing_artifact",
Rule::MalformedMetadata => "malformed_metadata",
Rule::MockEvidence => "mock_evidence",
Rule::MissingProvenance => "missing_provenance",
Rule::DirtyCheckout => "dirty_checkout",
Rule::StaleRunner => "stale_runner",
Rule::BuildMismatch => "build_mismatch",
Rule::CorrectnessRegression => "correctness_regression",
Rule::MissingDeliverable => "missing_deliverable",
Rule::UndispositionedChange => "undispositioned_change",
}
}
}
impl fmt::Display for Rule {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Violation {
pub rule: Rule,
pub level: Option<u8>,
pub detail: String,
}
impl Violation {
pub fn level(rule: Rule, level: u8, detail: impl Into<String>) -> Self {
Self {
rule,
level: Some(level),
detail: detail.into(),
}
}
pub fn bundle(rule: Rule, detail: impl Into<String>) -> Self {
Self {
rule,
level: None,
detail: detail.into(),
}
}
}
impl fmt::Display for Violation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.level {
Some(level) => write!(f, "[{}] L{}: {}", self.rule, level, self.detail),
None => write!(f, "[{}] bundle: {}", self.rule, self.detail),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct GateReport {
pub levels: Vec<u8>,
pub violations: Vec<Violation>,
pub notes: Vec<String>,
}
impl GateReport {
pub fn passed(&self) -> bool {
self.violations.is_empty()
}
pub fn absorb(&mut self, other: GateReport) {
self.violations.extend(other.violations);
self.notes.extend(other.notes);
}
pub fn render_human(&self) -> String {
let mut out = String::new();
let levels = if self.levels.is_empty() {
"none".to_string()
} else {
self.levels
.iter()
.map(|l| format!("L{l}"))
.collect::<Vec<_>>()
.join(",")
};
out.push_str(&format!(
"bakeoff-gate: {} ({} violation(s); levels read: {levels})\n",
if self.passed() { "PASS" } else { "FAIL" },
self.violations.len(),
));
for violation in &self.violations {
out.push_str(&format!(" {violation}\n"));
}
for note in &self.notes {
out.push_str(&format!(" note: {note}\n"));
}
out
}
pub fn render_json(&self) -> String {
let value = serde_json::json!({
"passed": self.passed(),
"levels": self.levels,
"violations": self.violations,
"notes": self.notes,
});
serde_json::to_string_pretty(&value).unwrap_or_else(|_| "{}".to_string())
}
}
#[cfg(test)]
mod tests;