Lines
97.95 %
Functions
46 %
Branches
100 %
//! Helpers that inject fetched Select options into an open form's fields,
//! and methods that fetch option lists from the server.
use cli_core::forms::{FormOption, parse_form_options};
use crate::app::App;
use crate::form::{Field, Form, FormKind};
use crate::modal::Modal;
use crate::route::{FormOptionsSource, RouteCtx};
use crate::widgets::{SelectOption, Widget};
/// The select options of fetched form options.
pub(super) fn select_options_of(options: Vec<FormOption>) -> Vec<SelectOption> {
options
.into_iter()
.map(|option| SelectOption {
id: option.id,
label: option.label,
})
.collect()
}
pub(super) use crate::widgets::with_none;
/// Inject options into ALL Select fields (used for forms with multiple commodity Selects).
pub(super) fn inject_all_select_options(fields: &mut [Field], options: &[SelectOption]) {
for field in fields {
if let Widget::Select(ref mut sw) = field.widget {
sw.set_options(options.to_vec());
/// Inject options into the Select field `key`, leaving the other Selects alone.
pub(super) fn inject_keyed_select_options(
fields: &mut [Field],
key: &str,
options: &[SelectOption],
) {
for field in fields.iter_mut().filter(|f| f.key == Some(key)) {
sw.set_options_preserving_id(options.to_vec());
pub(super) fn inject_splits_account_options(
options: Vec<SelectOption>,
fills: Vec<FormOption>,
if let Widget::Splits(ref mut sw) = field.widget {
sw.set_account_options(options);
sw.set_account_fills(fills);
return;
pub(super) fn inject_splits_commodity_options(fields: &mut [Field], options: Vec<SelectOption>) {
sw.set_commodity_options(options);
impl App {
/// Issue a `form-options` fetch to populate the parent Select in the open
/// account-create form. Bumps `form_options_seq` so a stale reply is dropped.
pub fn fetch_account_form_options(&mut self) {
if self.console_eval.is_none() {
let seq = self.form_options_seq.advance();
let _ = self.dispatch_app_eval(
RouteCtx::FormOptions {
seq,
source: FormOptionsSource::FormAccounts,
},
r#"(form-options "accounts")"#.to_string(),
);
/// Issue a `form-options` fetch for the commodity selects of the open report or convert form.
pub fn fetch_commodity_form_options(&mut self) {
source: FormOptionsSource::FormCommodities,
r#"(form-options "commodities")"#.to_string(),
/// Issue `form-options` for accounts and commodities to populate the Selects
/// in the open transaction form. Both requests share the same seq.
pub fn fetch_transaction_form_options(&mut self) {
self.form_options_seq.advance();
self.request_account_and_commodity_options();
/// Ask for both option lists under the current seq, so replies already in
/// flight for the form beneath stay valid.
pub fn request_account_and_commodity_options(&mut self) {
let seq = self.form_options_seq.current();
for (source, form) in [
(
FormOptionsSource::FormAccounts,
r#"(form-options "accounts")"#,
),
FormOptionsSource::FormCommodities,
r#"(form-options "commodities")"#,
] {
let _ = self.dispatch_app_eval(RouteCtx::FormOptions { seq, source }, form.to_string());
pub(super) fn deliver_form_options_reply(
&mut self,
wire: &str,
seq: u64,
source: FormOptionsSource,
if !self.form_options_seq.awaits(seq) {
match source {
FormOptionsSource::FormAccounts => self.deliver_transaction_account_options(wire),
FormOptionsSource::FormCommodities => self.deliver_transaction_commodity_options(wire),
/// The `(none)` entry keeps an unchosen account empty instead of silently
/// defaulting to the first option.
pub(super) fn deliver_transaction_account_options(&mut self, wire: &str) {
let fills = parse_form_options(wire);
let accounts = select_options_of(fills.clone());
if accounts.is_empty() {
if let Some(builder) = self.overlays.builder_mut() {
builder.accounts.clone_from(&accounts);
self.refresh_row_picks();
let options = with_none(accounts);
if let Some(form) = self.account_select_form_mut() {
fill(form, |form| {
inject_all_select_options(&mut form.fields, &options)
});
} else if let Some(form) = self.transaction_form_mut() {
inject_splits_account_options(&mut form.fields, options, fills);
/// The open form, or the one awaiting its submit reply, when `accepts` its kind.
pub(super) fn form_mut(&mut self, accepts: impl Fn(&Form) -> bool) -> Option<&mut Form> {
self.overlays
.form_mut()
.or_else(|| self.pending_form.as_mut().map(|pending| &mut pending.form))
.filter(|form| accepts(form))
/// The open transaction form, or the one awaiting its submit reply so a
/// rejected submit comes back with populated selects.
pub(super) fn transaction_form_mut(&mut self) -> Option<&mut Form> {
self.form_mut(Form::is_transaction)
/// The open account-create or -edit form, or the one awaiting its
/// submit reply.
pub(super) fn account_select_form_mut(&mut self) -> Option<&mut Form> {
self.form_mut(|form| {
matches!(
form.kind,
FormKind::AccountCreate | FormKind::AccountEdit | FormKind::TransactionFilter
)
/// Gives an open builder row form the names that arrived after it opened.
fn refresh_row_picks(&mut self) {
let Some(Modal::Form(form)) = self.overlays.top() else {
};
let FormKind::FilterRow(shape) = form.kind else {
let Some(options) = self
.overlays
.builder_mut()
.map(|builder| builder.options_for(shape).to_vec())
else {
if let Some(Modal::Form(form)) = self.overlays.top_mut() {
form.refresh_picks(&options);
/// The report form, also beneath a filter builder, or the one awaiting its submit reply.
fn report_form_mut(&mut self) -> Option<&mut Form> {
let pending = self.pending_form.as_mut().map(|pending| &mut pending.form);
.report_form_mut()
.or(pending)
.filter(|form| matches!(form.kind, FormKind::Report(_)))
pub(super) fn deliver_transaction_commodity_options(&mut self, wire: &str) {
let options = select_options(wire);
if options.is_empty() {
builder.commodities.clone_from(&options);
if let Some(form) = self.transaction_form_mut() {
inject_splits_commodity_options(&mut form.fields, options);
} else if let Some(form) = self.form_mut(|form| form.kind == FormKind::CommodityConvert) {
} else if let Some(form) = self.report_form_mut() {
inject_keyed_select_options(&mut form.fields, "target", &with_none(options));
/// Applies arriving options; an untouched form stays untouched.
fn fill(form: &mut Form, inject: impl FnOnce(&mut Form)) {
let clean = !form.is_dirty();
inject(form);
if clean {
form.rebaseline();
/// The options of a `form-options` reply.
fn select_options(wire: &str) -> Vec<SelectOption> {
select_options_of(parse_form_options(wire))