use std::cmp::Reverse;
use std::collections::BTreeMap;
use crate::{ActionKind, Answer, Grant, Level, Lifetime, Segment, TreePath};
pub trait Labels {
fn label(&self, level: Level, id: &str) -> Option<String> {
let _ = (level, id);
None
}
fn everything(&self) -> String;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Ids;
impl Labels for Ids {
fn everything(&self) -> String {
"Everything".to_owned()
}
}
#[must_use]
pub fn describe_node(node: &TreePath, labels: &dyn Labels) -> String {
if node.is_root() {
return labels.everything();
}
node.segments()
.iter()
.map(|segment| match segment {
Segment::Name(level, id) => labels.label(*level, id).unwrap_or_else(|| match level {
Level::Workspace => format!("workspace {id}"),
_ => id.clone(),
}),
Segment::Any(level) => format!("every {}", level.noun()),
Segment::Rest => "anywhere".to_owned(),
})
.collect::<Vec<_>>()
.join(" › ")
}
#[must_use]
pub fn describe(grants: &[Grant], labels: &dyn Labels) -> String {
let mut by_node: BTreeMap<(&TreePath, Reverse<Lifetime>), BTreeMap<ActionKind, Answer>> =
BTreeMap::new();
for grant in grants {
let answers = by_node
.entry((&grant.node, Reverse(grant.lifetime)))
.or_default();
for kind in grant.kinds.iter() {
answers
.entry(kind)
.and_modify(|held| *held = (*held).min(grant.answer))
.or_insert(grant.answer);
}
}
if by_node.is_empty() {
return "Nothing is granted.".to_owned();
}
by_node
.into_iter()
.map(|((node, Reverse(lifetime)), answers)| {
format!(
"{}{}: {}.",
describe_node(node, labels),
lifetime_suffix(lifetime),
clauses(&answers)
)
})
.collect::<Vec<_>>()
.join(" ")
}
fn clauses(answers: &BTreeMap<ActionKind, Answer>) -> String {
if answers.len() == ActionKind::ALL.len() {
let first = answers.values().next().copied();
if let Some(answer) = first.filter(|answer| answers.values().all(|held| held == answer)) {
return match answer {
Answer::Ask => "everything asks first".to_owned(),
answer => format!("everything {}", answer.phrase()),
};
}
}
[Answer::Allow, Answer::Ask, Answer::Deny]
.into_iter()
.filter_map(|answer| {
let kinds: Vec<&str> = answers
.iter()
.filter(|(_, held)| **held == answer)
.map(|(kind, _)| kind.phrase())
.collect();
(!kinds.is_empty()).then(|| format!("{} {}", join_and(&kinds), answer.phrase()))
})
.collect::<Vec<_>>()
.join(", ")
}
fn join_and(items: &[&str]) -> String {
match items {
[] => String::new(),
[one] => (*one).to_owned(),
[init @ .., last] => format!("{} and {last}", init.join(", ")),
}
}
fn lifetime_suffix(lifetime: Lifetime) -> String {
match lifetime {
Lifetime::UntilRevoked => String::new(),
Lifetime::Session => " (this session)".to_owned(),
Lifetime::Until(at) => format!(" (until {at} ms)"),
Lifetime::Once => " (once)".to_owned(),
}
}
#[must_use]
pub fn sentences(grants: &[Grant], labels: &dyn Labels) -> String {
if grants.is_empty() {
return "Nothing is granted.".to_owned();
}
let mut ordered: Vec<&Grant> = grants.iter().collect();
ordered.sort_by(|a, b| {
b.node
.segments()
.len()
.cmp(&a.node.segments().len())
.then_with(|| b.node.cmp(&a.node))
.then_with(|| a.id.cmp(&b.id))
});
ordered
.iter()
.map(|grant| {
format!(
"{}{}: {}.",
describe_node(&grant.node, labels),
lifetime_suffix(grant.lifetime),
grant_clauses(grant),
) + if grant.denies_reading_within_workspace() {
HIDDEN_NOT_SEALED
} else {
""
}
})
.collect::<Vec<_>>()
.join(" ")
}
const HIDDEN_NOT_SEALED: &str = " Tilo keeps it from them, but it is not encrypted apart: their devices hold the workspace's keys, so only the workspace itself keeps content from someone.";
fn grant_clauses(grant: &Grant) -> String {
if grant.kinds == crate::KindSet::ALL {
return match grant.answer {
Answer::Ask => "everything asks first".to_owned(),
answer => format!("everything {}", answer.phrase()),
};
}
let kinds: Vec<&str> = grant.kinds.iter().map(ActionKind::phrase).collect();
let what = join_and(&kinds);
if grant.answer == Answer::Deny {
format!("{what} not allowed")
} else {
format!("{what} {}", grant.answer.phrase())
}
}