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, AuthThrottle, AuthToken, ColumnPreference},
7 notification::StoredNotification,
8};
9
10/// Register every model Tablo ships in `models`: [`AdminUser`], [`AuthSession`],
11/// [`ColumnPreference`], [`AuthToken`], [`AuthFactor`], [`AuthThrottle`], [`StoredNotification`]
12/// and [`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 <AuthThrottle as toasty::schema::Model>::register(models);
28 <StoredNotification as toasty::schema::Model>::register(models);
29 <AuditEntry as toasty::schema::Model>::register(models);
30}
31
32/// [`toasty::models!`] with the models Tablo ships appended, so a panel's `Db` holds every table
33/// auth reads.
34///
35/// It takes what `toasty::models!` takes: the app's models, and `crate::*` or `some_crate::*` for
36/// every model a crate declares. A `Db` built from it never fails the mount with
37/// `MissingAuthModels`. An app with a model named as one of Tablo's lists them in
38/// `toasty::models!` instead: see [`register_models`](crate::register_models).
39///
40/// ```rust,no_run
41/// # #[derive(Debug, Clone, toasty::Model)]
42/// # struct Post {
43/// # #[key]
44/// # #[auto]
45/// # id: uuid::Uuid,
46/// # }
47/// # async fn connect() -> toasty::Result<toasty::Db> {
48/// toasty::Db::builder()
49/// .models(tablo_core::models!(Post))
50/// .connect("sqlite::memory:")
51/// .await
52/// # }
53/// ```
54#[macro_export]
55macro_rules! models {
56 ($($models:tt)*) => {{
57 let mut models = ::toasty::models!($($models)*);
58 $crate::register_models(&mut models);
59 models
60 }};
61}