Lines
100 %
Functions
Branches
//! The one answer to "can the builder write these rows as they stand".
//!
//! A filled-in row that no condition can be built from is a [`Fault`], never
//! silently left out of the text: that would run a broader report than the one
//! asked for. Only an empty builder (no rows at all) means no filter. A client
//! whose form shows an untouched placeholder row leaves it out of the items it
//! hands over; a blank row that does arrive here is a fault.
use super::{Entity, Item, Logic};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Reason {
/// A nested group holds no row.
EmptyGroup,
/// An `in` row holds no id at all.
EmptyIn,
/// An id, or a member of an `in` list, is blank.
BlankId,
TagWithoutEntity,
TagWithoutName,
TagWithoutValues,
NotArity(usize),
/// A row with nothing filled in.
BlankRow,
/// Text was typed into an account picker but no account was picked.
NotPicked,
/// A single-id row holds several ids.
NotSingle,
/// The amount is not a number.
BadAmount,
/// A row no condition can be built from, for a reason not listed above.
Incomplete,
}
impl Reason {
#[must_use]
pub fn describe(self) -> String {
match self {
Self::EmptyGroup => "a group needs at least one row".to_owned(),
Self::EmptyIn => "an id list needs at least one id".to_owned(),
Self::BlankId => "an id list needs non-blank ids".to_owned(),
Self::TagWithoutEntity => "a tag row needs an entity".to_owned(),
Self::TagWithoutName => "a tag row needs a name".to_owned(),
Self::TagWithoutValues => "a tag row needs a value".to_owned(),
Self::NotArity(count) => format!("not takes exactly one condition, got {count}"),
Self::BlankRow => "row is blank".to_owned(),
Self::NotPicked => "pick one from the list".to_owned(),
Self::NotSingle => "this row takes exactly one id".to_owned(),
Self::BadAmount => "the amount is not a number: use 5, 1/3, 0.25 or 15%".to_owned(),
Self::Incomplete => "row is incomplete".to_owned(),
/// The first refusal and where it is: `path` holds the index of each row on
/// the way down from the list that was checked, empty for the list itself.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Fault {
pub path: Vec<usize>,
pub reason: Reason,
impl Fault {
fn at(reason: Reason) -> Self {
Self {
path: Vec::new(),
reason,
fn under(mut self, at: usize) -> Self {
self.path.insert(0, at);
self
fn tag(entities: &[Entity], name: &str, values: &[String]) -> Result<(), Reason> {
match (name.trim().is_empty(), values.is_empty()) {
(true, true) => Err(Reason::BlankRow),
(true, false) => Err(Reason::TagWithoutName),
(false, true) => Err(Reason::TagWithoutValues),
(false, false) if entities.is_empty() => Err(Reason::TagWithoutEntity),
(false, false) => Ok(()),
fn blank_id(id: &str) -> Result<(), Reason> {
if id.trim().is_empty() {
return Err(Reason::BlankRow);
Ok(())
fn id_list(ids: &[String]) -> Result<(), Reason> {
if ids.is_empty() {
return Err(Reason::EmptyIn);
if ids.iter().any(|id| id.trim().is_empty()) {
return Err(Reason::BlankId);
fn logic_arity(logic: Logic, count: usize) -> Result<(), Reason> {
match (logic, count) {
(Logic::Not, 1) => Ok(()),
(Logic::Not, count) => Err(Reason::NotArity(count)),
_ => Ok(()),
/// Checks one row or nested group.
///
/// # Errors
/// The first `Fault`, the path counted from `item`.
pub fn check_item(item: &Item) -> Result<(), Fault> {
match item {
Item::Group { items, .. } if items.is_empty() => Err(Fault::at(Reason::EmptyGroup)),
Item::Group { logic, items } => check(*logic, items),
leaf => leaf_reason(leaf).map_err(Fault::at),
fn leaf_reason(item: &Item) -> Result<(), Reason> {
Item::Tag {
entities,
name,
values,
} => tag(entities, name, values),
Item::Account { id: value, .. }
| Item::CounterpartyEq { id: value }
| Item::CommodityEq { id: value } => blank_id(value),
Item::AccountIn { ids: list }
| Item::CounterpartyIn { ids: list }
| Item::CommodityIn { ids: list } => id_list(list),
Item::Amount { .. } | Item::Group { .. } => Ok(()),
/// Checks the rows of the builder. No rows is no filter and is fine.
/// The first `Fault`, the children before the list they are in.
pub fn check(logic: Logic, items: &[Item]) -> Result<(), Fault> {
for (at, item) in items.iter().enumerate() {
check_item(item).map_err(|fault| fault.under(at))?;
match items.len() {
0 => Ok(()),
count => logic_arity(logic, count).map_err(Fault::at),
#[cfg(test)]
#[path = "check_tests.rs"]
mod tests;