tablo-core 0.10.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
597
598
599
600
//! Registering a panel's resources and pages as it mounts: each resource's def is built once and
//! filled in, and every slug, route and sidebar entry follows from it.

use std::{any::TypeId, collections::HashMap, sync::Arc};

use topcoat::router::{PageFn, RouteFn};

use super::{
    Root,
    actions::{
        relation_action_options, relation_list_action, relation_record_action,
        resource_action_options, resource_bulk_delete, resource_delete, resource_export,
        resource_list_action, resource_options, resource_record_action,
    },
    area::AuthPage,
    build::validate_route_segment,
    detail::resource_view,
    forms::{
        ChildForm, resource_create, resource_create_post, resource_edit, resource_edit_post,
        resource_import, resource_import_post, resource_import_template, resource_picked,
    },
    list::{resource_columns, resource_list},
    pages::{page_action, page_action_options, page_handler},
    record_page::{record_page_view, relation_tab_view},
    relations::{Child, relation_table},
    state::{AreaNav, BadgeFn, NavEntry, ResourceTrail, Searchable},
};
use crate::{
    Ability, DeclarationError, DeclarationErrorKind, Page, PageDef, Site,
    form::RecordForm,
    naming::route_path,
    page::{MountedPage, PageCheck, check_page},
    panel::url::{
        ACTION_ROUTE_PARAM, ACTIONS_ROUTE_SEGMENT, BULK_DELETE_ROUTE_SEGMENT,
        COLUMNS_ROUTE_SEGMENT, CREATE_ROUTE_SEGMENT, DASH_ROUTE_SEGMENT, DELETE_ROUTE_SEGMENT,
        EDIT_ROUTE_SEGMENT, EXPORT_ROUTE_SEGMENT, OPTIONS_ROUTE_SEGMENT, PICKED_ROUTE_SEGMENT,
        RECORD_ROUTE_PARAM, RELATED_ROUTE_PARAM, RELATION_ROUTE_PARAM, RELATIONS_ROUTE_SEGMENT,
        import_template_url, import_url, list_route,
    },
    resource::{
        Mounted, Mounts, Places, Resource, ResourceCheck, ResourceDef, check_record_pages,
        check_resource,
    },
};

mod area;

pub(super) use area::AuditLogList;
mod form_facts;
mod links;

use form_facts::FormFacts;
use links::Link;

/// A resource or page waiting for its panel to mount.
pub(super) trait Registration: Send {
    fn register(self: Box<Self>, registry: &mut Registry);
}

/// Adjusts a resource's def for one panel.
type Customize<R> = Box<dyn FnOnce(ResourceDef<R>) -> ResourceDef<R> + Send>;

/// [`Panel::resource_with`](super::Panel::resource_with)'s pending resource.
pub(super) struct ResourceRegistration<R: Resource>(pub(super) Customize<R>);

/// Adjusts a page's def for one panel.
pub(super) type CustomizePage = Box<dyn FnOnce(PageDef) -> PageDef + Send>;

/// [`Panel::page_with`](super::Panel::page_with)'s pending page, or with `home` set,
/// [`Panel::home`](super::Panel::home)'s.
pub(super) struct PageRegistration<P> {
    pub(super) home: bool,
    pub(super) customize: CustomizePage,
    pub(super) _page: std::marker::PhantomData<fn() -> P>,
}

/// Everything a panel's registrations add up to.
pub(super) struct Registry {
    pub(super) prefix: String,
    schema: Option<crate::toasty_compat::model::AppSchema>,
    /// Every slug a resource, page or area mounts at, beside the URL it mounts under: the
    /// panel's prefix or an area's, each a namespace of its own.
    slugs: Vec<(String, String)>,
    /// The area the registrations now running land in; `None` lands them in the panel.
    scope: Option<area::Scope>,
    /// The panel's areas, in registration order.
    pub(super) areas: Vec<AreaNav>,
    /// The pages auth serves that an area holds, with the URL each mounts at.
    pub(super) auth_pages: Vec<(AuthPage, String)>,
    /// The audit log's list, until a registration places it.
    pub(super) audit_log: AuditLogList,
    /// The URL each resource and page serves at, by type.
    pub(super) urls: HashMap<TypeId, String>,
    /// The registered pages as the panel mounted them, by type.
    pub(super) mounted_pages: HashMap<TypeId, Arc<MountedPage>>,
    pub(super) nav_items: Vec<NavEntry>,
    /// The resources the global search searches, in registration order.
    pub(super) searches: Vec<Searchable>,
    pub(super) pages: Vec<PageFn>,
    pub(super) routes: Vec<RouteFn>,
    pub(super) root: Option<Root>,
    pub(super) mounts: Mounts,
    pub(super) resources: Vec<Registered>,
    /// Each registered page's declaration check.
    pub(super) page_checks: Vec<PageCheck>,
    /// Each resource's relation table, by resource type.
    pub(super) children: HashMap<TypeId, Child>,
    pub(super) errors: Vec<DeclarationError>,
}

/// One registered resource, its type erased.
pub(super) struct Registered {
    resource: TypeId,
    pub(super) name: &'static str,
    slug: String,
    /// The list's URL, `{base}/{slug}`.
    url: String,
    /// The declaration checks mount runs with the app's values.
    pub(super) check: ResourceCheck,
    /// The form's file fields, whose bytes only an installed uploader keeps.
    pub(super) file_fields: Vec<String>,
    /// The form's file fields' `max_bytes`, which the panel's body cap must exceed.
    pub(super) file_caps: Vec<(String, usize)>,
    /// Whether the list declares a toggleable column, whose choice a [`ColumnPreference`]
    /// keeps.
    ///
    /// [`ColumnPreference`]: crate::auth::ColumnPreference
    pub(super) toggles: bool,
    relations: Vec<Link>,
    /// The slugs of the resource's record pages, which its relation tabs may not take.
    record_page_slugs: Vec<String>,
    /// Serves the resource's relation tabs.
    relation_tab: topcoat::router::PageRenderFn,
    /// What a `has_one` relation's related form needs of the resource, as owner or as child.
    form: FormFacts,
}

impl Registry {
    pub(super) fn new(
        prefix: String,
        schema: Option<crate::toasty_compat::model::AppSchema>,
    ) -> Self {
        Self {
            prefix,
            schema,
            slugs: Vec::new(),
            scope: None,
            areas: Vec::new(),
            auth_pages: Vec::new(),
            audit_log: AuditLogList::NotKept,
            urls: HashMap::new(),
            mounted_pages: HashMap::new(),
            nav_items: Vec::new(),
            searches: Vec::new(),
            pages: Vec::new(),
            routes: Vec::new(),
            root: None,
            mounts: Mounts::default(),
            resources: Vec::new(),
            page_checks: Vec::new(),
            children: HashMap::new(),
            errors: Vec::new(),
        }
    }

    /// The URL the registrations now running mount under: the area's, else the panel's prefix.
    fn base(&self) -> &str {
        self.scope
            .as_ref()
            .map_or(self.prefix.as_str(), |scope| scope.base.as_str())
    }

    /// Claims `{base}/{slug}` for `T`, recording a refusal and returning `None` when the slug is
    /// unavailable.
    fn claim_slug<T: 'static>(&mut self, item: &'static str, slug: &str) -> Option<String> {
        let url = self.claim(item, slug, |kind| {
            DeclarationError::of::<T>(Site::Registration, kind)
        })?;
        self.urls.insert(TypeId::of::<T>(), url.clone());
        Some(url)
    }

    /// Claims `{base}/{slug}`, recording the refusal `refuse` builds and returning `None` when
    /// the slug is unavailable. Only the panel's own namespace reserves the routes auth and the
    /// panel serve under its prefix.
    fn claim(
        &mut self,
        item: &'static str,
        slug: &str,
        refuse: impl FnOnce(DeclarationErrorKind) -> DeclarationError,
    ) -> Option<String> {
        let base = self.base().to_string();
        let refused = if let Err(error) = validate_route_segment(item, slug) {
            Some(error)
        } else if self.scope.is_none() && RESERVED_SLUGS.contains(&slug) {
            Some(DeclarationErrorKind::ReservedSlug {
                slug: slug.to_string(),
            })
        } else if self
            .slugs
            .iter()
            .any(|(under, taken)| *under == base && taken == slug)
        {
            Some(DeclarationErrorKind::DuplicateSlug {
                slug: slug.to_string(),
            })
        } else {
            None
        };
        if let Some(error) = refused {
            self.errors.push(refuse(error));
            return None;
        }
        let url = format!("{base}/{slug}");
        self.slugs.push((base, slug.to_string()));
        Some(url)
    }

    /// Adds `entry` to the sidebar the registrations now running land in: the area's group, else
    /// the panel's, `first` putting it ahead of the panel's other entries.
    fn push_nav(&mut self, mut entry: NavEntry, first: bool) {
        match &self.scope {
            Some(scope) => {
                entry.item.group = Some(scope.group.clone());
                entry.area = Some(scope.slot);
                if scope.slot.tenant {
                    let visible = entry.visible;
                    entry.visible =
                        Arc::new(move |cx| crate::auth::membership(cx).is_some() && visible(cx));
                }
                self.nav_items.push(entry);
            }
            None if first => self.nav_items.insert(0, entry),
            None => self.nav_items.push(entry),
        }
    }

    /// Refuses a panel whose resources all leave the sidebar while no home page takes the root:
    /// the root, and the landing after a sign-in, would answer 404.
    pub(super) fn check_root(&mut self) {
        if self.root.is_none() && !self.resources.is_empty() {
            self.errors
                .push(DeclarationError::panel(DeclarationErrorKind::NoRoot));
        }
    }

    fn page(&mut self, method: http::Method, url: &str, handler: topcoat::router::PageRenderFn) {
        self.pages
            .push(PageFn::new(method, route_path(url), handler));
    }

    fn route(&mut self, method: http::Method, url: &str, handler: topcoat::router::RouteHandlerFn) {
        self.routes
            .push(RouteFn::new(method, route_path(url), handler));
    }
}

/// The segments the panel routes under its prefix itself, which no resource or page may take as its
/// slug: `-` leads the home page's action routes.
const RESERVED_SLUGS: &[&str] = &[
    "login",
    "logout",
    "register",
    "register-tenant",
    "tenant",
    "profile",
    "forgot-password",
    "reset-password",
    "verify-email",
    "email-change",
    "two-factor",
    "two-factor-challenge",
    "sign-in",
    "locale",
    "-",
];

impl<R: Resource> Registration for ResourceRegistration<R> {
    fn register(self: Box<Self>, registry: &mut Registry) {
        if registry.mounts.contains(TypeId::of::<R>()) {
            registry.errors.push(DeclarationError::of::<R>(
                Site::Registration,
                DeclarationErrorKind::DuplicateResource,
            ));
            return;
        }
        let Self(customize) = *self;
        let base = registry.base().to_string();
        let mounted = Mounted::new(customize(R::declare()), &base);
        let Some(url) = registry.claim_slug::<R>("ResourceDef::slug", &mounted.slug) else {
            return;
        };
        let entries = mounted.actions.entries();
        let routes = OptionalRoutes {
            create: mounted.pages.create,
            import: mounted.pages.create && mounted.imports,
            record: entries.iter().any(|a| a.places.intersects(Places::RECORD)),
            list: !mounted.header_actions.is_empty()
                || entries.iter().any(|a| a.places.contains(Places::BULK)),
            links: mounted
                .relations
                .iter()
                .any(|relation| relation.links().is_some()),
            columns: mounted.table.has_toggleable_columns(),
        };
        let toggles = routes.columns;
        register_routes::<R>(registry, &url, routes);
        let pages = &mounted.record_pages;
        let validate = |slug: &str| validate_route_segment("RecordPageDef::slug", slug).err();
        if check_record_pages(pages, validate, &mut registry.errors) {
            for page in pages {
                let route = format!("{url}/{RECORD_ROUTE_PARAM}/{}", page.slug);
                registry.page(http::Method::GET, &route, record_page_view::<R>);
            }
        }
        if registry.root.is_none() && registry.scope.is_none() && !mounted.navigation_hidden {
            registry.root = Some(Root::Redirect(url.clone()));
        }
        if !mounted.navigation_hidden {
            let entry = NavEntry {
                item: mounted.navigation.clone(),
                visible: Arc::new(|cx| crate::can::<R>(cx, Ability::ViewAny)),
                resource_list: Some(ResourceTrail {
                    list: url,
                    viewed: mounted.viewed(),
                    pages: pages
                        .iter()
                        .map(|page| (page.slug.clone(), page.label.clone()))
                        .collect(),
                }),
                badge: mounted.counts_navigation_badge().then(|| -> BadgeFn {
                    Arc::new(|cx| {
                        Box::pin(async move {
                            crate::resource::mounted::<R>(cx)?
                                .navigation_badge(cx)
                                .await
                        })
                    })
                }),
                area: None,
            };
            registry.push_nav(entry, false);
        }
        if mounted.globally_searched() {
            registry.searches.push(Searchable {
                label: mounted.plural_label.clone(),
                list: mounted.url.clone(),
                visible: crate::resource::searchable::<R>,
                search: crate::resource::search::<R>,
            });
        }
        registry.children.insert(
            TypeId::of::<R>(),
            Child {
                slug: mounted.slug.clone(),
                label: mounted.label.clone(),
                plural_label: mounted.plural_label.clone(),
                render: relation_table::<R>,
                listable: crate::can_list::<R>,
                hidden: |cx| {
                    crate::resource::mounted::<R>(cx)
                        .map(|mounted| mounted.field_mask(cx).hidden().to_vec())
                },
                form: ChildForm::of::<R>(),
            },
        );
        registry.resources.push(Registered {
            resource: TypeId::of::<R>(),
            name: std::any::type_name::<R>(),
            slug: mounted.slug.clone(),
            url: mounted.url.clone(),
            check: check_resource::<R>,
            toggles,
            file_fields: mounted
                .form
                .fields()
                .filter(|field| field.is_file())
                .map(|field| field.name().to_string())
                .collect(),
            file_caps: mounted.form.file_caps(),
            relations: mounted
                .relations
                .iter()
                .map(|relation| Link::of::<R>(relation, registry.schema.as_ref()))
                .collect(),
            record_page_slugs: mounted
                .record_pages
                .iter()
                .map(|page| page.slug.clone())
                .collect(),
            relation_tab: relation_tab_view::<R>,
            form: FormFacts::of(&mounted),
        });
        registry.mounts.insert(Arc::new(mounted));
    }
}

/// Which optional routes a resource serves.
struct OptionalRoutes {
    /// The create page: see [`ResourcePage::Create`](crate::ResourcePage::Create).
    create: bool,
    /// The CSV import beside the create page: [`importable`](crate::ResourceDef::importable).
    import: bool,
    /// One record's: an action placed on a row or a detail or edit page.
    record: bool,
    /// The list's: a header action, or an action placed on the bulk bar.
    list: bool,
    /// A record's many-to-many relations': attach and detach.
    links: bool,
    /// The column picker's: a toggleable column on the list.
    columns: bool,
}

/// Registers a resource's routes under its list `url`.
fn register_routes<R: Resource>(registry: &mut Registry, url: &str, routes: OptionalRoutes) {
    use http::Method;

    registry.page(Method::GET, url, resource_list::<R>);
    // The handler 404s a resource that declares no view. `matchit` prefers a static segment over
    // the `{id}` parameter, so the list's own routes sit under `-` (`{url}/-/create`): a record
    // whose key is `create` keeps its detail page.
    registry.page(
        Method::GET,
        &format!("{url}/{RECORD_ROUTE_PARAM}"),
        resource_view::<R>,
    );
    registry.page(
        Method::POST,
        &format!("{url}/{RECORD_ROUTE_PARAM}/{DELETE_ROUTE_SEGMENT}"),
        resource_delete::<R>,
    );
    registry.page(
        Method::POST,
        &list_route(url, BULK_DELETE_ROUTE_SEGMENT),
        resource_bulk_delete::<R>,
    );
    if routes.columns {
        registry.page(
            Method::POST,
            &list_route(url, COLUMNS_ROUTE_SEGMENT),
            resource_columns::<R>,
        );
    }
    // The `-` segment keeps `actions` from shadowing the edit and delete
    // routes of a record whose key is `actions`.
    if routes.record {
        registry.page(
            Method::POST,
            &format!(
                "{url}/{RECORD_ROUTE_PARAM}/{DASH_ROUTE_SEGMENT}/{ACTIONS_ROUTE_SEGMENT}/{ACTION_ROUTE_PARAM}"
            ),
            resource_record_action::<R>,
        );
    }
    if routes.list {
        registry.page(
            Method::POST,
            &list_actions_route(url),
            resource_list_action::<R>,
        );
    }
    // A record's action asks for the same input as the list's, so one route serves both.
    if routes.record || routes.list {
        registry.route(
            Method::GET,
            &format!("{}/options", list_actions_route(url)),
            resource_action_options::<R>,
        );
    }
    if routes.links {
        // `{url}/{id}/-/relations/{relation}`: the `-` segment keeps it from a record's own
        // routes, as it does the record's actions.
        let relation = format!(
            "{url}/{RECORD_ROUTE_PARAM}/{DASH_ROUTE_SEGMENT}/{RELATIONS_ROUTE_SEGMENT}/{RELATION_ROUTE_PARAM}"
        );
        registry.page(
            Method::POST,
            &list_actions_route(&relation),
            relation_list_action::<R>,
        );
        registry.page(
            Method::POST,
            &format!(
                "{relation}/{RELATED_ROUTE_PARAM}/{DASH_ROUTE_SEGMENT}/{ACTIONS_ROUTE_SEGMENT}/{ACTION_ROUTE_PARAM}"
            ),
            relation_record_action::<R>,
        );
        registry.route(
            Method::GET,
            &format!("{}/options", list_actions_route(&relation)),
            relation_action_options::<R>,
        );
    }
    registry.route(
        Method::GET,
        &list_route(url, EXPORT_ROUTE_SEGMENT),
        resource_export::<R>,
    );
    if routes.create {
        let create_url = list_route(url, CREATE_ROUTE_SEGMENT);
        registry.page(Method::GET, &create_url, resource_create::<R>);
        registry.page(Method::POST, &create_url, resource_create_post::<R>);
        registry.page(
            Method::GET,
            &list_route(url, PICKED_ROUTE_SEGMENT),
            resource_picked::<R>,
        );
    }
    if routes.import {
        let import_url = import_url(url);
        registry.page(Method::GET, &import_url, resource_import::<R>);
        registry.page(Method::POST, &import_url, resource_import_post::<R>);
        registry.route(
            Method::GET,
            &import_template_url(url),
            resource_import_template::<R>,
        );
    }
    if <R::Form as RecordForm>::HAS_FORM {
        let edit_url = format!("{url}/{RECORD_ROUTE_PARAM}/{EDIT_ROUTE_SEGMENT}");
        registry.page(Method::GET, &edit_url, resource_edit::<R>);
        registry.page(Method::POST, &edit_url, resource_edit_post::<R>);
        registry.route(
            Method::GET,
            &list_route(url, OPTIONS_ROUTE_SEGMENT),
            resource_options::<R>,
        );
    }
}

/// The route of the actions under a list or page `url`: `{url}/-/actions/{action}`.
pub(super) fn list_actions_route(url: &str) -> String {
    format!(
        "{}/{ACTION_ROUTE_PARAM}",
        list_route(url, ACTIONS_ROUTE_SEGMENT)
    )
}

impl<P: Page> Registration for PageRegistration<P> {
    fn register(self: Box<Self>, registry: &mut Registry) {
        if registry.mounted_pages.contains_key(&TypeId::of::<P>()) {
            registry.errors.push(DeclarationError::of::<P>(
                Site::Registration,
                DeclarationErrorKind::DuplicatePage,
            ));
            return;
        }
        let Self {
            home, customize, ..
        } = *self;
        let page = Arc::new(MountedPage::new::<P>(customize(P::declare())));
        let url = if home {
            if matches!(registry.root, Some(Root::Home)) {
                registry.errors.push(DeclarationError::of::<P>(
                    Site::Registration,
                    DeclarationErrorKind::SecondHome,
                ));
                return;
            }
            registry.root = Some(Root::Home);
            registry
                .urls
                .insert(TypeId::of::<P>(), registry.prefix.clone());
            registry.prefix.clone()
        } else {
            let Some(url) = registry.claim_slug::<P>("PageDef::slug", &page.slug) else {
                return;
            };
            url
        };
        registry.page(http::Method::GET, &url, page_handler::<P>);
        if !page.header_actions.is_empty() {
            registry.page(
                http::Method::POST,
                &list_actions_route(&url),
                page_action::<P>,
            );
            registry.route(
                http::Method::GET,
                &format!("{}/options", list_actions_route(&url)),
                page_action_options::<P>,
            );
        }
        registry
            .page_checks
            .push(check_page::<P>(Arc::clone(&page)));
        let item = NavEntry {
            item: page.navigation.clone().resolved(&url),
            visible: page.access(),
            resource_list: None,
            badge: None,
            area: None,
        };
        let listed = !page.navigation_hidden;
        registry.mounted_pages.insert(TypeId::of::<P>(), page);
        if listed {
            registry.push_nav(item, home);
        }
    }
}