Lines
100 %
Functions
92.31 %
Branches
//! Picker options for the client forms, built on the resolver's `Directory`
//! so aliases match the names lookups resolve.
use uuid::Uuid;
use super::Directory;
use crate::account_order::{path_names, path_then_id};
use crate::option_labels::unique_labels;
/// The sources a [`Directory`] holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DirectorySource {
Accounts,
Commodities,
}
/// One selectable entity: id, unique display label and its bare name.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormOption {
pub id: Uuid,
pub label: String,
pub alias: String,
/// The whole account path, or the commodity's display text.
pub path: String,
/// The only commodity an account holds; set by [`crate::forms::options`].
pub commodity: Option<Uuid>,
impl Directory {
pub(crate) fn options(&self, source: DirectorySource) -> Vec<FormOption> {
let (entries, fold): (_, fn(&str) -> String) = match source {
DirectorySource::Accounts => (&self.accounts, str::to_string),
DirectorySource::Commodities => (&self.commodities, str::to_lowercase),
};
let paths: Vec<(Uuid, Vec<String>)> = entries
.iter()
.map(|(id, name)| {
let names = match source {
DirectorySource::Accounts => self.account_names(*id),
DirectorySource::Commodities => vec![display(*id, name.as_deref())],
(*id, names)
})
.collect();
let labels = unique_labels(&paths, fold);
let mut options: Vec<FormOption> = entries
.zip(paths)
.zip(labels)
.map(|(((id, name), (_, names)), label)| {
let path = names.join(":");
FormOption {
id: *id,
alias: display(*id, name.as_deref()),
label,
path,
commodity: None,
options.sort_by(|a, b| path_then_id((&a.path, a.id), (&b.path, b.id)));
options
pub(crate) fn account_path(&self, id: Uuid) -> String {
self.account_names(id).join(":")
fn account_names(&self, id: Uuid) -> Vec<String> {
path_names(
id,
|id| self.account_name(id),
|id| self.parents.get(&id).copied(),
)
fn account_name(&self, id: Uuid) -> Option<&str> {
self.accounts
.find(|(entry, _)| *entry == id)
.and_then(|(_, name)| name.as_deref())
fn display(id: Uuid, name: Option<&str>) -> String {
name.map_or_else(|| id.to_string(), str::to_string)