1
//! Static form descriptor types.
2

            
3
use crate::transaction::PAGING_KEYS;
4

            
5
/// The cell of a nested row naming the key of its parent row.
6
pub const LINK_CELL: &str = "row";
7
/// The cell of a parent row holding the key its nested rows name.
8
pub const PARENT_KEY_CELL: &str = "key";
9

            
10
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11
pub enum Source {
12
    Accounts,
13
    Commodities,
14
    Templates,
15
    Scripts,
16
}
17

            
18
impl Source {
19
    /// The name used in descriptors, options requests and rendered forms.
20
    #[must_use]
21
412416
    pub const fn name(self) -> &'static str {
22
412416
        match self {
23
219072
            Self::Accounts => "accounts",
24
187008
            Self::Commodities => "commodities",
25
4416
            Self::Templates => "templates",
26
1920
            Self::Scripts => "scripts",
27
        }
28
412416
    }
29

            
30
    #[must_use]
31
47232
    pub fn by_name(name: &str) -> Option<Self> {
32
47232
        [
33
47232
            Self::Accounts,
34
47232
            Self::Commodities,
35
47232
            Self::Templates,
36
47232
            Self::Scripts,
37
47232
        ]
38
47232
        .into_iter()
39
76224
        .find(|source| source.name() == name)
40
47232
    }
41
}
42

            
43
/// Free-text suggestions a text cell offers, served by `form-options` as
44
/// plain strings rather than picker options.
45
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46
pub enum Suggest {
47
    TagNames(Scope),
48
    /// The values of the tag of this scope named by the sibling `name` cell.
49
    TagValues(Scope),
50
}
51

            
52
impl Suggest {
53
    /// The name used in descriptors and options requests.
54
    #[must_use]
55
731520
    pub const fn name(self) -> &'static str {
56
731520
        match self {
57
108480
            Self::TagNames(Scope::Transaction) => "transaction-tag-names",
58
150144
            Self::TagNames(Scope::Split) => "split-tag-names",
59
60864
            Self::TagNames(Scope::Account) => "account-tag-names",
60
57792
            Self::TagNames(Scope::Commodity) => "commodity-tag-names",
61
104448
            Self::TagValues(Scope::Transaction) => "transaction-tag-values",
62
133632
            Self::TagValues(Scope::Split) => "split-tag-values",
63
59136
            Self::TagValues(Scope::Account) => "account-tag-values",
64
57024
            Self::TagValues(Scope::Commodity) => "commodity-tag-values",
65
        }
66
731520
    }
67

            
68
    #[must_use]
69
55296
    pub fn by_name(name: &str) -> Option<Self> {
70
55296
        Scope::ALL
71
55296
            .into_iter()
72
205248
            .flat_map(|scope| [Self::TagNames(scope), Self::TagValues(scope)])
73
406848
            .find(|suggest| suggest.name() == name)
74
55296
    }
75
}
76

            
77
/// The kind of entity whose tags a text cell suggests.
78
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79
pub enum Scope {
80
    Transaction,
81
    Split,
82
    Account,
83
    Commodity,
84
}
85

            
86
impl Scope {
87
    const ALL: [Self; 4] = [
88
        Self::Transaction,
89
        Self::Split,
90
        Self::Account,
91
        Self::Commodity,
92
    ];
93
}
94

            
95
/// The seven field kinds; another needs owner sign-off.
96
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97
pub enum Kind {
98
    Text {
99
        multiline: bool,
100
        suggest: Option<Suggest>,
101
    },
102
    Select {
103
        source: Source,
104
        /// The cell of the same row a pick fills when it is empty, from the
105
        /// `:commodity` the picked option carries.
106
        fills: Option<&'static str>,
107
        /// What an empty cell means, shown by clients as its hint.
108
        empty: Option<&'static str>,
109
    },
110
    Amount,
111
    /// `whole_day` asks for a calendar day, which `range_end` turns into the
112
    /// last instant of that day; otherwise an instant is asked for.
113
    Date {
114
        range_end: bool,
115
        whole_day: bool,
116
    },
117
    /// A `nested_under` rows field belongs to the rows field of that key: its
118
    /// [`LINK_CELL`] holds the [`PARENT_KEY_CELL`] value of the owning row.
119
    Rows {
120
        fields: &'static [Field],
121
        nested_under: Option<&'static str>,
122
    },
123
    Hidden,
124
    /// A fixed list of `(key, label)` options; the key is submitted.
125
    Choice {
126
        options: &'static [(&'static str, &'static str)],
127
        default: Option<&'static str>,
128
    },
129
}
130

            
131
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132
pub struct Field {
133
    pub key: &'static str,
134
    pub label: &'static str,
135
    pub kind: Kind,
136
    pub required: bool,
137
}
138

            
139
impl Field {
140
    #[must_use]
141
18672192
    pub const fn nested_under(&self) -> Option<&'static str> {
142
18672192
        match self.kind {
143
8330688
            Kind::Rows { nested_under, .. } => nested_under,
144
10341504
            _ => None,
145
        }
146
18672192
    }
147

            
148
    #[must_use]
149
2149248
    pub const fn is_nested(&self) -> bool {
150
2149248
        self.nested_under().is_some()
151
2149248
    }
152

            
153
    /// Whether the field pages a result instead of filtering it.
154
    #[must_use]
155
639360
    pub fn is_paging(&self) -> bool {
156
639360
        PAGING_KEYS.contains(&self.key)
157
639360
    }
158
}
159

            
160
pub(crate) const fn field(
161
    key: &'static str,
162
    label: &'static str,
163
    kind: Kind,
164
    required: bool,
165
) -> Field {
166
    Field {
167
        key,
168
        label,
169
        kind,
170
        required,
171
    }
172
}
173

            
174
pub(crate) const TEXT: Kind = Kind::Text {
175
    multiline: false,
176
    suggest: None,
177
};
178
pub(crate) const ACCOUNT: Kind = Kind::Select {
179
    source: Source::Accounts,
180
    fills: None,
181
    empty: None,
182
};
183
pub(crate) const COMMODITY: Kind = Kind::Select {
184
    source: Source::Commodities,
185
    fills: None,
186
    empty: None,
187
};
188

            
189
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
190
pub enum Action {
191
    Mutation,
192
    Query,
193
    LoadMutation,
194
}
195

            
196
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
197
pub struct Form {
198
    pub name: &'static str,
199
    pub title: &'static str,
200
    pub fields: &'static [Field],
201
    pub action: Action,
202
}
203

            
204
166464
fn has_source(fields: &[Field], source: Source) -> bool {
205
850752
    fields.iter().any(|field| match field.kind {
206
149376
        Kind::Select { source: own, .. } => own == source,
207
99648
        Kind::Rows { fields, .. } => has_source(fields, source),
208
601728
        _ => false,
209
850752
    })
210
166464
}
211

            
212
110208
fn has_select(fields: &[Field]) -> bool {
213
285696
    fields.iter().any(|field| match field.kind {
214
50880
        Kind::Select { .. } => true,
215
48192
        Kind::Rows { fields, .. } => has_select(fields),
216
186624
        _ => false,
217
285696
    })
218
110208
}
219

            
220
impl Form {
221
    #[must_use]
222
62016
    pub fn has_select(&self) -> bool {
223
62016
        has_select(self.fields)
224
62016
    }
225

            
226
    #[must_use]
227
66816
    pub fn has_source(&self, source: Source) -> bool {
228
66816
        has_source(self.fields, source)
229
66816
    }
230

            
231
    /// The fields that filter a result: all but the paging ones.
232
18240
    pub fn filter_fields(&self) -> impl Iterator<Item = &'static Field> {
233
18615
        self.fields.iter().filter(|field| !field.is_paging())
234
18240
    }
235

            
236
18048
    pub fn nested_keys(&self) -> impl Iterator<Item = &'static str> {
237
18048
        self.fields
238
18048
            .iter()
239
1700
            .filter(|field| field.is_nested())
240
208
            .map(|field| field.key)
241
18048
    }
242
}