Skip to main content

tablo_core/column/
into_columns.rs

1//! [`IntoColumns`]: what a table's column list is built from.
2
3use std::sync::Arc;
4
5#[cfg(feature = "rich-text")]
6use super::RichTextColumn;
7use super::{
8    BooleanColumn, BoxColumn, ColorColumn, Column, ComputedColumn, CountColumn, DateColumn,
9    EmbeddedColumn, FileColumn, IconColumn, ImageColumn, ListColumn, RelationColumn,
10    RepeaterColumn, SelectColumn, TextColumn, TextInputColumn, ToggleColumn,
11};
12
13/// Convert a single built-in column, a model's [`FieldColumn`](crate::FieldColumn), a tuple of
14/// any of them, a `Vec` or slice of boxed columns, or `()` for none yet, into a table's column
15/// list.
16///
17/// [`Table::column`](crate::Table::column) appends any other [`Column`] past it. An app's column
18/// type joins a tuple as the built-in ones do by implementing this trait:
19///
20/// ```rust
21/// # #[derive(Debug, Clone, toasty::Model, tablo_core::PanelModel)]
22/// # struct User { #[key] #[auto] id: uuid::Uuid, name: String }
23/// # struct Initials;
24/// # use topcoat::context::Cx;
25/// # impl tablo_core::extend::Column<User> for Initials {
26/// #     fn name(&self) -> &str { "initials" }
27/// #     fn label(&self) -> tablo_core::Text { "Initials".into() }
28/// #     fn text(&self, _: &Cx, u: &User) -> String { u.name.clone() }
29/// # }
30/// use std::sync::Arc;
31///
32/// use tablo_core::{IntoColumns, Table};
33///
34/// impl IntoColumns<User> for Initials {
35///     fn into_columns(self) -> Vec<Arc<dyn tablo_core::extend::Column<User>>> {
36///         vec![Arc::new(self)]
37///     }
38/// }
39///
40/// Table::new((User::columns().name, Initials));
41/// ```
42pub trait IntoColumns<M> {
43    /// The columns, in the order the table lists them.
44    fn into_columns(self) -> Vec<BoxColumn<M>>;
45}
46
47/// The [`IntoColumns`] impls of the built-in columns: each is one column.
48macro_rules! column_conversions {
49    ($($ty:ident<$($g:ident),+>),+ $(,)?) => {
50        $(
51            impl<$($g),+> IntoColumns<M> for $ty<$($g),+>
52            where
53                Self: Column<M> + 'static,
54            {
55                fn into_columns(self) -> Vec<BoxColumn<M>> {
56                    vec![Arc::new(self)]
57                }
58            }
59        )+
60    };
61}
62
63column_conversions!(
64    TextColumn<M, T>,
65    DateColumn<M, T>,
66    ComputedColumn<M>,
67    RelationColumn<M>,
68    CountColumn<M>,
69    BooleanColumn<M>,
70    FileColumn<M>,
71    ColorColumn<M, T>,
72    IconColumn<M, T>,
73    ImageColumn<M, T>,
74    EmbeddedColumn<M, T>,
75    ListColumn<M, T>,
76    RepeaterColumn<M, T>,
77    ToggleColumn<M>,
78    SelectColumn<M, T>,
79    TextInputColumn<M, T>,
80);
81#[cfg(feature = "rich-text")]
82column_conversions!(RichTextColumn<M, T>);
83
84impl<M> IntoColumns<M> for () {
85    fn into_columns(self) -> Vec<BoxColumn<M>> {
86        Vec::new()
87    }
88}
89
90impl<M> IntoColumns<M> for BoxColumn<M> {
91    fn into_columns(self) -> Vec<BoxColumn<M>> {
92        vec![self]
93    }
94}
95
96impl<M> IntoColumns<M> for Vec<BoxColumn<M>> {
97    fn into_columns(self) -> Vec<BoxColumn<M>> {
98        self
99    }
100}
101
102impl<M> IntoColumns<M> for &[BoxColumn<M>] {
103    fn into_columns(self) -> Vec<BoxColumn<M>> {
104        self.to_vec()
105    }
106}
107
108/// The tuple impls of [`IntoColumns`], one arity per invocation.
109macro_rules! into_columns_tuples {
110    ($($T:ident => $v:ident),+ $(,)?) => {
111        impl<M, $($T),+> IntoColumns<M> for ($($T,)+)
112        where
113            $($T: IntoColumns<M>,)+
114        {
115            fn into_columns(self) -> Vec<BoxColumn<M>> {
116                let ($($v,)+) = self;
117                let mut columns = Vec::new();
118                $( columns.extend($v.into_columns()); )+
119                columns
120            }
121        }
122    };
123}
124
125into_columns_tuples!(A => a);
126into_columns_tuples!(A => a, B => b);
127into_columns_tuples!(A => a, B => b, C => c);
128into_columns_tuples!(A => a, B => b, C => c, D => d);
129into_columns_tuples!(A => a, B => b, C => c, D => d, E => e);
130into_columns_tuples!(A => a, B => b, C => c, D => d, E => e, F => f);
131into_columns_tuples!(A => a, B => b, C => c, D => d, E => e, F => f, G => g);
132into_columns_tuples!(
133    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h
134);
135into_columns_tuples!(
136    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i
137);
138into_columns_tuples!(
139    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j
140);
141into_columns_tuples!(
142    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j, K => k
143);
144into_columns_tuples!(
145    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j, K => k, L => l
146);