Skip to main content

rpc/resolve/
options.rs

1//! Picker options for the client forms, built on the resolver's `Directory`
2//! so aliases match the names lookups resolve.
3
4use uuid::Uuid;
5
6use super::Directory;
7use crate::account_order::{path_names, path_then_id};
8use crate::option_labels::unique_labels;
9
10/// The sources a [`Directory`] holds.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub(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)]
19pub 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
29impl Directory {
30    pub(crate) fn options(&self, source: DirectorySource) -> Vec<FormOption> {
31        let (entries, fold): (_, fn(&str) -> String) = match source {
32            DirectorySource::Accounts => (&self.accounts, str::to_string),
33            DirectorySource::Commodities => (&self.commodities, str::to_lowercase),
34        };
35        let paths: Vec<(Uuid, Vec<String>)> = entries
36            .iter()
37            .map(|(id, name)| {
38                let names = match source {
39                    DirectorySource::Accounts => self.account_names(*id),
40                    DirectorySource::Commodities => vec![display(*id, name.as_deref())],
41                };
42                (*id, names)
43            })
44            .collect();
45        let labels = unique_labels(&paths, fold);
46        let mut options: Vec<FormOption> = entries
47            .iter()
48            .zip(paths)
49            .zip(labels)
50            .map(|(((id, name), (_, names)), label)| {
51                let path = names.join(":");
52                FormOption {
53                    id: *id,
54                    alias: display(*id, name.as_deref()),
55                    label,
56                    path,
57                    commodity: None,
58                }
59            })
60            .collect();
61        options.sort_by(|a, b| path_then_id((&a.path, a.id), (&b.path, b.id)));
62        options
63    }
64
65    pub(crate) fn account_path(&self, id: Uuid) -> String {
66        self.account_names(id).join(":")
67    }
68
69    fn account_names(&self, id: Uuid) -> Vec<String> {
70        path_names(
71            id,
72            |id| self.account_name(id),
73            |id| self.parents.get(&id).copied(),
74        )
75    }
76
77    fn account_name(&self, id: Uuid) -> Option<&str> {
78        self.accounts
79            .iter()
80            .find(|(entry, _)| *entry == id)
81            .and_then(|(_, name)| name.as_deref())
82    }
83}
84
85fn display(id: Uuid, name: Option<&str>) -> String {
86    name.map_or_else(|| id.to_string(), str::to_string)
87}