Lines
100 %
Functions
75 %
Branches
use forms::{Field, Row, Source};
use super::{Cell, References};
/// The converted amount of a split is needed only when its commodities differ.
pub(super) struct Conversion {
rows: &'static str,
amount: &'static str,
from: &'static str,
to: &'static str,
}
pub(super) const CONVERSION: Conversion = Conversion {
rows: "splits",
amount: "to-amount",
from: "from-commodity",
to: "to-commodity",
};
impl Conversion {
/// Whether the row names two commodities; texts the `references` cannot
/// resolve are compared as written.
pub(super) fn differs(&self, row: Option<&Row>, references: &References) -> bool {
let get = |key: &str| row.and_then(|r| r.get(key)).map_or("", String::as_str);
let (from, to) = (get(self.from).trim(), get(self.to).trim());
if from.is_empty() || to.is_empty() {
return false;
let id = |text| {
references
.resolve(Source::Commodities, text)
.map(|option| option.id)
id(from).zip(id(to)).map_or(from != to, |(a, b)| a != b)
/// The fields of a row in the order a person tabs through them: a split
/// states its value before the commodity it is received in.
pub(super) fn flow<'f>(&self, rows: &str, fields: &'f [Field]) -> Vec<&'f Field> {
let mut ordered: Vec<&Field> = fields.iter().collect();
let at = |key| ordered.iter().position(|f| f.key == key);
if let (true, Some(to), Some(value)) = (rows == self.rows, at(self.to), at("value")) {
ordered.swap(to, value);
ordered
pub(super) fn mark(&self, rows: &str, key: &str, cell: &mut Cell, differs: bool) {
let Cell::Control(control) = cell else { return };
if (rows, key) == (self.rows, self.amount) {
control.converts = Some((self.from, self.to));
control.dormant = !differs && control.errors.is_empty();
if control.dormant {
control.class = "field hidden-field";