use std::collections::BTreeSet;
use crate::engine::{CompiledGroup, CompiledPolicy, Section};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Role {
Old,
New,
}
pub(super) struct Shared(BTreeSet<String>);
fn key(section: Section, group: &CompiledGroup) -> String {
format!("{section:?} {group:?}")
}
fn conditional(policy: &CompiledPolicy<'_>) -> BTreeSet<String> {
Section::ALL
.iter()
.flat_map(|s| {
policy
.section(*s)
.iter()
.filter(|g| g.conditional())
.map(|g| key(*s, g))
})
.collect()
}
impl Shared {
pub(super) fn new(old: &CompiledPolicy<'_>, new: &CompiledPolicy<'_>) -> Self {
Self(
conditional(old)
.intersection(&conditional(new))
.cloned()
.collect(),
)
}
pub(super) fn place(
&self,
role: Role,
section: Section,
group: &CompiledGroup,
) -> Option<Section> {
if !group.conditional() {
return Some(section);
}
if role == Role::New || self.0.contains(&key(section, group)) {
return None;
}
Some(match section {
Section::Deny => Section::Deny,
_ => Section::RepoReview,
})
}
}