tablo_core/models.rs
1//! The models Tablo ships, for auth and the bell, and [`models!`](crate::models!), which registers
2//! them beside the app's.
3
4use crate::{
5 auth::{AdminUser, AuthFactor, AuthSession, AuthToken, ColumnPreference},
6 notification::StoredNotification,
7};
8
9/// Register every model Tablo ships in `models`: [`AdminUser`], [`AuthSession`],
10/// [`ColumnPreference`], [`AuthToken`], [`AuthFactor`] and [`StoredNotification`].
11///
12/// [`models!`](crate::models!) calls it; call it directly when the app builds its
13/// [`ModelSet`](toasty::schema::ModelSet) by hand. A model the panel never reads costs an empty
14/// table and nothing else.
15///
16/// Toasty names each table after its model, so an app with a model of one of these names, such
17/// as its own `AdminUser` beside a custom authenticator, cannot register both: it lists the models
18/// its panel reads in [`toasty::models!`] instead.
19pub fn register_models(models: &mut toasty::schema::ModelSet) {
20 <AdminUser as toasty::schema::Model>::register(models);
21 <AuthSession as toasty::schema::Model>::register(models);
22 <ColumnPreference as toasty::schema::Model>::register(models);
23 <AuthToken as toasty::schema::Model>::register(models);
24 <AuthFactor as toasty::schema::Model>::register(models);
25 <StoredNotification as toasty::schema::Model>::register(models);
26}
27
28/// [`toasty::models!`] with the models Tablo ships appended, so a panel's `Db` holds every table
29/// auth reads.
30///
31/// It takes what `toasty::models!` takes: the app's models, and `crate::*` or `some_crate::*` for
32/// every model a crate declares. A `Db` built from it never fails the mount with
33/// `MissingAuthModels`. An app with a model named as one of Tablo's lists them in
34/// `toasty::models!` instead: see [`register_models`](crate::register_models).
35///
36/// ```rust,no_run
37/// # #[derive(Debug, Clone, toasty::Model)]
38/// # struct Post {
39/// # #[key]
40/// # #[auto]
41/// # id: uuid::Uuid,
42/// # }
43/// # async fn connect() -> toasty::Result<toasty::Db> {
44/// toasty::Db::builder()
45/// .models(tablo_core::models!(Post))
46/// .connect("sqlite::memory:")
47/// .await
48/// # }
49/// ```
50#[macro_export]
51macro_rules! models {
52 ($($models:tt)*) => {{
53 let mut models = ::toasty::models!($($models)*);
54 $crate::register_models(&mut models);
55 models
56 }};
57}