1use std::cmp::Ordering;
4use std::collections::HashSet;
5
6use uuid::Uuid;
7
8pub fn path_names<'a>(
11 id: Uuid,
12 name_of: impl Fn(Uuid) -> Option<&'a str>,
13 parent_of: impl Fn(Uuid) -> Option<Uuid>,
14) -> Vec<String> {
15 let mut seen = HashSet::new();
16 let mut names = Vec::new();
17 let mut next = Some(id);
18 while let Some(current) = next.filter(|current| seen.insert(*current)) {
19 names.push(name_of(current).map_or_else(|| current.to_string(), str::to_string));
20 next = parent_of(current);
21 }
22 names.reverse();
23 names
24}
25
26pub fn path_label<'a>(
28 id: Uuid,
29 name_of: impl Fn(Uuid) -> Option<&'a str>,
30 parent_of: impl Fn(Uuid) -> Option<Uuid>,
31) -> String {
32 path_names(id, name_of, parent_of).join(":")
33}
34
35#[must_use]
37pub fn path_then_id(a: (&str, Uuid), b: (&str, Uuid)) -> Ordering {
38 a.0.cmp(b.0).then(a.1.cmp(&b.1))
39}