use std::collections::BTreeMap;
use printobserver_types::{
Adjustable, EffectiveBounds, JobManifest, ManifestNarrowing, Range, SafetyEnvelope,
};
#[derive(Debug, Clone, PartialEq)]
pub struct Bounds {
pub effective: EffectiveBounds,
pub narrowings: Vec<ManifestNarrowing>,
}
impl Bounds {
#[must_use]
pub fn range(&self, adjustable: Adjustable) -> Option<Range> {
self.effective.allowed.get(&adjustable).copied()
}
}
#[must_use]
pub fn effective_bounds(envelope: &SafetyEnvelope, manifest: Option<&JobManifest>) -> Bounds {
let mut allowed = BTreeMap::new();
let mut narrowings = Vec::new();
for (adjustable, permitted) in &envelope.allowed {
let Some(asked_for) = manifest.and_then(|held| held.allowed.get(adjustable)) else {
allowed.insert(*adjustable, *permitted);
continue;
};
let applied = Range::new(
permitted.min.max(asked_for.min),
permitted.max.min(asked_for.max),
);
if asked_for.min < permitted.min || asked_for.max > permitted.max {
narrowings.push(ManifestNarrowing {
adjustable: *adjustable,
requested: *asked_for,
applied,
});
}
allowed.insert(*adjustable, applied);
}
Bounds {
effective: EffectiveBounds { allowed },
narrowings,
}
}