use ainl_contracts::{ContextFreshness, ImpactDecision};
#[derive(Debug, Clone, Default)]
pub struct FreshnessInputs {
pub index_stale_vs_head: Option<bool>,
pub unknown: bool,
}
#[must_use]
pub fn evaluate_freshness(i: &FreshnessInputs) -> ContextFreshness {
if i.unknown {
return ContextFreshness::Unknown;
}
match i.index_stale_vs_head {
Some(true) => ContextFreshness::Stale,
Some(false) => ContextFreshness::Fresh,
None => ContextFreshness::Unknown,
}
}
#[must_use]
pub fn impact_decision_strict(f: ContextFreshness, repo_intel_ready: bool) -> ImpactDecision {
match f {
ContextFreshness::Stale => {
if repo_intel_ready {
ImpactDecision::RequireImpactFirst
} else {
ImpactDecision::BlockUntilFresh
}
}
ContextFreshness::Unknown => {
let _ = repo_intel_ready;
ImpactDecision::RequireImpactFirst
}
ContextFreshness::Fresh => ImpactDecision::AllowExecute,
}
}
#[must_use]
pub fn impact_decision_balanced(f: ContextFreshness, repo_intel_ready: bool) -> ImpactDecision {
match f {
ContextFreshness::Fresh => ImpactDecision::AllowExecute,
ContextFreshness::Stale | ContextFreshness::Unknown => {
if repo_intel_ready {
ImpactDecision::RequireImpactFirst
} else {
ImpactDecision::AllowExecute
}
}
}
}
#[must_use]
pub fn can_execute_with_context(f: ContextFreshness, strict: bool, repo_intel_ready: bool) -> bool {
let d = if strict {
impact_decision_strict(f, repo_intel_ready)
} else {
impact_decision_balanced(f, repo_intel_ready)
};
matches!(d, ImpactDecision::AllowExecute)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fresh_allows() {
assert!(can_execute_with_context(
ContextFreshness::Fresh,
true,
false
));
}
}