1use crate::transaction::PAGING_KEYS;
4
5pub const LINK_CELL: &str = "row";
7pub const PARENT_KEY_CELL: &str = "key";
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Source {
12 Accounts,
13 Commodities,
14 Templates,
15 Scripts,
16}
17
18impl Source {
19 #[must_use]
21 pub const fn name(self) -> &'static str {
22 match self {
23 Self::Accounts => "accounts",
24 Self::Commodities => "commodities",
25 Self::Templates => "templates",
26 Self::Scripts => "scripts",
27 }
28 }
29
30 #[must_use]
31 pub fn by_name(name: &str) -> Option<Self> {
32 [
33 Self::Accounts,
34 Self::Commodities,
35 Self::Templates,
36 Self::Scripts,
37 ]
38 .into_iter()
39 .find(|source| source.name() == name)
40 }
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46pub enum Suggest {
47 TagNames(Scope),
48 TagValues(Scope),
50}
51
52impl Suggest {
53 #[must_use]
55 pub const fn name(self) -> &'static str {
56 match self {
57 Self::TagNames(Scope::Transaction) => "transaction-tag-names",
58 Self::TagNames(Scope::Split) => "split-tag-names",
59 Self::TagNames(Scope::Account) => "account-tag-names",
60 Self::TagNames(Scope::Commodity) => "commodity-tag-names",
61 Self::TagValues(Scope::Transaction) => "transaction-tag-values",
62 Self::TagValues(Scope::Split) => "split-tag-values",
63 Self::TagValues(Scope::Account) => "account-tag-values",
64 Self::TagValues(Scope::Commodity) => "commodity-tag-values",
65 }
66 }
67
68 #[must_use]
69 pub fn by_name(name: &str) -> Option<Self> {
70 Scope::ALL
71 .into_iter()
72 .flat_map(|scope| [Self::TagNames(scope), Self::TagValues(scope)])
73 .find(|suggest| suggest.name() == name)
74 }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79pub enum Scope {
80 Transaction,
81 Split,
82 Account,
83 Commodity,
84}
85
86impl Scope {
87 const ALL: [Self; 4] = [
88 Self::Transaction,
89 Self::Split,
90 Self::Account,
91 Self::Commodity,
92 ];
93}
94
95#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97pub enum Kind {
98 Text {
99 multiline: bool,
100 suggest: Option<Suggest>,
101 },
102 Select {
103 source: Source,
104 fills: Option<&'static str>,
107 empty: Option<&'static str>,
109 },
110 Amount,
111 Date {
114 range_end: bool,
115 whole_day: bool,
116 },
117 Rows {
120 fields: &'static [Field],
121 nested_under: Option<&'static str>,
122 },
123 Hidden,
124 Choice {
126 options: &'static [(&'static str, &'static str)],
127 default: Option<&'static str>,
128 },
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub struct Field {
133 pub key: &'static str,
134 pub label: &'static str,
135 pub kind: Kind,
136 pub required: bool,
137}
138
139impl Field {
140 #[must_use]
141 pub const fn nested_under(&self) -> Option<&'static str> {
142 match self.kind {
143 Kind::Rows { nested_under, .. } => nested_under,
144 _ => None,
145 }
146 }
147
148 #[must_use]
149 pub const fn is_nested(&self) -> bool {
150 self.nested_under().is_some()
151 }
152
153 #[must_use]
155 pub fn is_paging(&self) -> bool {
156 PAGING_KEYS.contains(&self.key)
157 }
158}
159
160pub(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
174pub(crate) const TEXT: Kind = Kind::Text {
175 multiline: false,
176 suggest: None,
177};
178pub(crate) const ACCOUNT: Kind = Kind::Select {
179 source: Source::Accounts,
180 fills: None,
181 empty: None,
182};
183pub(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)]
190pub enum Action {
191 Mutation,
192 Query,
193 LoadMutation,
194}
195
196#[derive(Debug, Clone, Copy, PartialEq, Eq)]
197pub struct Form {
198 pub name: &'static str,
199 pub title: &'static str,
200 pub fields: &'static [Field],
201 pub action: Action,
202}
203
204fn has_source(fields: &[Field], source: Source) -> bool {
205 fields.iter().any(|field| match field.kind {
206 Kind::Select { source: own, .. } => own == source,
207 Kind::Rows { fields, .. } => has_source(fields, source),
208 _ => false,
209 })
210}
211
212fn has_select(fields: &[Field]) -> bool {
213 fields.iter().any(|field| match field.kind {
214 Kind::Select { .. } => true,
215 Kind::Rows { fields, .. } => has_select(fields),
216 _ => false,
217 })
218}
219
220impl Form {
221 #[must_use]
222 pub fn has_select(&self) -> bool {
223 has_select(self.fields)
224 }
225
226 #[must_use]
227 pub fn has_source(&self, source: Source) -> bool {
228 has_source(self.fields, source)
229 }
230
231 pub fn filter_fields(&self) -> impl Iterator<Item = &'static Field> {
233 self.fields.iter().filter(|field| !field.is_paging())
234 }
235
236 pub fn nested_keys(&self) -> impl Iterator<Item = &'static str> {
237 self.fields
238 .iter()
239 .filter(|field| field.is_nested())
240 .map(|field| field.key)
241 }
242}