1use renox::serde_json::Value;
7use serde::Serialize;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
11#[serde(rename_all = "snake_case")]
12#[non_exhaustive]
13pub enum FieldKind {
14 Text,
16 Email,
18 Password,
20 Url,
22 Tel,
24 Textarea,
26 Number,
28 Money,
31 Date,
33 DateTime,
36 Select,
38 Checkbox,
40 Toggle,
42 BelongsTo,
44}
45
46#[derive(Debug, Clone, Serialize)]
65#[non_exhaustive]
66pub struct Field {
67 name: String,
68 label: String,
69 kind: FieldKind,
70 options: Vec<(String, String)>,
71 required: bool,
72 hint: Option<String>,
73 placeholder: Option<String>,
74 prefix: Option<String>,
75 suffix: Option<String>,
76 span: Option<String>,
77 rows: u32,
78 step: Option<String>,
79 min: Option<String>,
80 max: Option<String>,
81 autocomplete: Option<String>,
82 searchable: bool,
83 default: Option<Value>,
84 on_create: bool,
85 on_edit: bool,
86 readonly_on_edit: bool,
87 #[serde(skip)]
88 relation: Option<(String, String)>,
89}
90
91impl Field {
92 fn new(name: &str, label: &str, kind: FieldKind) -> Self {
93 Self {
94 name: name.to_owned(),
95 label: label.to_owned(),
96 kind,
97 options: Vec::new(),
98 required: false,
99 hint: None,
100 placeholder: None,
101 prefix: None,
102 suffix: None,
103 span: None,
104 rows: 4,
105 step: None,
106 min: None,
107 max: None,
108 autocomplete: None,
109 searchable: false,
110 default: None,
111 on_create: true,
112 on_edit: true,
113 readonly_on_edit: false,
114 relation: None,
115 }
116 }
117
118 pub fn text(name: &str, label: &str) -> Self {
120 Self::new(name, label, FieldKind::Text)
121 }
122
123 pub fn email(name: &str, label: &str) -> Self {
125 Self::new(name, label, FieldKind::Email)
126 }
127
128 pub fn password(name: &str, label: &str) -> Self {
131 let mut field = Self::new(name, label, FieldKind::Password);
132 field.autocomplete = Some("new-password".into());
133 field
134 }
135
136 pub fn url(name: &str, label: &str) -> Self {
138 Self::new(name, label, FieldKind::Url)
139 }
140
141 pub fn tel(name: &str, label: &str) -> Self {
143 Self::new(name, label, FieldKind::Tel)
144 }
145
146 pub fn textarea(name: &str, label: &str) -> Self {
148 Self::new(name, label, FieldKind::Textarea)
149 }
150
151 pub fn number(name: &str, label: &str) -> Self {
153 Self::new(name, label, FieldKind::Number)
154 }
155
156 pub fn money(name: &str, label: &str) -> Self {
164 let mut field = Self::new(name, label, FieldKind::Money);
165 field.min = Some("0".into());
166 field
167 }
168
169 pub fn date(name: &str, label: &str) -> Self {
171 Self::new(name, label, FieldKind::Date)
172 }
173
174 pub fn datetime(name: &str, label: &str) -> Self {
178 Self::new(name, label, FieldKind::DateTime)
179 }
180
181 pub fn select<V: Into<String>, L: Into<String>>(
184 name: &str,
185 label: &str,
186 options: impl IntoIterator<Item = (V, L)>,
187 ) -> Self {
188 let mut field = Self::new(name, label, FieldKind::Select);
189 field.options = options
190 .into_iter()
191 .map(|(value, label)| (value.into(), label.into()))
192 .collect();
193 field
194 }
195
196 pub fn checkbox(name: &str, label: &str) -> Self {
198 Self::new(name, label, FieldKind::Checkbox)
199 }
200
201 pub fn toggle(name: &str, label: &str) -> Self {
203 Self::new(name, label, FieldKind::Toggle)
204 }
205
206 pub fn belongs_to(name: &str, label: &str, table: &str, title: &str) -> Self {
211 let mut field = Self::new(name, label, FieldKind::BelongsTo);
212 field.relation = Some((table.to_owned(), title.to_owned()));
213 field.searchable = true;
214 field
215 }
216
217 pub fn required(mut self) -> Self {
220 self.required = true;
221 self
222 }
223
224 pub fn hint(mut self, hint: &str) -> Self {
226 self.hint = Some(hint.to_owned());
227 self
228 }
229
230 pub fn placeholder(mut self, placeholder: &str) -> Self {
232 self.placeholder = Some(placeholder.to_owned());
233 self
234 }
235
236 pub fn prefix(mut self, prefix: &str) -> Self {
238 self.prefix = Some(prefix.to_owned());
239 self
240 }
241
242 pub fn suffix(mut self, suffix: &str) -> Self {
244 self.suffix = Some(suffix.to_owned());
245 self
246 }
247
248 pub fn span_full(mut self) -> Self {
250 self.span = Some("full".into());
251 self
252 }
253
254 pub fn rows(mut self, rows: u32) -> Self {
256 self.rows = rows;
257 self
258 }
259
260 pub fn step(mut self, step: &str) -> Self {
262 self.step = Some(step.to_owned());
263 self
264 }
265
266 pub fn min(mut self, min: impl ToString) -> Self {
268 self.min = Some(min.to_string());
269 self
270 }
271
272 pub fn max(mut self, max: impl ToString) -> Self {
274 self.max = Some(max.to_string());
275 self
276 }
277
278 pub fn autocomplete(mut self, autocomplete: &str) -> Self {
280 self.autocomplete = Some(autocomplete.to_owned());
281 self
282 }
283
284 pub fn searchable(mut self) -> Self {
286 self.searchable = true;
287 self
288 }
289
290 pub fn default_value(mut self, value: impl Serialize) -> Self {
293 self.default = renox::serde_json::to_value(value).ok();
294 self
295 }
296
297 pub fn only_on_create(mut self) -> Self {
299 self.on_edit = false;
300 self
301 }
302
303 pub fn only_on_edit(mut self) -> Self {
305 self.on_create = false;
306 self
307 }
308
309 pub fn readonly_on_edit(mut self) -> Self {
312 self.readonly_on_edit = true;
313 self
314 }
315
316 pub fn name(&self) -> &str {
318 &self.name
319 }
320
321 pub fn kind(&self) -> FieldKind {
323 self.kind
324 }
325
326 pub(crate) fn shown(&self, creating: bool) -> bool {
327 if creating {
328 self.on_create
329 } else {
330 self.on_edit
331 }
332 }
333
334 pub(crate) fn relation(&self) -> Option<(&str, &str)> {
335 self.relation
336 .as_ref()
337 .map(|(table, title)| (table.as_str(), title.as_str()))
338 }
339
340 pub(crate) fn set_options(&mut self, options: Vec<(String, String)>) {
341 self.options = options;
342 }
343
344 pub(crate) fn label_text(&self) -> &str {
345 &self.label
346 }
347
348 pub(crate) fn has_placeholder(&self) -> bool {
349 self.placeholder.is_some()
350 }
351
352 pub(crate) fn is_required(&self) -> bool {
353 self.required
354 }
355
356 pub(crate) fn min_text(&self) -> Option<&str> {
357 self.min.as_deref()
358 }
359
360 pub(crate) fn max_text(&self) -> Option<&str> {
361 self.max.as_deref()
362 }
363
364 pub(crate) fn choices(&self) -> &[(String, String)] {
365 &self.options
366 }
367
368 pub(crate) fn translate(&mut self, translate: &dyn Fn(&str) -> String) {
371 self.label = translate(&self.label);
372 for text in [
373 &mut self.hint,
374 &mut self.placeholder,
375 &mut self.prefix,
376 &mut self.suffix,
377 ]
378 .into_iter()
379 .flatten()
380 {
381 *text = translate(text);
382 }
383 for (_, label) in &mut self.options {
384 *label = translate(label);
385 }
386 }
387}
388
389pub(crate) fn plain_name(name: &str) -> bool {
391 !name.is_empty()
392 && name.len() <= 64
393 && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
394 && !name.starts_with(|c: char| c.is_ascii_digit())
395}