Lines
95.45 %
Functions
75 %
Branches
100 %
//! What a select control shows for the reference it carries.
use std::collections::BTreeMap;
use forms::Source;
use rpc::forms::FormOption;
/// The options of every source, the text typed into each select control and
/// the controls a fill wrote (`true` if copied), by their submitted addresses.
#[derive(Debug, Default)]
pub struct References {
pub accounts: Vec<FormOption>,
pub commodities: Vec<FormOption>,
pub templates: Vec<FormOption>,
pub typed: BTreeMap<String, String>,
pub filled: BTreeMap<String, bool>,
}
impl References {
fn options(&self, source: Source) -> &[FormOption] {
match source {
Source::Accounts => &self.accounts,
Source::Commodities => &self.commodities,
Source::Templates => &self.templates,
// No web form selects a script yet; the script pages add the options.
Source::Scripts => &[],
/// The option whose id a cell carries.
pub(super) fn resolve(&self, source: Source, value: &str) -> Option<&FormOption> {
let id = uuid::Uuid::parse_str(value.trim()).ok()?;
self.options(source).iter().find(|option| option.id == id)
/// The current label of the entity a UUID value names; `None` for a value
/// no option has.
pub fn label_of(&self, source: Source, value: &str) -> Option<String> {
self.resolve(source, value)
.map(|option| option.label.clone())
/// The text a select control shows: what was typed into it, else the
/// label of the entity its value names, else the value as it is.
pub(super) fn shown(&self, source: Source, address: &str, value: &str) -> String {
if let Some(typed) = self.typed.get(address) {
return typed.clone();
.map_or_else(|| value.to_string(), |option| option.label.clone())