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, RelationColumn, RepeaterColumn,
10    SelectColumn, TextColumn, TextInputColumn, ToggleColumn,
11};
12
13/// Convert a single built-in column, a tuple of any [`Column`]s, a `Vec` or slice
14/// of boxed columns, or `()` for none yet, into a table's column list.
15///
16/// [`Table::column`](crate::Table::column) appends past it.
17pub trait IntoColumns<M> {
18    #[doc(hidden)]
19    fn into_columns(self) -> Vec<BoxColumn<M>>;
20}
21
22/// The [`IntoColumns`] impls of the built-in columns: each is one column.
23macro_rules! column_conversions {
24    ($($ty:ident<$($g:ident),+>),+ $(,)?) => {
25        $(
26            impl<$($g),+> IntoColumns<M> for $ty<$($g),+>
27            where
28                Self: Column<M> + 'static,
29            {
30                fn into_columns(self) -> Vec<BoxColumn<M>> {
31                    vec![Arc::new(self)]
32                }
33            }
34        )+
35    };
36}
37
38column_conversions!(
39    TextColumn<M, T>,
40    DateColumn<M, T>,
41    ComputedColumn<M>,
42    RelationColumn<M>,
43    CountColumn<M>,
44    BooleanColumn<M>,
45    FileColumn<M>,
46    ColorColumn<M, T>,
47    IconColumn<M, T>,
48    ImageColumn<M, T>,
49    EmbeddedColumn<M, T>,
50    RepeaterColumn<M, T>,
51    ToggleColumn<M>,
52    SelectColumn<M, T>,
53    TextInputColumn<M, T>,
54);
55#[cfg(feature = "rich-text")]
56column_conversions!(RichTextColumn<M, T>);
57
58impl<M> IntoColumns<M> for () {
59    fn into_columns(self) -> Vec<BoxColumn<M>> {
60        Vec::new()
61    }
62}
63
64impl<M> IntoColumns<M> for Vec<BoxColumn<M>> {
65    fn into_columns(self) -> Vec<BoxColumn<M>> {
66        self
67    }
68}
69
70impl<M> IntoColumns<M> for &[BoxColumn<M>] {
71    fn into_columns(self) -> Vec<BoxColumn<M>> {
72        self.to_vec()
73    }
74}
75
76/// The tuple impls of [`IntoColumns`], one arity per invocation.
77macro_rules! into_columns_tuples {
78    ($($T:ident => $v:ident),+ $(,)?) => {
79        impl<M, $($T),+> IntoColumns<M> for ($($T,)+)
80        where
81            $($T: Column<M> + 'static,)+
82        {
83            fn into_columns(self) -> Vec<BoxColumn<M>> {
84                let ($($v,)+) = self;
85                vec![$(Arc::new($v) as BoxColumn<M>,)+]
86            }
87        }
88    };
89}
90
91into_columns_tuples!(A => a);
92into_columns_tuples!(A => a, B => b);
93into_columns_tuples!(A => a, B => b, C => c);
94into_columns_tuples!(A => a, B => b, C => c, D => d);
95into_columns_tuples!(A => a, B => b, C => c, D => d, E => e);
96into_columns_tuples!(A => a, B => b, C => c, D => d, E => e, F => f);
97into_columns_tuples!(A => a, B => b, C => c, D => d, E => e, F => f, G => g);
98into_columns_tuples!(
99    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h
100);
101into_columns_tuples!(
102    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i
103);
104into_columns_tuples!(
105    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j
106);
107into_columns_tuples!(
108    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j, K => k
109);
110into_columns_tuples!(
111    A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h, I => i, J => j, K => k, L => l
112);