use crate::{FacetValue, Nesting, Standing};
impl From<tagtree::Standing> for Standing {
fn from(standing: tagtree::Standing) -> Self {
match standing {
tagtree::Standing::Open => Self::Open,
tagtree::Standing::Taken => Self::Taken,
tagtree::Standing::Inherited => Self::Inherited,
tagtree::Standing::Pruned => Self::Pruned,
tagtree::Standing::PrunedAbove => Self::PrunedAbove,
}
}
}
#[must_use]
pub fn values<'a>(
rows: &[tagtree::OutlineNode<'a>],
selection: &tagtree::Selection,
counts: Option<&tagtree::Rollup>,
) -> Vec<FacetValue<'a>> {
rows.iter()
.map(|row| {
let value = FacetValue::new(row.path, row.label)
.standing(selection.standing(row.path).into())
.at(
Nesting::at(u8::try_from(row.depth.saturating_sub(1)).unwrap_or(u8::MAX)),
row.branching,
);
match counts {
Some(rollup) => value.counted(rollup.total(row.path)),
None => value,
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_tagtree_standing_maps_to_its_namesake() {
assert_eq!(Standing::from(tagtree::Standing::Open), Standing::Open);
assert_eq!(Standing::from(tagtree::Standing::Taken), Standing::Taken);
assert_eq!(
Standing::from(tagtree::Standing::Inherited),
Standing::Inherited
);
assert_eq!(Standing::from(tagtree::Standing::Pruned), Standing::Pruned);
assert_eq!(
Standing::from(tagtree::Standing::PrunedAbove),
Standing::PrunedAbove
);
}
#[test]
fn an_unmeasured_facet_carries_no_counts() {
let index = tagtree::TagIndex::new(vec!["a.b".into()]);
let outline = index.outline();
let facet = values(&outline, &tagtree::Selection::new(), None);
assert!(facet.iter().all(|v| v.count.is_none()));
assert!(facet[0].branching && !facet[1].branching);
assert_eq!(facet[0].label, "a");
assert_eq!(facet[1].label, "b");
}
#[test]
fn a_prune_reads_out_below_it() {
let index = tagtree::TagIndex::new(vec!["a.b.c".into()]);
let outline = index.outline();
let mut selection = tagtree::Selection::taking(["a"]);
selection.prune("a.b");
let facet = values(&outline, &selection, None);
let standings: Vec<Standing> = facet.iter().map(|v| v.standing).collect();
assert_eq!(
standings,
[Standing::Taken, Standing::Pruned, Standing::PrunedAbove]
);
}
}