Lines
96.65 %
Functions
50 %
Branches
100 %
//! One from→to row of the splits editor.
use std::collections::BTreeSet;
use cli_core::forms::{FormOption, commodity_fill};
use forms::{Kind, LINK_CELL, Row, Source};
use super::{
COL_FROM, COL_FROM_COMM, COL_TO, COL_TO_AMOUNT, COL_TO_COMM, COL_VALUE, columns, tag_fields,
};
use crate::widgets::{AmountWidget, RowsWidget, SelectOption, SelectWidget, WidgetKind, with_none};
/// One from→to split row with four account/commodity selects and two amount editors.
#[derive(Debug)]
pub struct SplitRow {
pub from: SelectWidget,
pub to: SelectWidget,
pub from_commodity: SelectWidget,
pub to_commodity: SelectWidget,
pub value: AmountWidget,
pub to_amount: AmountWidget,
/// The tags of this split, linked to it by `key` on submit.
pub(super) tags: RowsWidget,
/// Loaded row identity and opaque server data; neither is shown or edited,
/// both are submitted unchanged. Empty on appended rows.
key: String,
hidden: String,
/// Loaded text of the from, to, from-commodity and to-commodity cells.
loaded: [String; 4],
/// The columns filled by a declared fill, not chosen by the user.
auto: BTreeSet<usize>,
/// A loaded row keeps every cell as stored: no fill touches it.
from_load: bool,
}
impl SplitRow {
/// Build a row whose Selects are pre-seeded from the cached option lists,
/// so a row added after the options arrive is immediately completable.
pub(super) fn new(accounts: &[SelectOption], commodities: &[SelectOption]) -> Self {
let mut row = Self {
from: SelectWidget::new(),
to: SelectWidget::new(),
from_commodity: SelectWidget::new(),
to_commodity: SelectWidget::new(),
value: AmountWidget::default(),
to_amount: AmountWidget::default(),
tags: RowsWidget::new(tag_fields()),
key: String::new(),
hidden: String::new(),
loaded: Default::default(),
auto: BTreeSet::new(),
from_load: false,
row.from.set_options(accounts.to_vec());
row.to.set_options(accounts.to_vec());
row.from_commodity
.set_options(with_none(commodities.to_vec()));
row.to_commodity
.set_options(with_empty_meaning(commodities));
row
/// A row holding the loaded cells; each select starts as a single option
/// whose id is the loaded text, until the option lists arrive.
pub(super) fn loaded(row: &Row) -> Self {
let text = |key: &str| row.get(key).map_or("", String::as_str);
let mut loaded = Self::new(&[], &[]);
loaded.value = AmountWidget::with_value(text("value"));
loaded.to_amount = AmountWidget::with_value(text("to-amount"));
loaded.from_load = true;
loaded.key = text("key").to_string();
loaded.hidden = text("hidden").to_string();
loaded.loaded =
["from", "to", "from-commodity", "to-commodity"].map(|k| text(k).to_string());
let [from, to, from_commodity, to_commodity] = &loaded.loaded;
loaded.from.set_options(seed(from));
loaded.to.set_options(seed(to));
loaded.from_commodity.set_options(seed(from_commodity));
loaded.to_commodity.set_options(seed(to_commodity));
loaded
pub(super) fn key(&self) -> &str {
&self.key
pub(super) fn set_key(&mut self, key: String) {
self.key = key;
/// The non-blank tags linked to this split's key.
pub(super) fn tag_rows(&self) -> impl Iterator<Item = Row> + '_ {
let rows = self.tags.rows().into_iter().filter(|tag| !tag.is_empty());
rows.map(|mut tag| {
tag.insert(LINK_CELL.to_string(), self.key.clone());
tag
})
pub(super) fn bind_accounts(&mut self, options: &[SelectOption]) {
let [from, to, ..] = &self.loaded;
self.from.set_options_preserving_id(bound(options, from));
self.to.set_options_preserving_id(bound(options, to));
pub(super) fn bind_commodities(&mut self, options: &[SelectOption]) {
let [.., from, to] = &self.loaded;
self.from_commodity
.set_options_preserving_id(bound(&with_none(options.to_vec()), from));
self.to_commodity
.set_options_preserving_id(bound(&with_empty_meaning(options), to));
/// What column `source` fills into its target: a commodity fills itself,
/// an account its only commodity.
fn fill_of(&self, source: usize, fills: &[FormOption]) -> Option<String> {
let text = self.cell_text(source);
match columns().nth(source).map(|field| field.kind) {
Some(Kind::Select {
source: Source::Commodities,
..
}) => Some(text.to_string()).filter(|text| !text.is_empty()),
_ => commodity_fill(fills, text),
/// Refill column `target` after a change of column `source`, unless the
/// user chose it: an auto-filled one follows the source, even back to unset.
pub(super) fn refill(&mut self, (source, target): (usize, usize), fills: &[FormOption]) {
if self.from_load {
return;
let fill = self.fill_of(source, fills);
let auto = self.auto.contains(&target);
let Some(select) = self.select_mut(target) else {
if !auto && !select.value().is_empty() {
select.select_id(fill.as_deref().unwrap_or(""));
match fill {
Some(_) => self.auto.insert(target),
None => self.auto.remove(&target),
pub(super) fn commodity_state(&mut self, target: usize) -> Option<(bool, String)> {
let value = self.select_mut(target)?.value().to_string();
Some((self.auto.contains(&target), value))
pub(super) fn restore_commodity(&mut self, target: usize, (auto, value): (bool, String)) {
if let Some(select) = self.select_mut(target) {
select.select_id(&value);
if auto {
self.auto.insert(target);
} else {
self.auto.remove(&target);
pub(super) fn picked(&mut self, col: usize) {
self.auto.remove(&col);
/// WidgetKind for the given column index.
#[must_use]
pub fn col_kind(col: usize) -> WidgetKind {
match columns().nth(col).map(|field| field.kind) {
Some(Kind::Select { .. }) => WidgetKind::Select,
_ => WidgetKind::Amount,
/// Submit text of descriptor field `key`; `""` for unknown keys.
pub fn text(&self, key: &str) -> &str {
match key {
"key" => &self.key,
"hidden" => &self.hidden,
_ => columns()
.position(|field| field.key == key)
.map_or("", |col| self.cell_text(col)),
fn cell_text(&self, col: usize) -> &str {
match col {
COL_FROM => self.from.value(),
COL_TO => self.to.value(),
COL_FROM_COMM => self.from_commodity.value(),
COL_TO_COMM => self.to_commodity.value(),
COL_VALUE => self.value.value(),
COL_TO_AMOUNT => self.to_amount.value(),
_ => "",
pub(in crate::widgets) fn select(&self, col: usize) -> Option<&SelectWidget> {
COL_FROM => Some(&self.from),
COL_TO => Some(&self.to),
COL_FROM_COMM => Some(&self.from_commodity),
COL_TO_COMM => Some(&self.to_commodity),
_ => None,
pub(in crate::widgets) fn select_mut(&mut self, col: usize) -> Option<&mut SelectWidget> {
COL_FROM => Some(&mut self.from),
COL_TO => Some(&mut self.to),
COL_FROM_COMM => Some(&mut self.from_commodity),
COL_TO_COMM => Some(&mut self.to_commodity),
pub(in crate::widgets) fn amount_mut(&mut self, col: usize) -> Option<&mut AmountWidget> {
COL_VALUE => Some(&mut self.value),
COL_TO_AMOUNT => Some(&mut self.to_amount),
/// An empty to-commodity means the from-commodity, so it leads the list and
/// a new row starts on it.
fn with_empty_meaning(commodities: &[SelectOption]) -> Vec<SelectOption> {
let meaning = match columns().nth(COL_TO_COMM).map(|field| field.kind) {
empty: Some(meaning),
}) => meaning,
let same = SelectOption {
id: String::new(),
label: meaning.to_string(),
std::iter::once(same)
.chain(commodities.iter().cloned())
.collect()
/// The label of a select whose option list has not arrived: a loaded id is
/// never shown.
pub(super) const PENDING_LABEL: &str = "…";
fn seed(cell: &str) -> Vec<SelectOption> {
match cell {
"" => Vec::new(),
cell => vec![SelectOption {
id: cell.to_string(),
label: PENDING_LABEL.to_string(),
}],
/// `options` themselves when one has the id `cell`, so the loaded entity is
/// selected by its option. Else (an id missing from the list) led by a seed
/// option holding the loaded `cell` text, so an untouched select submits it
/// byte for byte.
fn bound(options: &[SelectOption], cell: &str) -> Vec<SelectOption> {
if cell.is_empty() || options.iter().any(|option| option.id == cell) {
return options.to_vec();
let seed = SelectOption {
label: format!("{cell} (unchanged)"),
std::iter::once(seed)
.chain(options.iter().cloned())