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