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
//! Columns, which a [`Table`](crate::Table) lists and a [`Detail`](crate::Detail) shows: the
//! [`Column`] trait, the built-in [`TextColumn`], [`DateColumn`], [`ComputedColumn`],
//! [`RelationColumn`], [`CountColumn`], [`BooleanColumn`], [`FileColumn`], [`ColorColumn`],
//! [`ImageColumn`], [`EmbeddedColumn`], [`RepeaterColumn`] and [`RichTextColumn`], and the
//! [`IntoColumns`] seam.
//!
//! The module sits below both: `table` re-exports it, and `detail` implements
//! [`IntoDetail`](crate::IntoDetail) for each built-in column.

use std::sync::Arc;

use derive_where::derive_where;
use toasty::stmt::{Expr, List, OrderByExpr, Path, Query};
use topcoat::{context::Cx, view::*};

use crate::{display::Alignment, schema::Binding};

/// The `label` and `width` builders of a column over a [`ColumnBase`], inside its inherent impl.
macro_rules! base_builders {
    () => {
        /// Replace the label the field's name gives it.
        pub fn label(mut self, label: impl Into<$crate::Text>) -> Self {
            self.base.label = Some(label.into());
            self
        }

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

        toggle_builders!(base.toggle);
    };
    (sortable) => {
        base_builders!();

        /// Make the header a sort link.
        pub fn sortable(mut self) -> Self {
            self.sortable = true;
            self
        }
    };
}

/// The `toggleable` and `hidden_by_default` builders of a column holding its [`ColumnToggle`] at
/// the field path given, inside its inherent impl.
macro_rules! toggle_builders {
    ($($field:ident).+) => {
        /// Let each reader hide this column from their list; it starts shown.
        pub fn toggleable(mut self) -> Self {
            self.$($field).+ = $crate::column::ColumnToggle::ShownByDefault;
            self
        }

        /// Let each reader show this column in their list; it starts hidden.
        pub fn hidden_by_default(mut self) -> Self {
            self.$($field).+ = $crate::column::ColumnToggle::HiddenByDefault;
            self
        }
    };
}

/// The [`Column`] methods a column over a [`ColumnBase`] answers from it, inside its `Column`
/// impl; `binding` adds the binding's `misdeclared`.
macro_rules! base_column_methods {
    () => {
        base_column_methods!(@shared);

        fn reads(&self) -> Option<$crate::lens::Reads<M>> {
            Some(self.base.reads())
        }
    };
    (binding) => {
        base_column_methods!();
        base_column_methods!(@misdeclared);
    };
    // A column that answers `reads` itself.
    (binding, reads) => {
        base_column_methods!(@shared);
        base_column_methods!(@misdeclared);
    };
    (@misdeclared) => {
        fn misdeclared(&self) -> Option<$crate::DeclarationErrorKind> {
            self.base.binding.misdeclared()
        }
    };
    (@shared) => {
        fn related(&self) -> $crate::column::Related {
            $crate::column::Related::None
        }

        fn name(&self) -> &str {
            self.base.name()
        }

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

        fn column_width(&self) -> $crate::column::ColumnWidth {
            self.base.width
        }

        fn toggle(&self) -> $crate::column::ColumnToggle {
            self.base.toggle
        }
    };
}

fn sort_order<M, T>(path: &Path<M, T>, descending: bool) -> OrderByExpr {
    if descending {
        path.clone().desc()
    } else {
        path.clone().asc()
    }
}

mod related;
pub use related::Related;
mod boolean;
mod color;
mod computed;
mod date;
mod embedded;
mod file;
mod image;
mod into_columns;
mod relation;
mod repeater;
#[cfg(feature = "rich-text")]
mod rich_text;
mod text;

pub use boolean::BooleanColumn;
pub use color::ColorColumn;
pub use computed::ComputedColumn;
pub use date::{DateColumn, DateValue};
pub use embedded::EmbeddedColumn;
pub use file::FileColumn;
pub use image::ImageColumn;
pub use into_columns::IntoColumns;
pub use relation::{CountColumn, RelationColumn, RelationLens, ToOneRelation};
pub use repeater::RepeaterColumn;
#[cfg(feature = "rich-text")]
pub use rich_text::RichTextColumn;
pub use text::{TextColumn, contains_expr};
#[cfg(test)]
use {color::is_css_color, text::escape_like_pattern};

/// One column of a table or a [`Detail`](crate::Detail) declares its label, its value read off
/// the record, and its query predicates.
///
/// ```rust
/// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
/// # struct User { #[key] #[auto] id: uuid::Uuid, name: String }
/// # use tablo_core::{Text, extend::Column};
/// # use topcoat::context::Cx;
/// struct Initials;
///
/// impl Column<User> for Initials {
///     fn name(&self) -> &str {
///         "initials"
///     }
///     fn label(&self) -> Text {
///         "Initials".into()
///     }
///     fn text(&self, _cx: &Cx, row: &User) -> String {
///         row.name
///             .split_whitespace()
///             .filter_map(|w| w.chars().next())
///             .collect()
///     }
/// }
/// ```
///
/// Only [`name`](Self::name), [`label`](Self::label) and
/// [`text`](Self::text) are required.
pub trait Column<M>: Send + Sync {
    /// The column's identifier, distinct within its table.
    fn name(&self) -> &str;

    /// The header text.
    fn label(&self) -> crate::Text;

    /// The row's value as plain text.
    fn text(&self, cx: &Cx, row: &M) -> String;

    /// The row's table cell.
    fn cell<'a>(&self, cx: &'a Cx, row: &M) -> BoxView<'a> {
        crate::schema::value_cell(cx, &self.text(cx, row))
    }

    /// The record's entry on a detail page: the [`label`](Self::label) over the
    /// [`cell`](Self::cell).
    fn entry<'a>(&self, cx: &'a Cx, row: &M) -> BoxView<'a> {
        crate::schema::read_only(cx, &self.label(), self.cell(cx, row))
    }

    /// The width the column claims.
    fn column_width(&self) -> ColumnWidth {
        ColumnWidth::Auto
    }

    /// Whether each reader may hide or show the column in their list, and how it starts. A
    /// detail page shows every column.
    fn toggle(&self) -> ColumnToggle {
        ColumnToggle::Fixed
    }

    /// Where the header and the cells align their content.
    fn alignment(&self) -> Alignment {
        Alignment::Start
    }

    /// Whether the column joins the table's search.
    fn is_searchable(&self) -> bool {
        false
    }

    /// The predicate a search for `term` adds.
    fn search_expr(&self, _term: &str) -> Option<Expr<bool>> {
        None
    }

    /// Whether the header links to a sort on this column.
    fn is_sortable(&self) -> bool {
        false
    }

    /// The ordering a sort on this column applies.
    fn order_by(&self, _descending: bool) -> Option<OrderByExpr> {
        None
    }

    /// The relations [`text`](Self::text), [`cell`](Self::cell) and [`entry`](Self::entry) read.
    fn includes(&self) -> Includes<M> {
        Includes::new()
    }

    /// Check which related records [`includes`](Self::includes) loaded into `rows` the related
    /// resource shows the user, before any cell renders: a cell shows only those. Every loader
    /// that applies the includes runs it through `ex`, the executor that loaded the rows.
    #[doc(hidden)]
    fn restrict<'a>(
        &'a self,
        _cx: &'a Cx,
        _rows: &'a [M],
        _ex: &'a mut dyn toasty::Executor,
    ) -> crate::BoxFuture<'a, topcoat::Result<()>> {
        Box::pin(async { Ok(()) })
    }

    /// The fields of the row [`text`](Self::text), [`cell`](Self::cell),
    /// [`entry`](Self::entry), [`search_expr`](Self::search_expr) and
    /// [`order_by`](Self::order_by) read, or `None` when the column does not say.
    ///
    /// A panel drops a column reading a field
    /// [`field_access`](crate::ResourceDef::field_access) hides from the user, and refuses to
    /// mount a panel restricting a field whose columns answer `None`. The built-in columns
    /// answer their own field.
    fn reads(&self) -> Option<crate::lens::Reads<M>> {
        None
    }

    /// Whether the column shows a field of a related record that the related resource's
    /// [`field_access`](crate::ResourceDef::field_access) hides from `cx`: a panel drops it then,
    /// as it drops one reading a hidden field of the row.
    #[doc(hidden)]
    fn masked(&self, _cx: &Cx) -> bool {
        false
    }

    /// What the column reads of the related records it loads: see [`Related`]. A column answers
    /// [`Related::Row`] unless it says otherwise, since a closure reading its row may reach any
    /// relation the page loads.
    #[doc(hidden)]
    fn related(&self) -> Related {
        Related::Row
    }

    /// What is wrong with this column's declaration.
    #[doc(hidden)]
    fn misdeclared(&self) -> Option<crate::DeclarationErrorKind> {
        None
    }

    /// The source the column labels its records by, when the context's panel cannot load from it.
    #[doc(hidden)]
    fn unavailable_source(&self, _cx: &Cx) -> Option<&'static str> {
        None
    }
}

/// The relations a [`Column`] reads off its row.
#[derive_where(Clone, Debug, Default)]
pub struct Includes<M>(
    Vec<crate::toasty_compat::UntypedInclude>,
    #[derive_where(skip(Debug))] std::marker::PhantomData<fn() -> M>,
);

impl<M> Includes<M> {
    /// No relation.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add `relation`.
    pub fn with<T>(mut self, relation: impl Into<toasty::stmt::Include<M, T>>) -> Self {
        let include: crate::toasty_compat::UntypedInclude = relation.into().into();
        if !self.0.contains(&include) {
            self.0.push(include);
        }
        self
    }

    /// The relations, in the order they were added.
    pub(crate) fn into_vec(self) -> Vec<crate::toasty_compat::UntypedInclude> {
        self.0
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

/// One [`UnregisteredLabelSource`](DeclarationErrorKind::UnregisteredLabelSource) per column
/// labelling its records by a source `cx`'s panel cannot load from.
pub(crate) fn unavailable_sources<'c, M: 'c>(
    cx: &Cx,
    columns: impl IntoIterator<Item = &'c dyn Column<M>>,
) -> Vec<crate::DeclarationErrorKind> {
    columns
        .into_iter()
        .filter_map(|column| {
            column.unavailable_source(cx).map(|source| {
                crate::DeclarationErrorKind::UnregisteredLabelSource {
                    column: column.name().to_string(),
                    source,
                }
            })
        })
        .collect()
}

pub(crate) fn include_relations<'c, M>(
    mut query: Query<List<M>>,
    columns: impl IntoIterator<Item = &'c BoxColumn<M>>,
) -> Query<List<M>>
where
    M: toasty::schema::Model + 'c,
{
    let mut seen: Vec<crate::toasty_compat::UntypedInclude> = Vec::new();
    for include in columns.into_iter().flat_map(|c| c.includes().into_vec()) {
        if !seen.contains(&include) {
            query = query.include(include.clone());
            seen.push(include);
        }
    }
    query
}

/// Run each column's [`restrict`](Column::restrict) over `rows`, which a query through
/// [`include_relations`] loaded.
pub(crate) async fn restrict_relations<'c, M>(
    cx: &Cx,
    columns: impl IntoIterator<Item = &'c BoxColumn<M>>,
    rows: &[M],
    ex: &mut dyn toasty::Executor,
) -> topcoat::Result<()>
where
    M: 'c,
{
    for column in columns {
        column.restrict(cx, rows, &mut *ex).await?;
    }
    Ok(())
}

/// What a column over one field declares beside its lens: the field's binding, the declared label
/// and the width.
#[derive(Clone, Debug)]
struct ColumnBase {
    binding: Binding,
    /// The declared label, over the binding's.
    label: Option<crate::Text>,
    width: ColumnWidth,
    toggle: ColumnToggle,
}

impl ColumnBase {
    /// Bind `path`, with no declared label.
    fn new<M, T>(path: &Path<M, T>, width: ColumnWidth) -> Self
    where
        M: toasty::schema::Model,
    {
        Self {
            binding: Binding::of(path),
            label: None,
            width,
            toggle: ColumnToggle::Fixed,
        }
    }

    fn name(&self) -> &str {
        self.binding.name()
    }

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

    /// The field the column reads.
    fn reads<M>(&self) -> crate::lens::Reads<M> {
        self.binding.reads()
    }
}

/// How a [`Column`] takes the table's width.
///
/// The table lays out automatically: each column is as wide as its content, and the first column
/// that [`Grow`](Self::Grow)s takes the width left over. A table wider than its card scrolls
/// sideways.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ColumnWidth {
    /// As wide as its content, on one line: a status, date, number or boolean.
    #[default]
    Auto,
    /// As wide as its content up to 20rem, past which a value truncates; the first growing column
    /// also takes the table's spare width. The default for text and relation columns.
    Grow,
    /// Wrap the content onto several lines, between 12rem and 28rem wide.
    Wrap,
    /// Exactly this many rem; a value past that truncates.
    Rem(u8),
}

/// Whether each reader may hide a [`Column`] from their list, and whether it starts shown.
///
/// A list with a toggleable column renders a Columns menu in its toolbar, whose Apply keeps the
/// checked columns and Reset restores the defaults. A panel keeps each signed-in user's choice per
/// resource, in the [`ColumnPreference`](crate::auth::ColumnPreference) model, so the relation
/// tables over a resource show what its list shows; a column added later, or whose default changes,
/// takes its default until the reader picks otherwise. Search, sort and export still read every
/// column. A panel without auth, and a table a page renders with
/// [`wired_table`](crate::wired_table), render no menu and show the defaults.
///
/// ```rust
/// # #[derive(Debug, Clone, toasty::Model, tablo_core::ModelColumns)]
/// # struct Post { #[key] #[auto] id: uuid::Uuid, title: String, slug: String }
/// use tablo_core::{TextColumn, lens};
///
/// TextColumn::new(lens!(Post.title)).toggleable();
/// TextColumn::new(lens!(Post.slug)).hidden_by_default();
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ColumnToggle {
    /// Always shown.
    #[default]
    Fixed,
    /// Shown until the reader hides it: `toggleable()`.
    ShownByDefault,
    /// Hidden until the reader shows it: `hidden_by_default()`.
    HiddenByDefault,
}

impl ColumnToggle {
    /// Whether the reader may hide or show the column.
    pub(crate) fn is_toggleable(self) -> bool {
        self != Self::Fixed
    }
}

impl ColumnWidth {
    /// The classes of the box a body cell wraps its content in, if any.
    pub(crate) fn content_class(self) -> Option<&'static str> {
        match self {
            Self::Auto => None,
            Self::Grow => Some("max-w-xs truncate"),
            Self::Wrap => Some("min-w-48 max-w-md whitespace-normal"),
            Self::Rem(_) => Some("truncate"),
        }
    }

    /// The `style` of a cell of this width, header and body alike.
    pub(crate) fn style(self) -> Option<String> {
        match self {
            Self::Rem(rem) => Some(format!("width: {rem}rem; max-width: {rem}rem")),
            Self::Auto | Self::Grow | Self::Wrap => None,
        }
    }
}

/// A table's columns, as the table stores them.
pub(crate) type BoxColumn<M> = Arc<dyn Column<M>>;

/// Whether `column` shows to `cx`, which may not view the row's `hidden` fields: it reads none of
/// them, and no field of a related record its resource hides from `cx`.
pub(crate) fn shows<M>(cx: &Cx, column: &dyn Column<M>, hidden: &[String]) -> bool {
    crate::lens::shows(column.reads(), hidden) && !column.masked(cx)
}

#[cfg(test)]
mod tests;