tablo_core/table/model.rs
1//! [`PanelModel`]: what a panel needs from a model a resource serves.
2
3use toasty::{schema::Model, stmt::IntoExpr};
4
5use crate::{Detail, Table};
6
7/// What a panel needs from a model a resource serves: its tenant columns, its version counter,
8/// how to copy a record without its relations, and the table and detail page it reads as when its
9/// resource declares none, which a [`ResourceDef`](crate::ResourceDef) without a
10/// [`table`](crate::ResourceDef::table) lists and without a [`detail`](crate::ResourceDef::detail)
11/// shows.
12///
13/// Derive it with [`PanelModel`](derive@crate::PanelModel), which gives a column to each field
14/// a person reads, whether a form or Toasty writes it. An impl by hand names the model's tenant
15/// columns; one that keeps the defaults here lists no column, for a resource that declares its own
16/// table and detail page, or turns the view page off.
17#[diagnostic::on_unimplemented(
18 message = "`{Self}` is not a panel model",
19 label = "a resource's model implements `PanelModel`",
20 note = "add `#[derive(tablo::PanelModel)]` to `{Self}`, or `impl tablo::PanelModel for \
21 {Self} {{ const TENANT_COLUMNS: &[&str] = &[..]; }}`, naming its `TenantId` fields, \
22 when its resources declare their own table and detail page"
23)]
24pub trait PanelModel: Model + IntoExpr<Self> + Sized {
25 /// The model's [`TenantId`](crate::TenantId) fields, by name: the derive lists each field
26 /// typed `TenantId` or `Option<TenantId>`. Mounting a panel refuses a resource over a model
27 /// that lists one and declares no [`tenancy`](crate::ResourceDef::tenancy), which would serve
28 /// every tenant's rows. It has no default, so an impl by hand states it.
29 const TENANT_COLUMNS: &'static [&'static str];
30
31 /// The default table, which lists no column here.
32 fn table() -> Table<Self> {
33 Table::new(())
34 }
35
36 /// The default detail page, which lists no column here: a resource serving the view page then
37 /// declares its own, else mounting refuses it.
38 fn detail() -> Detail<Self> {
39 Detail::empty()
40 }
41
42 /// The record's `#[version]` counter, or `None` for a model without one.
43 ///
44 /// The edit form carries it, so a save refuses a record another save changed since the form
45 /// opened. The derive reads the model's `#[version]` field; an impl written by hand keeps
46 /// this default unless it returns the counter itself.
47 fn version(&self) -> Option<u64> {
48 None
49 }
50
51 /// Whether [`bare`](Self::bare) leaves out every relation the model holds. The derive
52 /// answers `true` when each relation is a `Deferred` one; an impl by hand keeps this default
53 /// unless its `bare` leaves them out.
54 ///
55 /// A panel then hands a closure over the row that declares it reads none, such as a
56 /// [`row_color`](crate::Table::row_color), a copy of the row without them, so the closure
57 /// mounts beside a column loading a record of a model a resource restricts a field of. Such a
58 /// closure reaching a relation anyway panics at render, where it read the relation another
59 /// column loaded before: declare the relations it reads.
60 ///
61 /// An eager relation, typed without `Deferred`, holds its records on every query, so a model
62 /// with one answers `false` and keeps the refusal.
63 const UNLOADS_RELATIONS: bool = false;
64
65 /// A copy of the record without its relations, holding its own fields: the derive builds it
66 /// field by field, leaving each `Deferred` relation unloaded rather than copying its records.
67 #[must_use]
68 fn bare(&self) -> Self
69 where
70 Self: Clone,
71 {
72 self.clone()
73 }
74
75 /// The record's foreign keys and [`TenantId`](crate::TenantId) fields, by name, each spelled
76 /// as the database stores it, `None` where it holds nothing.
77 ///
78 /// The default table leaves them out, so a [`ChangedField`](crate::ChangedField) reads them
79 /// here: moving a record to another owner or tenant is a change a write names. The derive
80 /// lists them; an impl by hand keeps this default, which lists none.
81 fn references(&self) -> Vec<(&'static str, Option<String>)> {
82 Vec::new()
83 }
84}
85
86/// A foreign key or tenant field's type, spelled for [`PanelModel::references`].
87#[doc(hidden)]
88pub trait Reference {
89 /// The value as the database stores it, `None` for an empty optional one.
90 fn spell(&self) -> Option<String>;
91}
92
93/// Spells each key type a foreign key or tenant field holds, and an `Option` of one.
94macro_rules! reference {
95 ($($ty:ty),* $(,)?) => {
96 $(
97 impl Reference for $ty {
98 fn spell(&self) -> Option<String> {
99 Some(self.to_string())
100 }
101 }
102
103 impl Reference for Option<$ty> {
104 fn spell(&self) -> Option<String> {
105 self.as_ref().map(ToString::to_string)
106 }
107 }
108 )*
109 };
110}
111
112reference!(
113 uuid::Uuid,
114 crate::TenantId,
115 String,
116 i8,
117 i16,
118 i32,
119 i64,
120 isize,
121 u8,
122 u16,
123 u32,
124 u64,
125 usize,
126);
127
128/// A copy of `row` without its relations: see [`PanelModel::UNLOADS_RELATIONS`].
129pub(crate) fn bare<M: PanelModel + Clone>(row: &M) -> M {
130 row.bare()
131}
132
133mod field;
134#[doc(hidden)]
135pub mod pick;
136
137pub use field::FieldColumn;
138
139#[cfg(test)]
140mod tests;