tablo-core 0.9.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Composes field slots, layout containers, and embedded values into one tree.

use std::{
    collections::HashMap,
    sync::atomic::{AtomicUsize, Ordering},
};

use topcoat::{Result, context::Cx, view::*};

use super::{
    Schema,
    condition::{Condition, disable, watch},
    embedded::Embedded,
    fields::{Field, Placement},
    layouts::{Grid, Group, Section, Step, Tab, Tabs, Wizard, spanned},
};
use crate::values::{FieldError, FieldErrors, FormValue, FormValues};

#[derive(Debug)]
pub(crate) enum Node {
    /// A field, by its index in the root schema's field list.
    Field(usize),
    Section(Box<Section>),
    Group(Box<Group>),
    Grid(Box<Grid>),
    /// A `Tabs` block, whose children are its `Tab` nodes.
    Tabs(Box<Tabs>),
    /// One tab, placed only in a `Tabs` block's children.
    Tab(Box<Tab>),
    /// A `Wizard` block, whose children are its `Step` nodes.
    Wizard(Box<Wizard>),
    /// One step, placed only in a `Wizard` block's children.
    Step(Box<Step>),
    Embedded(Box<Embedded>),
}

/// Holds the values and errors a schema's controls render with.
pub struct Source<'a> {
    values: &'a FormValues,
    errors: &'a FieldErrors,
    /// The conditions each watched field's key drives.
    watched: HashMap<String, Vec<Condition>>,
    /// The prefix of the form's DOM ids and of its signals' keys; empty for a form alone on its
    /// page.
    scope: String,
    /// Where a searchable choice fetches its options, when not the resource's own route.
    options: Option<String>,
    /// Whether the form edits a stored record.
    editing: bool,
    /// The keys the request's field access hides or locks.
    mask: super::FormMask,
    /// What every control's name starts with: a related form's `{slug}.`, empty otherwise.
    prefix: String,
    /// How many tabs hold the node rendering now, which tells a nested block's DOM ids from its
    /// outer tab's.
    tab_depth: AtomicUsize,
    /// Whether a dialog frames the form, whose sections then render without a panel.
    flat: bool,
}

impl<'a> Source<'a> {
    /// Renders controls hydrated with `values` and inline `errors`.
    pub fn form(values: &'a FormValues, errors: &'a FieldErrors) -> Self {
        Self {
            values,
            errors,
            watched: HashMap::new(),
            scope: String::new(),
            options: None,
            editing: false,
            mask: super::FormMask::default(),
            prefix: String::new(),
            tab_depth: AtomicUsize::new(0),
            flat: false,
        }
    }

    /// Renders sections without a panel when `flat`, for a form a dialog frames.
    pub(crate) fn flat(mut self, flat: bool) -> Self {
        self.flat = flat;
        self
    }

    /// Whether sections render without a panel.
    pub(super) fn is_flat(&self) -> bool {
        self.flat
    }

    /// Renders the edit form of a stored record, where `disabled_on_edit` disables a control.
    pub(crate) fn editing(mut self, editing: bool) -> Self {
        self.editing = editing;
        self
    }

    /// Renders nothing for the keys `mask` hides, and disables the controls of the ones it locks.
    pub(crate) fn masked(mut self, mask: super::FormMask) -> Self {
        self.mask = mask;
        self
    }

    /// Renders a form sharing its page with another: its DOM ids and its signals' keys carry
    /// `scope`, so neither form's labels, conditions or variants reach the other's.
    pub(crate) fn scoped(mut self, scope: impl Into<String>) -> Self {
        self.scope = scope.into();
        self
    }

    /// Renders a related form inside its owner's: every control posts under `prefix`, while its
    /// values, errors, mask and conditions keep the related form's own keys.
    pub(crate) fn posting_under(mut self, prefix: impl Into<String>) -> Self {
        self.prefix = prefix.into();
        self
    }

    /// Fetches a searchable choice's options from `url` rather than the resource's own route.
    pub(crate) fn options_at(mut self, url: impl Into<String>) -> Self {
        self.options = Some(url.into());
        self
    }

    /// The prefix of the form's DOM ids and of its signals' keys.
    pub(crate) fn scope(&self) -> &str {
        &self.scope
    }

    /// Renders each watched field with the handlers its conditions follow.
    pub(crate) fn watching(mut self, watched: HashMap<String, Vec<Condition>>) -> Self {
        self.watched = watched;
        self
    }

    /// Renders `view` guarded by `condition`, or as it is without one.
    pub(crate) fn guard<'v>(
        &self,
        cx: &'v Cx,
        condition: Option<&Condition>,
        view: BoxView<'v>,
    ) -> BoxView<'v> {
        match condition {
            Some(condition) => condition.guard(cx, &self.scope, self.values, view),
            None => view,
        }
    }

    /// How many tabs hold the node rendering now.
    pub(crate) fn tab_depth(&self) -> usize {
        self.tab_depth.load(Ordering::Relaxed)
    }

    /// Renders what follows inside `depth` tabs.
    pub(crate) fn set_tab_depth(&self, depth: usize) {
        self.tab_depth.store(depth, Ordering::Relaxed);
    }

    /// Whether `condition` holds in the submission, as the page first renders it: always without
    /// one.
    pub(crate) fn holds(&self, condition: Option<&Condition>) -> bool {
        condition.is_none_or(|condition| condition.holds(self.values))
    }

    pub(crate) fn value(&self, name: &str) -> Option<&FormValue> {
        self.values.get(name)
    }

    /// The text `name` holds: a scalar control's value.
    pub(crate) fn text(&self, name: &str) -> Option<&str> {
        self.values.text(name)
    }

    /// The rows `name` holds: a repeater's, none when it holds none.
    pub(crate) fn rows(&self, name: &str) -> &[FormValues] {
        self.values.rows(name).unwrap_or_default()
    }

    /// The errors the controls render under.
    pub(crate) fn errors(&self) -> &FieldErrors {
        self.errors
    }

    /// Where a searchable choice fetches its options, when not the resource's own route.
    pub(crate) fn options(&self) -> Option<&str> {
        self.options.as_deref()
    }

    /// The DOM id of the control posting `name`.
    pub(crate) fn id(&self, name: &str) -> String {
        self.placement(name, None).id(name)
    }

    /// Where the field posting `name` renders in this form, under its parent's value `parent`.
    fn placement<'p>(&'p self, name: &str, parent: Option<&'p str>) -> Placement<'p> {
        Placement {
            parent,
            scope: &self.scope,
            options: self.options.as_deref(),
            editing: self.editing,
            locked: self.mask.locks(name),
            prefix: &self.prefix,
        }
    }

    /// Returns the message the field `field` renders under its control.
    pub(crate) fn error_for(&self, field: &Field) -> Option<&FieldError> {
        self.errors.first(field.name())
    }
}

impl Node {
    /// Renders this node from `source`, reading field slots from `fields`.
    pub(crate) async fn render<'a>(
        &self,
        cx: &'a Cx,
        fields: &[Field],
        source: &Source<'_>,
    ) -> Result<BoxView<'a>> {
        match self {
            Node::Field(index) => {
                if !shows(&fields[*index], source) {
                    return Ok(().boxed());
                }
                let field = &fields[*index];
                // A repeater's rows render from the keys and errors of each of their controls.
                if let Some(repeater) = field.as_repeater() {
                    let view = Box::pin(repeater.render(cx, field, source)).await?;
                    let view = spanned(cx, field.span(), view);
                    return Ok(source.guard(cx, field.condition(), view));
                }
                let error = source.error_for(field);
                let parent = field.parent_key().and_then(|key| source.text(key));
                let mut view = Box::pin(field.render_under(
                    cx,
                    source.value(field.name()),
                    error,
                    source.placement(field.name(), parent),
                ))
                .await?;
                if let Some(conditions) = source.watched.get(field.name()) {
                    let posted = format!("{}{}", source.prefix, field.name());
                    view = watch(
                        cx,
                        field,
                        &posted,
                        conditions,
                        &source.scope,
                        source.values,
                        view,
                    );
                }
                if let Some(condition) = field.disabling() {
                    view = disable(cx, condition, &source.scope, source.values, view);
                }
                let view = spanned(cx, field.span(), view);
                Ok(source.guard(cx, field.condition(), view))
            }
            // A block whose every field is masked renders no heading over nothing.
            Node::Section(_) | Node::Group(_) | Node::Grid(_) | Node::Tabs(_) | Node::Wizard(_)
                if !self.shows_any(fields, source) =>
            {
                Ok(().boxed())
            }
            Node::Section(s) => {
                let view = Box::pin(s.render(cx, fields, source)).await?;
                Ok(source.guard(cx, s.condition.as_ref(), view))
            }
            Node::Group(g) => {
                let view = Box::pin(g.render(cx, fields, source)).await?;
                Ok(source.guard(cx, g.condition.as_ref(), view))
            }
            Node::Grid(g) => {
                let view = Box::pin(g.render(cx, fields, source)).await?;
                Ok(source.guard(cx, g.condition.as_ref(), view))
            }
            Node::Tabs(t) => {
                let view = Box::pin(t.render(cx, fields, source)).await?;
                Ok(source.guard(cx, t.condition.as_ref(), view))
            }
            // A tab renders through its block, which lays out its trigger and its panel.
            Node::Tab(t) => Box::pin(render_nodes(cx, &t.children.nodes, fields, source)).await,
            Node::Wizard(w) => {
                let view = Box::pin(w.render(cx, fields, source)).await?;
                Ok(source.guard(cx, w.condition.as_ref(), view))
            }
            // A step renders through its block, which lays out its trigger and its panel.
            Node::Step(s) => Box::pin(render_nodes(cx, &s.children.nodes, fields, source)).await,
            Node::Embedded(e) => Box::pin(e.render(cx, fields, source)).await,
        }
    }

    /// Whether a field under this node renders an error from `source`: under its own key, or a
    /// repeater's under one of its rows' keys.
    pub(crate) fn has_error(&self, fields: &[Field], source: &Source<'_>) -> bool {
        let mut any = false;
        self.visit_fields(&mut |index| {
            let name = fields[index].name();
            any |= source.errors.iter().any(|error| {
                error.key == name
                    || error
                        .key
                        .strip_prefix(name)
                        .is_some_and(|rest| rest.starts_with('.'))
            });
        });
        any
    }

    /// The first field slot under this node, in declaration order.
    pub(crate) fn first_field(&self) -> Option<usize> {
        let mut first = None;
        self.visit_fields(&mut |index| {
            first.get_or_insert(index);
        });
        first
    }

    /// Calls `f` with every field slot under this node, in declaration order.
    fn visit_fields(&self, f: &mut impl FnMut(usize)) {
        match self {
            Node::Field(index) => f(*index),
            Node::Embedded(e) => e.visit_fields(f),
            node => {
                for child in node.children().unwrap_or_default() {
                    child.visit_fields(f);
                }
            }
        }
    }

    /// Whether some field under this node renders under `source`'s mask.
    pub(crate) fn shows_any(&self, fields: &[Field], source: &Source<'_>) -> bool {
        match self {
            Node::Field(index) => shows(&fields[*index], source),
            Node::Embedded(e) => {
                let mut any = false;
                e.visit_fields(&mut |index| any |= shows(&fields[index], source));
                any
            }
            node => node
                .children()
                .unwrap_or_default()
                .iter()
                .any(|child| child.shows_any(fields, source)),
        }
    }

    /// Returns the nodes a layout container holds, or `None` for a field slot and an embedded
    /// value.
    pub(crate) fn children(&self) -> Option<&[Node]> {
        match self {
            Node::Section(s) => Some(&s.children.nodes),
            Node::Group(g) => Some(&g.children.nodes),
            Node::Grid(g) => Some(&g.children.nodes),
            Node::Tabs(t) => Some(&t.children.nodes),
            Node::Tab(t) => Some(&t.children.nodes),
            Node::Wizard(w) => Some(&w.children.nodes),
            Node::Step(s) => Some(&s.children.nodes),
            Node::Field(_) | Node::Embedded(_) => None,
        }
    }

    fn children_mut(&mut self) -> Option<&mut Vec<Node>> {
        match self {
            Node::Section(s) => Some(&mut s.children.nodes),
            Node::Group(g) => Some(&mut g.children.nodes),
            Node::Grid(g) => Some(&mut g.children.nodes),
            Node::Tabs(t) => Some(&mut t.children.nodes),
            Node::Tab(t) => Some(&mut t.children.nodes),
            Node::Wizard(w) => Some(&mut w.children.nodes),
            Node::Step(s) => Some(&mut s.children.nodes),
            Node::Field(_) | Node::Embedded(_) => None,
        }
    }

    /// Shifts every field slot under this node by `by`.
    pub(crate) fn offset(&mut self, by: usize) {
        match self {
            Node::Field(index) => *index += by,
            Node::Embedded(e) => e.offset(by),
            _ => {
                for child in self.children_mut().into_iter().flatten() {
                    child.offset(by);
                }
            }
        }
    }
}

impl<F> Schema<F> {
    /// The first field whose control posts keys besides its own or fetches from its resource's
    /// routes: a file field, a repeater, an embedded value's leaf, a searchable or dependent
    /// choice. A related form, posting under its owner's prefix, carries none of them.
    pub(crate) fn needs_own_keys(&self) -> Option<&str> {
        let mut embedded = Vec::new();
        for node in &self.nodes {
            node.embedded_fields(&mut embedded);
        }
        self.fields
            .iter()
            .enumerate()
            .find(|(index, field)| {
                embedded.contains(index)
                    || field.is_file()
                    || field.as_repeater().is_some()
                    || field.parent_key().is_some()
                    || field
                        .as_choice()
                        .is_some_and(|choice| choice.is_searchable())
            })
            .map(|(_, field)| field.name())
    }
}

impl Node {
    /// Adds to `out` every field slot an embedded value under this node holds.
    fn embedded_fields(&self, out: &mut Vec<usize>) {
        match self {
            Node::Embedded(e) => e.visit_fields(&mut |index| out.push(index)),
            node => {
                for child in node.children().unwrap_or_default() {
                    child.embedded_fields(out);
                }
            }
        }
    }
}

/// Whether `field` renders under `source`'s mask: not when hidden, nor a locked repeater, whose
/// rows' order posts its key.
fn shows(field: &Field, source: &Source<'_>) -> bool {
    !(source.mask.hides(field.name())
        || field.is_hidden(source.editing)
        || (source.mask.locks(field.name()) && field.as_repeater().is_some()))
}

/// Renders `nodes` in order as one view.
pub(crate) async fn render_nodes<'a>(
    cx: &'a Cx,
    nodes: &[Node],
    fields: &[Field],
    source: &Source<'_>,
) -> Result<BoxView<'a>> {
    let mut views = Vec::with_capacity(nodes.len());
    for node in nodes {
        views.push(node.render(cx, fields, source).await?);
    }
    Ok(view! {
        for v in views {
            (v)
        }
    }
    .boxed())
}

/// Converts a field, a layout block, a tuple of either, or a schema into a [`Schema`].
///
/// `F` is the record form whose controls the schema places, `()` for a page's or an action's
/// schema: a control converts into the schema of its own form only.
#[diagnostic::on_unimplemented(
    message = "`{Self}` cannot be placed in a `Schema<{F}>`",
    label = "not a control, layout block, schema, or tuple of 2 to 12 of them for this schema",
    note = "a resource's form places its record form's own controls, from `controls()`; \
            `Field::text` and the other constructors build a page's or an action's controls"
)]
pub trait IntoSchema<F = ()> {
    fn into_schema(self) -> Schema<F>;
}

/// Moves a control or a schema to the form `G`: what `#[derive(RecordForm)]` hands its controls
/// over with.
#[doc(hidden)]
pub trait Retype {
    type As<G>;

    fn retype<G>(self) -> Self::As<G>;
}

impl<F> IntoSchema<F> for Schema<F> {
    fn into_schema(self) -> Schema<F> {
        self
    }
}

impl IntoSchema for Field {
    fn into_schema(self) -> Schema {
        Schema::from_parts(vec![Node::Field(0)], vec![self])
    }
}

/// Generates the single-node [`IntoSchema`] impls for every layout container, holding a schema or
/// nothing.
macro_rules! container_nodes {
    ($($ty:ident),+ $(,)?) => {
        $(
            impl<F> IntoSchema<F> for $ty<Schema<F>> {
                fn into_schema(self) -> Schema<F> {
                    let mut fields = Vec::new();
                    let block = self.map(|mut children| {
                        fields = std::mem::take(&mut children.fields);
                        children.retype()
                    });
                    Schema::from_parts(vec![Node::$ty(Box::new(block))], fields)
                }
            }

            impl<F> IntoSchema<F> for $ty<()> {
                fn into_schema(self) -> Schema<F> {
                    self.holding(Schema::<F>::default()).into_schema()
                }
            }
        )+
    };
}

container_nodes!(Section, Group, Grid);

impl<F> IntoSchema<F> for Tabs<Schema<F>> {
    fn into_schema(self) -> Schema<F> {
        let mut fields = Vec::new();
        let block = self.map(|mut children| {
            fields = std::mem::take(&mut children.fields);
            children.retype()
        });
        Schema::from_parts(vec![Node::Tabs(Box::new(block))], fields)
    }
}

/// Generates the tuple impls of [`IntoSchema`] from one list per arity.
macro_rules! into_schema_tuples {
    ($($T:ident => $v:ident),+ $(,)?) => {
        impl<Form, $($T),+> IntoSchema<Form> for ($($T,)+)
        where
            $($T: IntoSchema<Form>,)+
        {
            fn into_schema(self) -> Schema<Form> {
                let ($($v,)+) = self;
                let mut schema = Schema::default();
                $( schema.append($v.into_schema()); )+
                schema
            }
        }
    };
}

into_schema_tuples!(A => a, B => b);
into_schema_tuples!(A => a, B => b, C => c);
into_schema_tuples!(A => a, B => b, C => c, D => d);
into_schema_tuples!(A => a, B => b, C => c, D => d, E => e);
into_schema_tuples!(A => a, B => b, C => c, D => d, E => e, F => f);
into_schema_tuples!(A => a, B => b, C => c, D => d, E => e, F => f, G => g);
into_schema_tuples!(
    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h
);
into_schema_tuples!(
    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i
);
into_schema_tuples!(
    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j
);
into_schema_tuples!(
    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j, K => k
);
into_schema_tuples!(
    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j, K => k, L => l
);