Lines
98.7 %
Functions
95.65 %
Branches
100 %
//! The one rule turning entity names into picker labels.
use std::collections::{HashMap, HashSet};
use uuid::Uuid;
const MIN_PREFIX: usize = 4;
const HEX_LEN: usize = 32;
const SEPARATOR: &str = ":";
/// Cell-safe text: `|` is the cell separator, line breaks end a cell.
fn cell_safe(raw: &str) -> String {
raw.replace("\r\n", " ")
.replace('|', "¦")
.replace(['\n', '\r'], " ")
}
fn hex_prefix(hex: &str, length: usize) -> &str {
&hex[..length.min(HEX_LEN)]
fn suffixed(base: &str, hex: &str, length: usize) -> String {
let padding = "-".repeat(length.saturating_sub(HEX_LEN));
format!("{base} ‹{}{padding}›", hex_prefix(hex, length))
/// The cell-safe forms of a path, shortest first: the name, then the name
/// under each further ancestor, up to the whole path.
fn candidates(path: &[String]) -> Vec<String> {
(1..=path.len().max(1))
.map(|depth| cell_safe(&path[path.len().saturating_sub(depth)..].join(SEPARATOR)))
.collect()
fn is_uuid(text: &str) -> bool {
Uuid::parse_str(text.trim()).is_ok()
struct Owners(HashMap<String, HashSet<usize>>);
impl Owners {
fn new(forms: &[Vec<String>], fold: fn(&str) -> String) -> Self {
let mut owners: HashMap<String, HashSet<usize>> = HashMap::new();
for (index, texts) in forms.iter().enumerate() {
for text in texts {
owners.entry(fold(text)).or_default().insert(index);
Self(owners)
fn only(&self, folded: &str, index: usize) -> bool {
self.0
.get(folded)
.is_none_or(|owners| owners.iter().all(|&owner| owner == index))
fn taken(&self, folded: &str) -> bool {
self.0.contains_key(folded)
/// The shortest id-prefix suffix that the entries sharing the whole path do
/// not share and that equals no form of any entry.
fn disambiguated(
base: &str,
id: Uuid,
group: &[String],
owners: &Owners,
fold: fn(&str) -> String,
) -> String {
let hex = id.simple().to_string();
let shared = |length: usize| {
let own = hex_prefix(&hex, length);
group
.iter()
.filter(|other| hex_prefix(other, length) == own)
.count()
> 1
};
(MIN_PREFIX..)
.map(|length| (length, suffixed(base, &hex, length)))
.find(|(length, label)| !shared(*length) && !owners.taken(&fold(label)))
.map_or_else(|| base.to_string(), |(_, label)| label)
/// Unique, cell-safe labels for `(id, path)` pairs, in input order; a path is
/// its names from the root down (one name for a flat source). An entry is
/// shown by its shortest form (name, then parent:name, …) that no other entry
/// has as any form, judged on `fold` after every transformation. An entry
/// with no such form, or only UUID-shaped ones, gets its whole path with the
/// shortest id prefix no entry of the same path shares, so the result does
/// not depend on the input order.
pub fn unique_labels(entries: &[(Uuid, Vec<String>)], fold: fn(&str) -> String) -> Vec<String> {
let forms: Vec<Vec<String>> = entries.iter().map(|(_, path)| candidates(path)).collect();
let owners = Owners::new(&forms, fold);
let mut groups: HashMap<String, Vec<String>> = HashMap::new();
for ((id, _), texts) in entries.iter().zip(&forms) {
let whole = texts.last().map_or_else(String::new, |text| fold(text));
groups
.entry(whole)
.or_default()
.push(id.simple().to_string());
entries
.zip(&forms)
.enumerate()
.map(|(index, ((id, _), texts))| {
let free = texts
.find(|text| !is_uuid(text) && owners.only(&fold(text), index));
let whole = texts.last().map_or("", String::as_str);
free.cloned()
.unwrap_or_else(|| disambiguated(whole, *id, &groups[&fold(whole)], &owners, fold))
})
#[cfg(test)]
mod tests;