tablo-core 0.8.0

The core toolkit types for Tablo, a server-rendered admin toolkit on Topcoat and Toasty.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Unified Schema primitive — fields and layout blocks that compose via `view!`.
//!
//! `Schema` holds layout blocks, embedded values, and [`Field`] slots and resolves every field once
//! into one list for rendering and validation.

mod comparison;
mod condition;
pub(crate) mod embedded;
pub(crate) mod fields;
pub(crate) mod input;
mod layouts;
mod lenses;
mod mask;
mod options;
mod read;
pub(crate) mod relationship;
pub(crate) mod repeater;
pub(crate) mod tree;

use std::{collections::HashSet, marker::PhantomData};

pub use condition::Watched;
pub use embedded::EmbeddedForm;
use fields::ChoiceControl;
#[cfg(feature = "rich-text")]
pub use fields::RichTextField;
pub use fields::{
    ChoiceField, CustomField, Field, FileField, IntoFormValue, IntoOptions, RepeaterField, Switch,
    TextField, Toggle,
};
pub(crate) use fields::{
    MAX_FORM_BYTES,
    file::{FileHref, file_href},
    is_linkable, is_rooted, option_view, read_only, stored_upload, value_cell,
};
pub(crate) use input::FormInput;
pub use layouts::{Grid, Group, HoldsTabs, Section, Tab, Tabs};
pub(crate) use layouts::{tab_id, tab_view, tabs_chrome};
pub(crate) use lenses::Binding;
pub use lenses::form_key;
pub(crate) use mask::FormMask;
pub use options::Options;
pub use relationship::MAX_RELATIONSHIP_OPTIONS;
pub(crate) use relationship::{
    OptionLoadError, OptionSource, clamp_query_term, ensure_option_access,
};
pub use repeater::{MAX_REPEATER_ROWS, parse_items, write_items};
use topcoat::{Result, context::Cx, view::*};
pub use tree::{IntoSchema, Source};
pub(crate) use tree::{Node, Retype, render_nodes};

use crate::values::{FieldErrors, FormValue, FormValues};

/// Composes fields and layout blocks and resolves every field once into one list.
///
/// `F` is the record form whose controls the schema places. A resource's form is a
/// `Schema<UserForm>`, which only `UserForm::controls()` fill, so a control from another form or a
/// field built with [`Field::text`] fails to compile there. A page's or an action's schema is a
/// plain `Schema`, which [`Field`]'s constructors fill.
pub struct Schema<F = ()> {
    pub(crate) nodes: Vec<Node>,
    pub(crate) fields: Vec<Field>,
    form: PhantomData<fn() -> F>,
}

impl<F> Default for Schema<F> {
    fn default() -> Self {
        Self::from_parts(Vec::new(), Vec::new())
    }
}

impl<F> std::fmt::Debug for Schema<F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Schema")
            .field("nodes", &self.nodes)
            .field("fields", &self.fields)
            .finish()
    }
}

impl Schema {
    /// A schema with no field: a page's or an action's that renders nothing. A resource's form
    /// that places nothing is `Schema::default()`, which renders every control.
    pub fn empty() -> Self {
        Self::default()
    }
}

impl<F> Retype for Schema<F> {
    type As<G> = Schema<G>;

    fn retype<G>(self) -> Schema<G> {
        Schema::from_parts(self.nodes, self.fields)
    }
}

impl<F> Schema<F> {
    pub(crate) fn from_parts(nodes: Vec<Node>, fields: Vec<Field>) -> Self {
        Self {
            nodes,
            fields,
            form: PhantomData,
        }
    }

    /// Reports whether this schema declares nothing to render.
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Builds a `Schema` from any `IntoSchema` and reports duplicate field names as declaration
    /// errors.
    pub fn new(children: impl IntoSchema<F>) -> Self {
        children.into_schema()
    }

    /// Every field, in the order it joins the schema: a layout's fields as it composes, in
    /// declaration order.
    pub fn fields(&self) -> impl Iterator<Item = &Field> {
        self.fields.iter()
    }

    /// Renders each control required exactly when `required` names its key.
    pub(crate) fn require(&mut self, required: &HashSet<&str>) {
        for field in &mut self.fields {
            let key = required.contains(field.name());
            field.set_required(key);
        }
    }

    /// Renders the schema from `source` and fails with declaration errors instead of rendering.
    ///
    /// # Errors
    ///
    /// A misdeclared schema fails with its errors rather than render.
    pub async fn render<'a>(&self, cx: &'a Cx, source: Source<'_>) -> Result<BoxView<'a>> {
        let errors = self.declaration_errors();
        if !errors.is_empty() {
            return Err(crate::error::misdeclared(&errors));
        }
        let source = source.watching(self.watched());
        render_nodes(cx, &self.nodes, &self.fields, &source).await
    }

    /// Appends `other`'s nodes and fields after this one's, re-slotting its field slots.
    pub(crate) fn append(&mut self, other: Schema<F>) {
        let Schema {
            mut nodes, fields, ..
        } = other;
        let offset = self.fields.len();
        for node in &mut nodes {
            node.offset(offset);
        }
        self.fields.extend(fields);
        self.nodes.extend(nodes);
    }

    /// The embedded node a derived value's schema holds.
    pub(crate) fn embedded_root(&self) -> &embedded::Embedded {
        match self.nodes.as_slice() {
            [Node::Embedded(node)] => node,
            _ => panic!("an embedded value's schema is its one embedded node"),
        }
    }

    /// The create form's initial values: each field's `default`.
    pub(crate) fn defaults(&self) -> FormValues {
        self.fields
            .iter()
            .filter_map(|field| Some((field.name().to_string(), field.default_value()?.clone())))
            .collect()
    }

    /// The `default` of each field `keys` names that declares one.
    pub(crate) fn defaults_of<'k>(&self, keys: impl Iterator<Item = &'k str>) -> FormValues {
        let keys: Vec<&str> = keys.collect();
        self.fields
            .iter()
            .filter(|field| keys.contains(&field.name()))
            .filter_map(|field| Some((field.name().to_string(), field.default_value()?.clone())))
            .collect()
    }

    /// Drops what a disabled control posted, which the browser never sends: on the edit form when
    /// `editing`, else on the create form, where the field takes its `default`. Run it before
    /// [`condition_hidden`](Self::condition_hidden), which drops a hidden field's default again.
    pub(crate) fn drop_disabled(&self, values: &mut FormValues, editing: bool) {
        for field in self
            .fields
            .iter()
            .filter(|field| field.is_disabled(editing))
        {
            let name = field.name();
            values.remove(name);
            if let Some(default) = field.default_value()
                && !editing
            {
                values.insert(name, default.clone());
            }
        }
    }

    /// The keys of the controls disabled on the edit form when `editing`, else the create form.
    pub(crate) fn disabled_keys(&self, editing: bool) -> impl Iterator<Item = &str> {
        self.fields
            .iter()
            .filter(move |field| field.is_disabled(editing))
            .map(Field::name)
    }

    /// Appends another schema's nodes after this one's.
    pub fn extend(mut self, other: Schema<F>) -> Schema<F> {
        self.append(other);
        self
    }

    /// Returns the first key in `errors` that no control renders a message under: neither a
    /// field's own key nor, for a repeater, a row's key under `{key}.`.
    pub(crate) fn unplaced_error<'e>(&self, errors: &'e crate::FieldErrors) -> Option<&'e str> {
        errors.iter().map(|error| error.key.as_str()).find(|key| {
            !self
                .fields
                .iter()
                .any(|field| match key.strip_prefix(field.name()) {
                    Some(rest) => {
                        rest.is_empty() || (rest.starts_with('.') && field.as_repeater().is_some())
                    }
                    None => false,
                })
        })
    }

    /// One [`EmptyChoice`](crate::DeclarationErrorKind::EmptyChoice) per choice with neither
    /// options nor a relationship. Mounting checks a resource's form and its actions' inputs; a
    /// page's schema may build its options from data that is empty for now, so rendering does
    /// not.
    pub(crate) fn empty_choices(&self) -> Vec<crate::DeclarationErrorKind> {
        self.fields
            .iter()
            .filter(|field| field.offers_nothing())
            .map(|field| crate::DeclarationErrorKind::EmptyChoice {
                field: field.name().to_string(),
            })
            .collect()
    }

    /// What is wrong with the schema's dependent choices: a column of another model than the
    /// relationship's source, or no relationship, and a parent the schema does not place, which
    /// posts nothing to narrow the options by.
    fn dependent_errors(&self) -> Vec<crate::DeclarationErrorKind> {
        let mut errors = Vec::new();
        for field in &self.fields {
            let Some(choice) = field.as_choice() else {
                continue;
            };
            if choice.misdeclared_parent() {
                errors.push(crate::DeclarationErrorKind::MisdeclaredDependentChoice {
                    field: field.name().to_string(),
                });
            }
            let Some(parent) = choice.parent_key() else {
                continue;
            };
            match self.fields.iter().find(|placed| placed.name() == parent) {
                None => errors.push(crate::DeclarationErrorKind::UnplacedParentField {
                    field: field.name().to_string(),
                    parent: parent.to_string(),
                }),
                Some(parent)
                    if choice.is_multiple()
                        || parent.as_choice().is_some_and(ChoiceControl::is_multiple) =>
                {
                    errors.push(crate::DeclarationErrorKind::MultipleDependentChoice {
                        field: field.name().to_string(),
                    });
                }
                Some(_) if choice.is_listed() => {
                    errors.push(crate::DeclarationErrorKind::ListedDependentChoice {
                        field: field.name().to_string(),
                    });
                }
                Some(_) => {}
            }
        }
        errors
    }

    /// Each key a condition, a dependent choice or a comparison watches, with the key of a field
    /// it drives: `None` for a condition guarding no field, such as one over an empty section.
    pub(crate) fn watchers(&self) -> Vec<(String, Option<String>)> {
        let mut watchers = self.condition_watchers();
        watchers.extend(self.comparison_watchers());
        watchers.extend(self.fields.iter().filter_map(|field| {
            field
                .parent_key()
                .map(|parent| (parent.to_string(), Some(field.name().to_string())))
        }));
        watchers
    }

    /// Reports what is wrong with this declaration: a field whose lens binds no single column, and
    /// two fields sharing a name.
    pub fn declaration_errors(&self) -> Vec<crate::DeclarationErrorKind> {
        let mut errors = self.condition_errors();
        errors.extend(self.dependent_errors());
        errors.extend(self.comparison_errors(""));
        // A control disabled on create posts nothing there, so the create needs a value.
        for field in &self.fields {
            errors.extend(field.rule_errors(field.name()));
            if let Some(repeater) = field.as_repeater() {
                let item = repeater.item_schema();
                for leaf in item.fields() {
                    errors.extend(leaf.rule_errors(&format!("{}.{}", field.name(), leaf.name())));
                }
                errors.extend(item.comparison_errors(&format!("{}.", field.name())));
            }
            let blank = field.default_value().is_none_or(|value| match value {
                FormValue::Text(text) => text.trim().is_empty(),
                FormValue::List(items) => items.is_empty(),
                FormValue::Rows(rows) => rows.is_empty(),
            });
            if field.is_always_disabled() && field.is_required() && blank {
                errors.push(crate::DeclarationErrorKind::DisabledWithoutValue {
                    field: field.name().to_string(),
                });
            }
            if let Some(value) = field.default_value()
                && field.refuses(value)
            {
                errors.push(crate::DeclarationErrorKind::UnpostedDefault {
                    field: field.name().to_string(),
                    value: match value {
                        FormValue::Text(text) => text.clone(),
                        FormValue::List(items) => format!("{items:?}"),
                        FormValue::Rows(_) => "rows".to_string(),
                    },
                });
            }
            // A row's controls post under the repeater's key, which neither the disabled drop nor
            // the defaults read.
            if let Some(repeater) = field.as_repeater()
                && let Some(leaf) = repeater
                    .item_schema()
                    .fields()
                    .find(|leaf| leaf.is_disabled(true) || leaf.default_value().is_some())
            {
                errors.push(crate::DeclarationErrorKind::RepeaterLeafModifier {
                    field: field.name().to_string(),
                    leaf: leaf.name().to_string(),
                });
            }
            // An edit renders each stored row, and a password never renders back: every save
            // would replace the stored secret with what the user retyped, or with nothing.
            if let Some(repeater) = field.as_repeater()
                && let Some(leaf) = repeater
                    .item_schema()
                    .fields()
                    .find(|leaf| leaf.is_password())
            {
                errors.push(crate::DeclarationErrorKind::RepeaterPassword {
                    field: field.name().to_string(),
                    leaf: leaf.name().to_string(),
                });
            }
        }
        let mut seen = HashSet::new();
        for field in &self.fields {
            match field.misdeclared() {
                Some(error) => errors.push(error),
                None if !seen.insert(field.name()) => {
                    errors.push(crate::DeclarationErrorKind::DuplicateField {
                        name: field.name().to_string(),
                    });
                }
                None => {}
            }
        }
        errors
    }

    /// Collects the keys of the fields this submission hides: the payload of every embedded
    /// variant it does not choose, and every field a condition hides.
    pub(crate) fn hidden_fields(&self, values: &FormValues) -> HashSet<String> {
        fn walk(nodes: &[Node], values: &FormValues, out: &mut Vec<usize>) {
            for node in nodes {
                match node {
                    Node::Embedded(embedded) => embedded.hidden_fields(values, out),
                    node => {
                        if let Some(children) = node.children() {
                            walk(children, values, out);
                        }
                    }
                }
            }
        }
        let mut indices = Vec::new();
        walk(&self.nodes, values, &mut indices);
        indices
            .into_iter()
            .map(|index| self.fields[index].name().to_string())
            .chain(self.condition_hidden(values))
            .collect()
    }

    /// Adds to `errors` what the controls' own rules refuse in a submission: an unchecked
    /// accepted checkbox, a multiple choice's or a repeater's count of items, an email or URL
    /// field's value, a text field's declared length, bounds and pattern, and a choice that is not
    /// one of its options. Skips hidden fields and keys `errors` already refuses. A value the
    /// record already stores keeps its rules unchecked: a disabled control posts it back, and a
    /// rule declared since must not lock the record.
    pub(crate) async fn check_controls(
        &self,
        cx: &Cx,
        values: &FormValues,
        stored: &FormValues,
        errors: &mut FieldErrors,
    ) {
        let hidden = self.hidden_fields(values);
        let zone = crate::time_zone(cx);
        for field in &self.fields {
            let name = field.name();
            if errors.contains_key(name) || hidden.contains(name) {
                continue;
            }
            let Some(value) = values.get(name) else {
                if let Some(error) = field.check_unposted(&zone) {
                    errors.push(error);
                }
                continue;
            };
            let refusal = (stored.get(name) != Some(value))
                .then(|| field.check(value, &zone))
                .flatten();
            if let Some(repeater) = field.as_repeater() {
                // The count's refusal owns the repeater's key; each row's own keys still check.
                if let Some(error) = refusal {
                    errors.push(error);
                }
                let rows = value.as_rows().unwrap_or_default();
                let held = stored.get(name).and_then(FormValue::as_rows);
                repeater
                    .check(cx, name, rows, held.unwrap_or_default(), errors)
                    .await;
                continue;
            }
            if let Some(error) =
                refusal.or_else(|| self.comparison_refusal(field, values, stored, &hidden))
            {
                errors.push(error);
                continue;
            }
            let parent = field.parent_key().and_then(|key| values.text(key));
            for message in field
                .validate_exists(cx, value, parent, stored.get(name))
                .await
            {
                errors.add(name, message);
            }
        }
    }

    /// What the controls' own rules refuse in `values`, alone.
    #[cfg(test)]
    pub(crate) async fn checked(&self, cx: &Cx, values: &FormValues) -> FieldErrors {
        let mut errors = FieldErrors::new();
        self.check_controls(cx, values, &FormValues::new(), &mut errors)
            .await;
        errors
    }

    /// Puts back the stored value of each text field whose control posted it as rendered: see
    /// [`Field::keep_unedited`].
    pub(crate) fn keep_unedited(
        &self,
        values: &mut FormValues,
        stored: &FormValues,
        zone: &jiff::tz::TimeZone,
    ) {
        for field in &self.fields {
            let name = field.name();
            let (Some(value), Some(held)) = (values.get(name), stored.get(name)) else {
                continue;
            };
            if let Some(kept) = field.keep_unedited(value, held, zone) {
                values.insert(name, kept);
            }
        }
    }

    /// Re-checks every submitted relationship key through the write's open transaction, but a
    /// multiple choice's keys the record's stored value in `stored` holds already.
    pub(crate) async fn recheck_relationships(
        &self,
        cx: &Cx,
        values: &FormValues,
        stored: &FormValues,
        ex: &mut dyn toasty::Executor,
    ) -> FieldErrors {
        let mut errors = FieldErrors::new();
        let hidden = self.hidden_fields(values);
        for field in &self.fields {
            let name = field.name();
            if hidden.contains(name) {
                continue;
            }
            let Some(value) = values.get(name) else {
                continue;
            };
            let parent = field.parent_key().and_then(|key| values.text(key));
            for message in field
                .recheck(cx, value, parent, stored.get(name), &mut *ex)
                .await
            {
                errors.add(name, message);
            }
        }
        errors
    }

    /// Puts back in each multiple choice's list the keys of the record's stored list `stored` it
    /// drops that the user cannot view, through the write's open transaction: the form never
    /// offered them, so its submission leaves them linked.
    pub(crate) async fn keep_unseen_links(
        &self,
        cx: &Cx,
        values: &mut FormValues,
        stored: &FormValues,
        ex: &mut dyn toasty::Executor,
    ) {
        for field in &self.fields {
            let name = field.name();
            let (Some(value), Some(held)) = (values.get(name), stored.get(name)) else {
                continue;
            };
            if let Some(kept) = field.keep_unseen(cx, value, held, &mut *ex).await {
                values.insert(name, kept);
            }
        }
    }
}

#[cfg(test)]
mod tests;