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