1
//! The one answer to "can the builder write these rows as they stand".
2
//!
3
//! A filled-in row that no condition can be built from is a [`Fault`], never
4
//! silently left out of the text: that would run a broader report than the one
5
//! asked for. Only an empty builder (no rows at all) means no filter. A client
6
//! whose form shows an untouched placeholder row leaves it out of the items it
7
//! hands over; a blank row that does arrive here is a fault.
8

            
9
use super::{Entity, Item, Logic};
10

            
11
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12
pub enum Reason {
13
    /// A nested group holds no row.
14
    EmptyGroup,
15
    /// An `in` row holds no id at all.
16
    EmptyIn,
17
    /// An id, or a member of an `in` list, is blank.
18
    BlankId,
19
    TagWithoutEntity,
20
    TagWithoutName,
21
    TagWithoutValues,
22
    NotArity(usize),
23
    /// A row with nothing filled in.
24
    BlankRow,
25
    /// Text was typed into an account picker but no account was picked.
26
    NotPicked,
27
    /// A single-id row holds several ids.
28
    NotSingle,
29
    /// The amount is not a number.
30
    BadAmount,
31
    /// A row no condition can be built from, for a reason not listed above.
32
    Incomplete,
33
}
34

            
35
impl Reason {
36
    #[must_use]
37
5388
    pub fn describe(self) -> String {
38
5388
        match self {
39
577
            Self::EmptyGroup => "a group needs at least one row".to_owned(),
40
769
            Self::EmptyIn => "an id list needs at least one id".to_owned(),
41
385
            Self::BlankId => "an id list needs non-blank ids".to_owned(),
42
385
            Self::TagWithoutEntity => "a tag row needs an entity".to_owned(),
43
385
            Self::TagWithoutName => "a tag row needs a name".to_owned(),
44
193
            Self::TagWithoutValues => "a tag row needs a value".to_owned(),
45
577
            Self::NotArity(count) => format!("not takes exactly one condition, got {count}"),
46
577
            Self::BlankRow => "row is blank".to_owned(),
47
1153
            Self::NotPicked => "pick one from the list".to_owned(),
48
1
            Self::NotSingle => "this row takes exactly one id".to_owned(),
49
385
            Self::BadAmount => "the amount is not a number: use 5, 1/3, 0.25 or 15%".to_owned(),
50
1
            Self::Incomplete => "row is incomplete".to_owned(),
51
        }
52
5388
    }
53
}
54

            
55
/// The first refusal and where it is: `path` holds the index of each row on
56
/// the way down from the list that was checked, empty for the list itself.
57
#[derive(Debug, Clone, PartialEq, Eq)]
58
pub struct Fault {
59
    pub path: Vec<usize>,
60
    pub reason: Reason,
61
}
62

            
63
impl Fault {
64
5019
    fn at(reason: Reason) -> Self {
65
5019
        Self {
66
5019
            path: Vec::new(),
67
5019
            reason,
68
5019
        }
69
5019
    }
70

            
71
3471
    fn under(mut self, at: usize) -> Self {
72
3471
        self.path.insert(0, at);
73
3471
        self
74
3471
    }
75
}
76

            
77
6187
fn tag(entities: &[Entity], name: &str, values: &[String]) -> Result<(), Reason> {
78
6187
    match (name.trim().is_empty(), values.is_empty()) {
79
387
        (true, true) => Err(Reason::BlankRow),
80
385
        (true, false) => Err(Reason::TagWithoutName),
81
193
        (false, true) => Err(Reason::TagWithoutValues),
82
5222
        (false, false) if entities.is_empty() => Err(Reason::TagWithoutEntity),
83
4836
        (false, false) => Ok(()),
84
    }
85
6187
}
86

            
87
14270
fn blank_id(id: &str) -> Result<(), Reason> {
88
14270
    if id.trim().is_empty() {
89
1351
        return Err(Reason::BlankRow);
90
12919
    }
91
12919
    Ok(())
92
14270
}
93

            
94
7126
fn id_list(ids: &[String]) -> Result<(), Reason> {
95
7126
    if ids.is_empty() {
96
769
        return Err(Reason::EmptyIn);
97
6357
    }
98
9826
    if ids.iter().any(|id| id.trim().is_empty()) {
99
388
        return Err(Reason::BlankId);
100
5969
    }
101
5969
    Ok(())
102
7126
}
103

            
104
26797
fn logic_arity(logic: Logic, count: usize) -> Result<(), Reason> {
105
26797
    match (logic, count) {
106
2511
        (Logic::Not, 1) => Ok(()),
107
580
        (Logic::Not, count) => Err(Reason::NotArity(count)),
108
23706
        _ => Ok(()),
109
    }
110
26797
}
111

            
112
/// Checks one row or nested group.
113
///
114
/// # Errors
115
/// The first `Fault`, the path counted from `item`.
116
43374
pub fn check_item(item: &Item) -> Result<(), Fault> {
117
43374
    match item {
118
6173
        Item::Group { items, .. } if items.is_empty() => Err(Fault::at(Reason::EmptyGroup)),
119
5593
        Item::Group { logic, items } => check(*logic, items),
120
37201
        leaf => leaf_reason(leaf).map_err(Fault::at),
121
    }
122
43374
}
123

            
124
37201
fn leaf_reason(item: &Item) -> Result<(), Reason> {
125
37201
    match item {
126
        Item::Tag {
127
            entities,
128
            name,
129
            values,
130
6187
        } => tag(entities, name, values),
131
        Item::Account { id: value, .. }
132
        | Item::CounterpartyEq { id: value }
133
14270
        | Item::CommodityEq { id: value } => blank_id(value),
134
        Item::AccountIn { ids: list }
135
        | Item::CounterpartyIn { ids: list }
136
7126
        | Item::CommodityIn { ids: list } => id_list(list),
137
9618
        Item::Amount { .. } | Item::Group { .. } => Ok(()),
138
    }
139
37201
}
140

            
141
/// Checks the rows of the builder. No rows is no filter and is fine.
142
///
143
/// # Errors
144
/// The first `Fault`, the children before the list they are in.
145
30848
pub fn check(logic: Logic, items: &[Item]) -> Result<(), Fault> {
146
30848
    for (at, item) in items.iter().enumerate() {
147
38369
        check_item(item).map_err(|fault| fault.under(at))?;
148
    }
149
27377
    match items.len() {
150
580
        0 => Ok(()),
151
26797
        count => logic_arity(logic, count).map_err(Fault::at),
152
    }
153
30848
}
154

            
155
#[cfg(test)]
156
#[path = "check_tests.rs"]
157
mod tests;