1
//! Picker options for the client forms, built on the resolver's `Directory`
2
//! so aliases match the names lookups resolve.
3

            
4
use uuid::Uuid;
5

            
6
use super::Directory;
7
use crate::account_order::{path_names, path_then_id};
8
use crate::option_labels::unique_labels;
9

            
10
/// The sources a [`Directory`] holds.
11
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12
pub(crate) enum DirectorySource {
13
    Accounts,
14
    Commodities,
15
}
16

            
17
/// One selectable entity: id, unique display label and its bare name.
18
#[derive(Debug, Clone, PartialEq, Eq)]
19
pub struct FormOption {
20
    pub id: Uuid,
21
    pub label: String,
22
    pub alias: String,
23
    /// The whole account path, or the commodity's display text.
24
    pub path: String,
25
    /// The only commodity an account holds; set by [`crate::forms::options`].
26
    pub commodity: Option<Uuid>,
27
}
28

            
29
impl Directory {
30
146581
    pub(crate) fn options(&self, source: DirectorySource) -> Vec<FormOption> {
31
146581
        let (entries, fold): (_, fn(&str) -> String) = match source {
32
75214
            DirectorySource::Accounts => (&self.accounts, str::to_string),
33
71367
            DirectorySource::Commodities => (&self.commodities, str::to_lowercase),
34
        };
35
146581
        let paths: Vec<(Uuid, Vec<String>)> = entries
36
146581
            .iter()
37
261026
            .map(|(id, name)| {
38
261026
                let names = match source {
39
149330
                    DirectorySource::Accounts => self.account_names(*id),
40
111696
                    DirectorySource::Commodities => vec![display(*id, name.as_deref())],
41
                };
42
261026
                (*id, names)
43
261026
            })
44
            .collect();
45
146581
        let labels = unique_labels(&paths, fold);
46
146581
        let mut options: Vec<FormOption> = entries
47
146581
            .iter()
48
146581
            .zip(paths)
49
146581
            .zip(labels)
50
261026
            .map(|(((id, name), (_, names)), label)| {
51
261026
                let path = names.join(":");
52
261026
                FormOption {
53
261026
                    id: *id,
54
261026
                    alias: display(*id, name.as_deref()),
55
261026
                    label,
56
261026
                    path,
57
261026
                    commodity: None,
58
261026
                }
59
261026
            })
60
            .collect();
61
146634
        options.sort_by(|a, b| path_then_id((&a.path, a.id), (&b.path, b.id)));
62
146581
        options
63
146581
    }
64

            
65
402357
    pub(crate) fn account_path(&self, id: Uuid) -> String {
66
402357
        self.account_names(id).join(":")
67
402357
    }
68

            
69
551687
    fn account_names(&self, id: Uuid) -> Vec<String> {
70
551687
        path_names(
71
551687
            id,
72
554340
            |id| self.account_name(id),
73
554340
            |id| self.parents.get(&id).copied(),
74
        )
75
551687
    }
76

            
77
554340
    fn account_name(&self, id: Uuid) -> Option<&str> {
78
554340
        self.accounts
79
554340
            .iter()
80
909885
            .find(|(entry, _)| *entry == id)
81
554340
            .and_then(|(_, name)| name.as_deref())
82
554340
    }
83
}
84

            
85
372722
fn display(id: Uuid, name: Option<&str>) -> String {
86
372722
    name.map_or_else(|| id.to_string(), str::to_string)
87
372722
}