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
//! The models Tablo ships, for auth and the bell, and [`models!`](crate::models!), which registers
//! them beside the app's.
use crate::{
audit::AuditEntry,
auth::{AdminUser, AuthFactor, AuthSession, AuthThrottle, AuthToken, ColumnPreference},
notification::StoredNotification,
};
/// Register every model Tablo ships in `models`: [`AdminUser`], [`AuthSession`],
/// [`ColumnPreference`], [`AuthToken`], [`AuthFactor`], [`AuthThrottle`], [`StoredNotification`]
/// and [`AuditEntry`].
///
/// [`models!`](crate::models!) calls it; call it directly when the app builds its
/// [`ModelSet`](toasty::schema::ModelSet) by hand. A model the panel never reads costs an empty
/// table and nothing else.
///
/// Toasty names each table after its model, so an app with a model of one of these names, such
/// as its own `AdminUser` beside a custom authenticator, cannot register both: it lists the models
/// its panel reads in [`toasty::models!`] instead.
pub fn register_models(models: &mut toasty::schema::ModelSet) {
<AdminUser as toasty::schema::Model>::register(models);
<AuthSession as toasty::schema::Model>::register(models);
<ColumnPreference as toasty::schema::Model>::register(models);
<AuthToken as toasty::schema::Model>::register(models);
<AuthFactor as toasty::schema::Model>::register(models);
<AuthThrottle as toasty::schema::Model>::register(models);
<StoredNotification as toasty::schema::Model>::register(models);
<AuditEntry as toasty::schema::Model>::register(models);
}
/// [`toasty::models!`] with the models Tablo ships appended, so a panel's `Db` holds every table
/// auth reads.
///
/// It takes what `toasty::models!` takes: the app's models, and `crate::*` or `some_crate::*` for
/// every model a crate declares. A `Db` built from it never fails the mount with
/// `MissingAuthModels`. An app with a model named as one of Tablo's lists them in
/// `toasty::models!` instead: see [`register_models`](crate::register_models).
///
/// ```rust,no_run
/// # #[derive(Debug, Clone, toasty::Model)]
/// # struct Post {
/// # #[key]
/// # #[auto]
/// # id: uuid::Uuid,
/// # }
/// # async fn connect() -> toasty::Result<toasty::Db> {
/// toasty::Db::builder()
/// .models(tablo_core::models!(Post))
/// .connect("sqlite::memory:")
/// .await
/// # }
/// ```
#[macro_export]
macro_rules! models {
($($models:tt)*) => {{
let mut models = ::toasty::models!($($models)*);
$crate::register_models(&mut models);
models
}};
}