Skip to main content

rpc/
account_order.rs

1//! The account path and the picker order every client shows.
2
3use std::cmp::Ordering;
4use std::collections::HashSet;
5
6use 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.
10pub 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
26/// The `:`-joined [`path_names`].
27pub 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/// Path first, then id: the order of every account picker and list.
36#[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}