1
//! The account path and the picker order every client shows.
2

            
3
use std::cmp::Ordering;
4
use std::collections::HashSet;
5

            
6
use uuid::Uuid;
7

            
8
/// The names from the root down to `id`; an account with no name is shown by
9
/// its id, and a parent cycle ends the walk.
10
553281
pub fn path_names<'a>(
11
553281
    id: Uuid,
12
553281
    name_of: impl Fn(Uuid) -> Option<&'a str>,
13
553281
    parent_of: impl Fn(Uuid) -> Option<Uuid>,
14
553281
) -> Vec<String> {
15
553281
    let mut seen = HashSet::new();
16
553281
    let mut names = Vec::new();
17
553281
    let mut next = Some(id);
18
1109283
    while let Some(current) = next.filter(|current| seen.insert(*current)) {
19
556002
        names.push(name_of(current).map_or_else(|| current.to_string(), str::to_string));
20
556002
        next = parent_of(current);
21
    }
22
553281
    names.reverse();
23
553281
    names
24
553281
}
25

            
26
/// The `:`-joined [`path_names`].
27
1594
pub fn path_label<'a>(
28
1594
    id: Uuid,
29
1594
    name_of: impl Fn(Uuid) -> Option<&'a str>,
30
1594
    parent_of: impl Fn(Uuid) -> Option<Uuid>,
31
1594
) -> String {
32
1594
    path_names(id, name_of, parent_of).join(":")
33
1594
}
34

            
35
/// Path first, then id: the order of every account picker and list.
36
#[must_use]
37
193994
pub fn path_then_id(a: (&str, Uuid), b: (&str, Uuid)) -> Ordering {
38
193994
    a.0.cmp(b.0).then(a.1.cmp(&b.1))
39
193994
}