Lines
90.91 %
Functions
70 %
Branches
100 %
//! The picker options of a form field.
use std::collections::HashMap;
use forms::Source;
use server::artifact_mgmt::user::{ScriptInfo, TemplateInfo};
use server::command::CmdResult;
use server::command::account_commodities::ListAccountSingleCommodities;
use server::command::template_list::ListTemplates;
use server::script_state::is_enabled;
use server::user::User;
use uuid::Uuid;
use super::Failure;
use crate::option_labels::unique_labels;
use crate::resolve::Directory;
use crate::resolve::options::DirectorySource;
pub use crate::resolve::options::FormOption;
const CTX: &str = "form-options";
/// The user's accounts, commodities or templates as picker options. An account
/// holding exactly one commodity carries it as `commodity`; a template is
/// labelled by its name, listed in the web's order.
pub async fn options(user_id: Uuid, source: Source) -> Result<Vec<FormOption>, Failure> {
match source {
Source::Accounts => directory_options(user_id, DirectorySource::Accounts).await,
Source::Commodities => directory_options(user_id, DirectorySource::Commodities).await,
Source::Templates => templates(user_id).await,
Source::Scripts => scripts(user_id).await,
}
async fn directory_options(
user_id: Uuid,
source: DirectorySource,
) -> Result<Vec<FormOption>, Failure> {
let directory = Directory::load(CTX, user_id).await?;
let mut options = directory.options(source);
if source == DirectorySource::Accounts {
let held = single_commodities(user_id).await?;
for option in &mut options {
option.commodity = held.get(&option.id).copied();
Ok(options)
async fn single_commodities(user_id: Uuid) -> Result<HashMap<Uuid, Uuid>, Failure> {
let held = ListAccountSingleCommodities::new()
.user_id(user_id)
.run()
.await
.map_err(|err| Failure::from_command(CTX, err))?;
match held {
Some(CmdResult::SingleCommodities(held)) => Ok(held),
other => Err(Failure::Internal(wasmtime::Error::msg(format!(
"form-options: unexpected result {other:?}"
)))),
async fn templates(user_id: Uuid) -> Result<Vec<FormOption>, Failure> {
let listed = ListTemplates::new()
match listed {
Some(CmdResult::Templates(listed)) => Ok(template_options(listed)),
async fn scripts(user_id: Uuid) -> Result<Vec<FormOption>, Failure> {
let listed = User { id: user_id }
.list_scripts()
.map_err(|err| Failure::from_command(CTX, err.into()))?;
Ok(script_options(listed))
/// A script's state shows after its name: `tag-sync (disabled, no source)`.
fn script_label(label: &str, script: &ScriptInfo) -> String {
let states: Vec<&str> = [
(!is_enabled(script.enabled.as_deref())).then_some("disabled"),
(!script.has_source).then_some("no source"),
]
.into_iter()
.flatten()
.collect();
match states.is_empty() {
true => label.to_string(),
false => format!("{label} ({})", states.join(", ")),
fn script_options(listed: Vec<ScriptInfo>) -> Vec<FormOption> {
let names: Vec<(Uuid, Vec<String>)> = listed
.iter()
.map(|script| {
let name = script.name.as_deref().unwrap_or("unnamed");
(script.id, vec![name.to_string()])
})
let labels = unique_labels(&names, str::to_string);
listed
.zip(labels)
.map(|(script, label)| FormOption {
id: script.id,
label: script_label(&label, &script),
alias: label.clone(),
path: label,
commodity: None,
.collect()
fn template_options(listed: Vec<TemplateInfo>) -> Vec<FormOption> {
.map(|template| {
let name = template.name.as_deref().unwrap_or("unnamed");
(template.id, vec![name.to_string()])
.map(|(template, label)| FormOption {
id: template.id,
path: label.clone(),
label,