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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! [`RelationColumn`] and [`CountColumn`]: columns over a relation field, which load the relation
//! they read, and the [`RelationLens`] that names the field, built by
//! [`relation!`](crate::relation!).

use std::sync::Arc;

use derive_where::derive_where;
use toasty::{Deferred, Executor};
use topcoat::{Result, context::Cx};

use super::{Column, ColumnToggle, ColumnWidth, Includes, Related};
use crate::{BoxFuture, DeclarationErrorKind, lens::Reads, schema::OptionSource};

mod lens;
mod restrict;

pub use lens::RelationLens;
use restrict::{Restrict, restrict_many, restrict_one, shows};

/// The text an unscoped relation column renders for a row whose relation was not loaded.
const UNLOADED: &str = "(unloaded)";

/// What a relation column reads off a row in the request's context, or `None` when the row's
/// relation was not loaded.
type Loaded<M, T> = Arc<dyn Fn(&Cx, &M) -> Option<T> + Send + Sync>;

/// A label source's type name, and whether a context's panel can load from it.
type LabelSource = (&'static str, fn(&Cx) -> bool);

/// Whether a request may not view a field of the related records a column shows.
type Masked = Arc<dyn Fn(&Cx) -> bool + Send + Sync>;

/// The text a relation column shows for a row, which a source's label reads in the request's
/// context, or `None` when the row's relation was not loaded.
type LoadedText<M> = Arc<dyn Fn(&Cx, &M) -> Option<String> + Send + Sync>;

/// A to-one relation field to `T` a [`RelationColumn`] reads: `Deferred<T>`, or
/// `Deferred<Option<T>>` for a nullable relation.
///
/// `T` is inferred from the field: `Deferred<X>` matches the first impl with `T = X` alone,
/// since an `Option` is not a model, and the two impls cannot overlap, which would need
/// `T = Option<T>`.
pub trait ToOneRelation<T>: sealed::Sealed<T> {
    /// The related record, `Some(None)` for a nullable relation holding none, or `None` when the
    /// relation was not loaded.
    #[doc(hidden)]
    fn loaded(&self) -> Option<Option<&T>>;
}

mod sealed {
    pub trait Sealed<T> {}

    impl<T: toasty::schema::Model> Sealed<T> for toasty::Deferred<T> {}
    impl<T: toasty::schema::Model> Sealed<T> for toasty::Deferred<Option<T>> {}
}

impl<T: toasty::schema::Model> ToOneRelation<T> for Deferred<T> {
    fn loaded(&self) -> Option<Option<&T>> {
        (!self.is_unloaded()).then(|| Some(self.get()))
    }
}

impl<T: toasty::schema::Model> ToOneRelation<T> for Deferred<Option<T>> {
    fn loaded(&self) -> Option<Option<&T>> {
        (!self.is_unloaded()).then(|| self.get().as_ref())
    }
}

/// A column showing the record of a `Deferred` `belongs_to` or `has_one` field as text, or the
/// records of a `has_many` one ([`list`](Self::list)), which declares the field's include so every
/// page that renders it loads them.
///
/// ```rust
/// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
/// # struct Author { #[key] #[auto] id: uuid::Uuid, name: String }
/// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
/// # struct Post {
/// #     #[key] #[auto] id: uuid::Uuid,
/// #     author_id: uuid::Uuid,
/// #     #[belongs_to(key = author_id, references = id)]
/// #     author: toasty::Deferred<Author>,
/// # }
/// # struct AuthorResource;
/// # impl tablo_core::extend::OptionSource for AuthorResource {
/// #     type Model = Author;
/// #     fn scoped_query(_cx: &topcoat::context::Cx)
/// #         -> topcoat::Result<toasty::stmt::Query<toasty::stmt::List<Author>>>
/// #     {
/// #         Ok(toasty::stmt::Query::all())
/// #     }
/// #     fn label(_cx: &topcoat::context::Cx, author: &Author) -> String {
/// #         author.name.clone()
/// #     }
/// # }
/// use tablo_core::{RelationColumn, lens, relation};
///
/// RelationColumn::new::<AuthorResource>(
///     relation!(Post.author),
///     lens!(Author.name),
///     |a: &Author| a.name.clone(),
/// );
/// ```
///
/// The header and the column's name are the field's name, capitalized, until
/// [`label`](Self::label) replaces both. The column neither searches nor sorts.
///
/// A column over a source `S` shows only the related records `S` shows the user: the ones its
/// [`scoped_query`](OptionSource::scoped_query) returns and its policy lets the user view, as a
/// relationship choice over `S` offers them. A related record it hides, soft-deleted or another
/// tenant's, renders empty, and so does every record of a row loaded around the table's or the
/// [`Detail`](crate::Detail)'s loader, which checks them. The row keeps what the include loaded,
/// so a policy reading the relation sees it whole. [`unscoped`](Self::unscoped) shows whatever
/// the include loads.
///
/// A field [`field_access`](crate::ResourceDef::field_access) restricts shows to a user only
/// where the user may view it, on every resource the panel mounts over its model: a request that
/// may not view a field [`new`](Self::new) reads finds no such column. A record's
/// [`label`](OptionSource::label) is its title, which the mount keeps clear of every restricted
/// field, or its key, which no field access may hide.
#[derive_where(Clone, Debug)]
pub struct RelationColumn<M> {
    relation: Declared<M>,
    #[derive_where(skip(Debug))]
    project: LoadedText<M>,
}

impl<M> RelationColumn<M>
where
    M: toasty::schema::Model + Send + Sync + 'static,
{
    /// Declare a column rendering `project` of the record `relation` loads, when `S` shows it.
    /// `reads` names the fields of the record `project` reads, a [`lens!`](crate::lens!) or a
    /// [`Reads`]: a request `S` hides one of them from finds no such column.
    pub fn new<S>(
        relation: RelationLens<M, impl ToOneRelation<S::Model> + 'static>,
        reads: impl Into<Reads<S::Model>>,
        project: impl Fn(&S::Model) -> String + Send + Sync + 'static,
    ) -> Self
    where
        S: OptionSource,
    {
        let read = relation.read;
        let reads = reads.into();
        Self {
            relation: Declared::of(&relation, ColumnWidth::Grow)
                .scoped::<S>(restrict_one::<M, S, _>(relation.read))
                .masked(Arc::new(move |cx| reads.reads_any(&S::hidden_fields(cx)))),
            project: Arc::new(move |cx, row| {
                read(row).loaded().map(|target| {
                    target
                        .filter(|target| shows::<S>(cx, target))
                        .map(&project)
                        .unwrap_or_default()
                })
            }),
        }
    }

    /// Declare a column showing the record `relation` loads by `S`'s
    /// [`label`](OptionSource::label), when `S` shows it: for a resource, the
    /// [`record_title`](crate::ResourceDef::record_title) its detail page is titled with.
    ///
    /// ```rust
    /// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
    /// # struct Author { #[key] #[auto] id: uuid::Uuid, name: String }
    /// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
    /// # struct Post {
    /// #     #[key] #[auto] id: uuid::Uuid,
    /// #     author_id: uuid::Uuid,
    /// #     #[belongs_to(key = author_id, references = id)]
    /// #     author: toasty::Deferred<Author>,
    /// # }
    /// # struct AuthorResource;
    /// # impl tablo_core::extend::OptionSource for AuthorResource {
    /// #     type Model = Author;
    /// #     fn scoped_query(_cx: &topcoat::context::Cx)
    /// #         -> topcoat::Result<toasty::stmt::Query<toasty::stmt::List<Author>>>
    /// #     {
    /// #         Ok(toasty::stmt::Query::all())
    /// #     }
    /// #     fn label(_cx: &topcoat::context::Cx, author: &Author) -> String {
    /// #         author.name.clone()
    /// #     }
    /// # }
    /// use tablo_core::{RelationColumn, relation};
    ///
    /// RelationColumn::of::<AuthorResource>(relation!(Post.author));
    /// ```
    pub fn of<S>(relation: RelationLens<M, impl ToOneRelation<S::Model> + 'static>) -> Self
    where
        S: OptionSource,
    {
        let read = relation.read;
        Self {
            relation: Declared::of(&relation, ColumnWidth::Grow)
                .scoped::<S>(restrict_one::<M, S, _>(relation.read)),
            project: Arc::new(move |cx, row| {
                read(row).loaded().map(|target| {
                    target
                        .filter(|target| shows::<S>(cx, target))
                        .map(|target| S::label(cx, target))
                        .unwrap_or_default()
                })
            }),
        }
    }

    /// Declare a column listing the records a `has_many` `relation` loads that `S` shows, a
    /// many-to-many `#[has_many(via = ..)]` one included, each by `S`'s
    /// [`label`](OptionSource::label), joined by commas.
    ///
    /// ```rust
    /// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
    /// # struct Post {
    /// #     #[key] #[auto] id: uuid::Uuid,
    /// #     #[has_many]
    /// #     taggings: toasty::Deferred<Vec<Tagging>>,
    /// #     #[has_many(via = taggings.tag)]
    /// #     tags: toasty::Deferred<Vec<Tag>>,
    /// # }
    /// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
    /// # struct Tag { #[key] #[auto] id: uuid::Uuid, name: String }
    /// # #[derive(Debug, Clone, toasty::Model)]
    /// # struct Tagging {
    /// #     #[key] #[auto] id: uuid::Uuid,
    /// #     #[index] post_id: uuid::Uuid,
    /// #     #[belongs_to(key = post_id, references = id)]
    /// #     post: toasty::Deferred<Post>,
    /// #     #[index] tag_id: uuid::Uuid,
    /// #     #[belongs_to(key = tag_id, references = id)]
    /// #     tag: toasty::Deferred<Tag>,
    /// # }
    /// # struct TagResource;
    /// # impl tablo_core::extend::OptionSource for TagResource {
    /// #     type Model = Tag;
    /// #     fn scoped_query(_cx: &topcoat::context::Cx)
    /// #         -> topcoat::Result<toasty::stmt::Query<toasty::stmt::List<Tag>>>
    /// #     {
    /// #         Ok(toasty::stmt::Query::all())
    /// #     }
    /// #     fn label(_cx: &topcoat::context::Cx, tag: &Tag) -> String {
    /// #         tag.name.clone()
    /// #     }
    /// #     fn allows(_cx: &topcoat::context::Cx, _: tablo_core::Ability<'_, Tag>) -> bool {
    /// #         true
    /// #     }
    /// # }
    /// use tablo_core::{RelationColumn, relation};
    ///
    /// RelationColumn::list::<TagResource>(relation!(Post.tags));
    /// ```
    pub fn list<S>(relation: RelationLens<M, Deferred<Vec<S::Model>>>) -> Self
    where
        S: OptionSource,
    {
        let read = relation.read;
        Self {
            relation: Declared::of(&relation, ColumnWidth::Grow)
                .scoped::<S>(restrict_many::<M, S>(relation.read)),
            project: Arc::new(move |cx, row| {
                let records = read(row);
                (!records.is_unloaded()).then(|| {
                    records
                        .get()
                        .iter()
                        .filter(|record| shows::<S>(cx, record))
                        .map(|record| S::label(cx, record))
                        .collect::<Vec<_>>()
                        .join(", ")
                })
            }),
        }
    }

    /// Declare a column rendering `project` of whatever record `relation` loads, under no
    /// resource's query, policy or field access: for a related model no resource administers. A
    /// panel mounting a resource that restricts a field of the related model refuses it. A row
    /// loaded without the column's include renders `"(unloaded)"` and fails a debug assertion.
    pub fn unscoped<T>(
        relation: RelationLens<M, impl ToOneRelation<T> + 'static>,
        project: impl Fn(&T) -> String + Send + Sync + 'static,
    ) -> Self {
        let read = relation.read;
        Self {
            relation: Declared::of(&relation, ColumnWidth::Grow),
            project: Arc::new(move |_cx, row| {
                read(row)
                    .loaded()
                    .map(|target| target.map(&project).unwrap_or_default())
            }),
        }
    }
}

impl<M> RelationColumn<M> {
    /// The row's text, or `None` when its relation was not loaded.
    fn loaded_text(&self, cx: &Cx, row: &M) -> Option<String> {
        (self.project)(cx, row)
    }
}

/// A column counting the records of a `Deferred` `has_many` field that a source `S` shows, which
/// declares the field's include so every page that renders it loads the records.
///
/// ```rust
/// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
/// # struct Post {
/// #     #[key] #[auto] id: uuid::Uuid,
/// #     #[has_many]
/// #     comments: toasty::Deferred<Vec<Comment>>,
/// # }
/// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
/// # struct Comment {
/// #     #[key] #[auto] id: uuid::Uuid,
/// #     #[index]
/// #     post_id: uuid::Uuid,
/// #     #[belongs_to(key = post_id, references = id)]
/// #     post: toasty::Deferred<Post>,
/// # }
/// # struct CommentResource;
/// # impl tablo_core::extend::OptionSource for CommentResource {
/// #     type Model = Comment;
/// #     fn scoped_query(_cx: &topcoat::context::Cx)
/// #         -> topcoat::Result<toasty::stmt::Query<toasty::stmt::List<Comment>>>
/// #     {
/// #         Ok(toasty::stmt::Query::all())
/// #     }
/// #     fn label(_cx: &topcoat::context::Cx, comment: &Comment) -> String {
/// #         comment.id.to_string()
/// #     }
/// # }
/// use tablo_core::{CountColumn, relation};
///
/// CountColumn::of::<CommentResource>(relation!(Post.comments));
/// ```
///
/// The header and the column's name are the field's name, capitalized, until
/// [`label`](Self::label) replaces both.
///
/// The count is how many of the loaded records `S` shows the user, which [`RelationColumn`]
/// decides the same way: a page loads every related record of every row it counts.
/// [`unscoped`](Self::unscoped) counts every record the include loads.
#[derive_where(Clone, Debug)]
pub struct CountColumn<M> {
    relation: Declared<M>,
    #[derive_where(skip(Debug))]
    count: Loaded<M, usize>,
}

impl<M> CountColumn<M>
where
    M: toasty::schema::Model + Send + Sync + 'static,
{
    /// Declare a column counting the records `relation` loads that `S` shows.
    pub fn of<S>(relation: RelationLens<M, Deferred<Vec<S::Model>>>) -> Self
    where
        S: OptionSource,
    {
        Self {
            relation: Declared::of(&relation, ColumnWidth::Auto)
                .scoped::<S>(restrict_many::<M, S>(relation.read))
                .fieldless(),
            count: count(relation.read, shows::<S>),
        }
    }

    /// Declare a column counting every record `relation` loads, under no resource's query or
    /// policy: for a related model no resource administers. A count shows no field of the
    /// records.
    pub fn unscoped<T>(relation: RelationLens<M, Deferred<Vec<T>>>) -> Self
    where
        T: toasty::schema::Model + 'static,
    {
        Self {
            relation: Declared::of(&relation, ColumnWidth::Auto).fieldless(),
            count: count(relation.read, |_, _| true),
        }
    }
}

impl<M> CountColumn<M> {
    /// The row's count, or `None` when its relation was not loaded.
    fn loaded_text(&self, cx: &Cx, row: &M) -> Option<String> {
        (self.count)(cx, row).map(|count| count.to_string())
    }
}

/// How many records of the list `read` reads off a row `shown` accepts, or `None` when it was not
/// loaded.
fn count<M: 'static, T: 'static>(
    read: fn(&M) -> &Deferred<Vec<T>>,
    shown: fn(&Cx, &T) -> bool,
) -> Loaded<M, usize> {
    Arc::new(move |cx, row| {
        let records = read(row);
        (!records.is_unloaded()).then(|| records.get().iter().filter(|r| shown(cx, r)).count())
    })
}

/// What both relation-field columns declare: the name and label, the include and the width.
#[derive_where(Clone, Debug)]
struct Declared<M> {
    name: String,
    /// The declared label, over the field's.
    label: Option<crate::Text>,
    field_label: crate::Text,
    misdeclared: Option<DeclarationErrorKind>,
    includes: Includes<M>,
    reads: crate::lens::Reads<M>,
    width: ColumnWidth,
    toggle: ColumnToggle,
    /// The source that decides which related records show, and whether a context's panel can
    /// load from it.
    source: Option<LabelSource>,
    /// Drops the related records the source does not show; `None` for an unscoped column.
    #[derive_where(skip(Debug))]
    restrict: Option<Restrict<M>>,
    /// What the column shows of the related records, which a panel checks at mount.
    related: Related,
    /// Whether a request may not view a field the column shows; `None` for one showing only a
    /// record's label or count.
    #[derive_where(skip(Debug))]
    masked: Option<Masked>,
}

impl<M> Declared<M> {
    fn of<R>(relation: &RelationLens<M, R>, width: ColumnWidth) -> Self {
        let (name, field_label, misdeclared) = match relation.field() {
            Ok((name, _)) => (name.to_string(), crate::Text::field(name), None),
            Err(error) => (String::new(), crate::Text::from(""), Some(error)),
        };
        Self {
            name,
            label: None,
            field_label,
            misdeclared,
            includes: relation.includes().clone(),
            reads: relation.reads(),
            width,
            toggle: ColumnToggle::Fixed,
            source: None,
            restrict: None,
            related: Related::Unmasked,
            masked: None,
        }
    }

    /// Show only the related records `S` shows, which `restrict` keeps, by their label or count
    /// until [`masked`](Self::masked) says what else shows.
    fn scoped<S: OptionSource>(self, restrict: Restrict<M>) -> Self {
        Self {
            source: Some((std::any::type_name::<S>(), S::available)),
            restrict: Some(restrict),
            related: Related::Source(std::any::TypeId::of::<S>()),
            ..self
        }
    }

    /// Show the fields of the related records `masked` hides from a request it refuses.
    fn masked(self, masked: Masked) -> Self {
        Self {
            masked: Some(masked),
            ..self
        }
    }

    /// Show no field of the related records.
    fn fieldless(self) -> Self {
        Self {
            related: Related::None,
            ..self
        }
    }

    fn label(&self) -> crate::Text {
        self.label
            .clone()
            .unwrap_or_else(|| self.field_label.clone())
    }

    fn relabel(&mut self, label: crate::Text) {
        self.name = label.key();
        self.label = Some(label);
    }

    /// The text of a row's loaded relation. A scoped column renders empty when its row holds no
    /// record, which is how a required related record the source hides reads. An unscoped one
    /// renders the unloaded marker, since a loader skipped the include it declares.
    fn text(&self, loaded: Option<String>) -> String {
        if self.restrict.is_some() {
            return loaded.unwrap_or_default();
        }
        debug_assert!(
            loaded.is_some(),
            "the `{}` column declares its include, but its row was loaded without it",
            self.name
        );
        loaded.unwrap_or_else(|| UNLOADED.to_string())
    }
}

/// The builders and the [`Column`] impl both relation columns share.
macro_rules! relation_column {
    ($ty:ident) => {
        impl<M> $ty<M> {
            /// Replace the label the field's name gives it, and name the column after the label,
            /// as a [`ComputedColumn`](super::ComputedColumn) is: two columns over one relation
            /// need two labels.
            pub fn label(mut self, label: impl Into<crate::Text>) -> Self {
                self.relation.relabel(label.into());
                self
            }

            /// Declare this column's width.
            pub fn width(mut self, width: ColumnWidth) -> Self {
                self.relation.width = width;
                self
            }

            toggle_builders!(relation.toggle);
        }

        impl<M> Column<M> for $ty<M>
        where
            M: toasty::schema::Model + Send + Sync + 'static,
        {
            fn name(&self) -> &str {
                &self.relation.name
            }

            fn label(&self) -> crate::Text {
                self.relation.label()
            }

            fn text(&self, cx: &Cx, row: &M) -> String {
                self.relation.text(self.loaded_text(cx, row))
            }

            fn column_width(&self) -> ColumnWidth {
                self.relation.width
            }

            fn toggle(&self) -> ColumnToggle {
                self.relation.toggle
            }

            fn includes(&self) -> Includes<M> {
                self.relation.includes.clone()
            }

            fn reads(&self) -> Option<crate::lens::Reads<M>> {
                Some(self.relation.reads.clone())
            }

            fn restrict<'a>(
                &'a self,
                cx: &'a Cx,
                rows: &'a [M],
                ex: &'a mut dyn Executor,
            ) -> BoxFuture<'a, Result<()>> {
                match &self.relation.restrict {
                    Some(restrict) => restrict(cx, rows, ex),
                    None => Box::pin(async { Ok(()) }),
                }
            }

            fn masked(&self, cx: &Cx) -> bool {
                self.relation
                    .masked
                    .as_ref()
                    .is_some_and(|masked| masked(cx))
            }

            fn related(&self) -> Related {
                self.relation.related
            }

            fn misdeclared(&self) -> Option<DeclarationErrorKind> {
                self.relation.misdeclared.clone()
            }

            fn unavailable_source(&self, cx: &Cx) -> Option<&'static str> {
                self.relation
                    .source
                    .filter(|(_, available)| !available(cx))
                    .map(|(source, _)| source)
            }
        }
    };
}

relation_column!(RelationColumn);
relation_column!(CountColumn);

#[cfg(test)]
mod tests;