use super::{ScopeVisitor, Snapshot};
pub(super) fn restrict(snapshot: &Snapshot, paths: &[Vec<(u64, u32)>]) -> Snapshot {
if paths.is_empty() {
return snapshot.clone();
}
snapshot
.iter()
.map(|scope| {
scope
.iter()
.map(|(name, values)| {
let values = values.as_ref().map(|values| {
values
.iter()
.filter(|value| {
paths.iter().any(|path| {
!value.choices.iter().any(|(id, arm)| {
path.iter()
.any(|(other, choice)| id == other && arm != choice)
})
})
})
.cloned()
.collect()
});
(name.clone(), values)
})
.collect()
})
.collect()
}
pub(super) fn taken(left: &[Vec<(u64, u32)>], right: &[Vec<(u64, u32)>]) -> Option<bool> {
match (left.is_empty(), right.is_empty()) {
(false, true) => Some(true),
(true, false) => Some(false),
_ => None,
}
}
pub(in crate::embedded::walk) fn branch_paths(
paths: &[Vec<(u64, u32)>],
id: u64,
arm: u32,
) -> Vec<Vec<(u64, u32)>> {
paths
.iter()
.filter(|path| {
!path
.iter()
.any(|(other, choice)| *other == id && *choice != arm)
})
.map(|path| {
let mut path = path.clone();
if !path.contains(&(id, arm)) {
path.push((id, arm));
}
path
})
.collect()
}
pub(in crate::embedded::walk::variants) fn changed_paths(
visitor: &mut ScopeVisitor<'_>,
snapshot: &Snapshot,
other: &Snapshot,
paths: &[Vec<(u64, u32)>],
) -> Snapshot {
visitor.variant_paths = paths.to_vec();
snapshot
.iter()
.zip(other)
.map(|(scope, other)| {
scope
.iter()
.map(|(name, values)| {
let values = if Some(values) == other.get(name) {
values.clone()
} else {
values
.clone()
.and_then(|values| visitor.with_variant_paths(values))
};
(name.clone(), values)
})
.collect()
})
.collect()
}