tablo_core/table/model/field.rs
1//! [`FieldColumn`]: one model field, as a model's derived `columns()` hands it over.
2
3use derive_where::derive_where;
4use toasty::{
5 schema::{Field, Model},
6 stmt::List,
7};
8
9use crate::{
10 BooleanColumn, ColorColumn, DateColumn, DateValue, Detail, EmbeddedColumn, FileColumn,
11 IconColumn, ImageColumn, IntoColumns, IntoDetail, Lens, ListColumn, RepeaterColumn,
12 SelectColumn, Text, TextColumn, TextInputColumn, ToggleColumn,
13 column::BoxColumn,
14 schema::{EmbeddedForm, FormInput, Options},
15 values::FormScalar,
16};
17
18/// One field of a model, as the [`PanelModel`](derive@crate::PanelModel) derive's
19/// `columns()` hands it over: `Product::columns().qty`.
20///
21/// Placed as it is, in a [`Table`](crate::Table) or on a [`Detail`] page, it is the column the
22/// derive picks for the field's type there, with the field's `#[columns(..)]` keys applied: the
23/// column the model's default table or detail page shows. A kind method, such as
24/// [`text`](Self::text), [`date`](Self::date) or [`toggle`](Self::toggle), is that column type's
25/// own builder over the field instead, labelled by the field's `#[columns(label = "..")]`; each
26/// compiles only for a field type its column reads.
27///
28/// ```rust
29/// # #[derive(Debug, Clone, toasty::Model, tablo_core::PanelModel)]
30/// # struct Product { #[key] #[auto] id: uuid::Uuid, name: String, qty: i64, active: bool }
31/// use tablo_core::Table;
32///
33/// let columns = Product::columns();
34/// Table::new((
35/// // The derive's pick: a sortable, searchable text.
36/// columns.name,
37/// // A text column of the derive's, reshaped.
38/// columns.qty.text().numeric(0).sortable(),
39/// // A switch saving the record as it flips, in place of the derive's yes or no.
40/// columns.active.toggle(),
41/// ));
42/// ```
43///
44/// [`lens`](Self::lens) hands over the field's [`Lens`] for a filter, a tab or a field access
45/// rule; [`lens!`](crate::lens!) builds one for a path the derive does not reach, such as an
46/// embedded value's own field.
47#[derive_where(Clone)]
48pub struct FieldColumn<M, T, P = T> {
49 lens: Lens<M, T, P>,
50 label: Option<Text>,
51 table: fn() -> Vec<BoxColumn<M>>,
52 detail: fn() -> Detail<M>,
53}
54
55impl<M, T, P> FieldColumn<M, T, P> {
56 /// The field `lens` reads, labelled `label`, placed in a table as `table` builds it and on a
57 /// detail page as `detail` does; only the derive builds one.
58 #[doc(hidden)]
59 pub fn new(
60 lens: Lens<M, T, P>,
61 label: Option<Text>,
62 table: fn() -> Vec<BoxColumn<M>>,
63 detail: fn() -> Detail<M>,
64 ) -> Self {
65 Self {
66 lens,
67 label,
68 table,
69 detail,
70 }
71 }
72
73 /// The field's lens: its query path paired with its reader.
74 pub fn lens(&self) -> Lens<M, T, P> {
75 self.lens.clone()
76 }
77}
78
79/// The kind methods of [`FieldColumn`]: each binds its column type to the field, labelled by the
80/// field's `label` key.
81macro_rules! kinds {
82 ($($(#[$doc:meta])* $method:ident => $column:ty;)+) => {
83 $(
84 $(#[$doc])*
85 pub fn $method(self) -> $column {
86 let column = <$column>::new(self.lens);
87 match self.label {
88 Some(label) => column.label(label),
89 None => column,
90 }
91 }
92 )+
93 };
94}
95
96impl<M, T> FieldColumn<M, T>
97where
98 M: Model,
99 T: FormScalar + Send + Sync + 'static,
100{
101 kinds! {
102 /// A [`TextColumn`]: the value's text, or an `Options` enum's label.
103 text => TextColumn<M, T>;
104 /// An [`IconColumn`]: an `Options` enum's icon in place of its label.
105 icon => IconColumn<M, T>;
106 /// A [`TextInputColumn`]: a text input saving the record as it changes.
107 text_input => TextInputColumn<M, T>;
108 }
109}
110
111impl<M, T> FieldColumn<M, T>
112where
113 M: Model,
114 T: DateValue,
115{
116 kinds! {
117 /// A [`DateColumn`]: the value's calendar day.
118 date => DateColumn<M, T>;
119 }
120}
121
122impl<M, T> FieldColumn<M, T>
123where
124 M: Model,
125 T: Options + FormScalar + Send + Sync + 'static,
126{
127 kinds! {
128 /// A [`SelectColumn`]: a select over the enum's options, saving the record as it
129 /// changes.
130 select => SelectColumn<M, T>;
131 }
132}
133
134impl<M, T> FieldColumn<M, T>
135where
136 M: Model,
137 T: Field<Inner = String>,
138{
139 kinds! {
140 /// A [`ColorColumn`]: a swatch of the CSS color the text holds.
141 color => ColorColumn<M, T>;
142 /// An [`ImageColumn`]: the image the text names.
143 image => ImageColumn<M, T>;
144 }
145
146 /// A [`RichTextColumn`](crate::RichTextColumn): the cleaned HTML the text holds.
147 #[cfg(feature = "rich-text")]
148 pub fn rich_text(self) -> crate::RichTextColumn<M, T> {
149 let column = crate::RichTextColumn::new(self.lens);
150 match self.label {
151 Some(label) => column.label(label),
152 None => column,
153 }
154 }
155}
156
157impl<M> FieldColumn<M, bool>
158where
159 M: Model,
160{
161 kinds! {
162 /// A [`BooleanColumn`]: a yes or no.
163 boolean => BooleanColumn<M>;
164 /// A [`ToggleColumn`]: a switch saving the record as it flips.
165 toggle => ToggleColumn<M>;
166 }
167}
168
169impl<M> FieldColumn<M, String>
170where
171 M: Model,
172{
173 kinds! {
174 /// A [`FileColumn`]: a link to the upload the text names.
175 file => FileColumn<M>;
176 }
177}
178
179impl<M, T> FieldColumn<M, T>
180where
181 M: Model,
182 T: EmbeddedForm,
183{
184 kinds! {
185 /// An [`EmbeddedColumn`]: the embedded value's leaves.
186 embedded => EmbeddedColumn<M, T>;
187 }
188}
189
190impl<M, T> FieldColumn<M, Vec<T>, List<T>>
191where
192 M: Model,
193 T: FormScalar,
194{
195 kinds! {
196 /// A [`ListColumn`]: a badge per value.
197 list => ListColumn<M, T>;
198 }
199}
200
201impl<M, T> FieldColumn<M, Vec<T>, List<T>>
202where
203 M: Model,
204 T: FormInput,
205{
206 kinds! {
207 /// A [`RepeaterColumn`]: the list's items.
208 repeater => RepeaterColumn<M, T>;
209 }
210}
211
212/// The table column the derive picks, or none for a field only a detail page shows.
213impl<M, T, P> IntoColumns<M> for FieldColumn<M, T, P> {
214 fn into_columns(self) -> Vec<BoxColumn<M>> {
215 (self.table)()
216 }
217}
218
219/// The detail page column the derive picks.
220impl<M, T, P> IntoDetail<M> for FieldColumn<M, T, P> {
221 fn into_detail(self) -> Detail<M> {
222 (self.detail)()
223 }
224}
225
226impl<M, T, P> From<FieldColumn<M, T, P>> for Lens<M, T, P> {
227 fn from(column: FieldColumn<M, T, P>) -> Self {
228 column.lens
229 }
230}