Lines
100 %
Functions
75 %
Branches
use super::{Cell, Choice, Input};
/// The values of a choice cell, each with its label.
type Options = &'static [(&'static str, &'static str)];
/// The submit button of each form.
const SUBMITS: &[(&str, &str)] = &[
("transaction-create", "Create transaction"),
("transaction-edit", "Save"),
("transaction-query", "Show transactions"),
("report-balance", "Generate"),
("report-activity", "Generate"),
("account-merge", "Review merge"),
];
/// The add and remove buttons of each kind of row.
const ROW_BUTTONS: &[(&str, &str, &str)] = &[
("splits", "Add split", "Remove split"),
("tags", "Add tag", "Remove tag"),
("split-tags", "Add split tag", "Remove split tag"),
pub(super) fn submit_text(form: &str) -> String {
let text = SUBMITS
.iter()
.find(|(name, _)| *name == form)
.map_or("Submit", |(_, text)| text);
t!(text).to_string()
}
fn row_buttons(rows: &str) -> (&'static str, &'static str) {
ROW_BUTTONS
.find(|(kind, _, _)| *kind == rows)
.map_or(("Add row", "Remove"), |(_, add, remove)| (add, remove))
pub(super) fn add_text(rows: &str) -> String {
t!(row_buttons(rows).0).to_string()
pub(super) fn remove_text(rows: &str) -> String {
t!(row_buttons(rows).1).to_string()
fn choices(options: Options, value: &str) -> Vec<Choice> {
options
.map(|&(option, label)| Choice {
value: option,
label: t!(label).to_string(),
selected: option == value,
})
.collect()
/// A descriptor choice: the default stands in for an empty value, and an
/// optional field without a default gets an empty first option.
pub(super) fn field_choice(
options: Options,
default: Option<&str>,
required: bool,
value: &str,
) -> Input {
let value = if value.is_empty() {
default.unwrap_or_default()
} else {
value
};
let blank = Choice {
value: "",
label: String::new(),
selected: value.is_empty(),
let blank = (default.is_none() && !required).then_some(blank);
Input::Choice(blank.into_iter().chain(choices(options, value)).collect())
pub(super) fn mark(rows: &str, key: &str, cell: &mut Cell) {
let Cell::Control(control) = cell else { return };
if let (Input::Text, ("", "filter")) = (&control.input, (rows, key)) {
control.input = Input::Filter(format!("\n{}", control.value));