vantage_table/table/impls/
columns.rs1use indexmap::IndexMap;
2use vantage_types::Entity;
3
4use crate::{
5 column::core::ColumnType, prelude::ColumnLike, table::Table, traits::table_source::TableSource,
6};
7
8impl<T: TableSource, E: Entity<T::Value>> Table<T, E> {
9 pub fn add_column<NewColumnType>(&mut self, column: T::Column<NewColumnType>)
11 where
12 NewColumnType: ColumnType,
13 {
14 let name = column.name().to_string();
15
16 if self.columns.contains_key(&name) {
17 panic!("Duplicate column: {}", name);
18 }
19
20 let any_column = self.data_source.to_any_column(column);
22 self.columns.insert(name, any_column);
23 }
24
25 pub fn with_column<NewColumnType>(mut self, column: T::Column<NewColumnType>) -> Self
27 where
28 NewColumnType: ColumnType,
29 {
30 self.add_column(column);
31 self
32 }
33
34 pub fn add_column_of<NewColumnType>(&mut self, name: impl Into<String>)
36 where
37 NewColumnType: ColumnType,
38 {
39 let column = self
40 .data_source
41 .create_column::<NewColumnType>(&name.into());
42 self.add_column(column);
43 }
44
45 pub fn with_id_column(mut self, name: impl Into<String>) -> Self
47 where
48 T::Id: ColumnType,
49 {
50 let name = name.into();
51 self.id_field = Some(name.clone());
52 let column = self.data_source.create_column::<T::Id>(&name);
53 self.add_column(column);
54 self
55 }
56
57 pub fn with_column_of<NewColumnType>(self, name: impl Into<String>) -> Self
59 where
60 NewColumnType: ColumnType,
61 {
62 let column = self
63 .data_source
64 .create_column::<NewColumnType>(&name.into());
65 self.with_column(column)
66 }
67
68 pub fn columns(&self) -> &IndexMap<String, T::Column<T::AnyType>> {
70 &self.columns
71 }
72
73 pub fn get_column<Type>(&self, name: &str) -> Option<T::Column<Type>>
75 where
76 Type: ColumnType,
77 {
78 let any_column = self.columns.get(name)?;
79 self.data_source
80 .convert_any_column::<Type>(any_column.clone())
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87 use crate::mocks::mock_column::MockColumn;
88 use crate::prelude::MockTableSource;
89 use serde_json::Value;
90 use vantage_types::EmptyEntity;
91
92 #[test]
93 fn test_add_column() {
94 let ds = MockTableSource::new();
95 let mut table = Table::<MockTableSource, EmptyEntity>::new("test", ds);
96
97 table.add_column(MockColumn::<String>::new("name"));
98
99 assert!(table.columns().contains_key("name"));
100 assert_eq!(table.columns().len(), 1);
101 }
102
103 #[test]
104 fn test_with_column() {
105 let ds = MockTableSource::new();
106 let table = Table::<MockTableSource, EmptyEntity>::new("test", ds)
107 .with_column(MockColumn::<Value>::new("name"))
108 .with_column(MockColumn::<i32>::new("email"));
109
110 assert!(table.columns().contains_key("name"));
111 assert!(table.columns().contains_key("email"));
112 assert_eq!(table.columns().len(), 2);
113 }
114
115 #[test]
116 #[should_panic(expected = "Duplicate column")]
117 fn test_duplicate_column_panics() {
118 let ds = MockTableSource::new();
119 let mut table = Table::<MockTableSource, EmptyEntity>::new("test", ds);
120
121 table.add_column(MockColumn::<String>::new("name"));
122 table.add_column(MockColumn::<String>::new("name")); }
124
125 #[test]
126 fn test_with_column_of() {
127 let ds = MockTableSource::new();
128 let table = Table::<MockTableSource, EmptyEntity>::new("test", ds)
129 .with_column_of::<String>("name")
130 .with_column_of::<i64>("age")
131 .with_column_of::<bool>("active");
132
133 assert!(table.columns().contains_key("name"));
134 assert!(table.columns().contains_key("age"));
135 assert!(table.columns().contains_key("active"));
136 assert_eq!(table.columns().len(), 3);
137 }
138
139 #[test]
140 fn test_add_column_of() {
141 let ds = MockTableSource::new();
142 let mut table = Table::<MockTableSource, EmptyEntity>::new("test", ds);
143
144 table.add_column_of::<String>("email");
145 table.add_column_of::<i64>("balance");
146
147 assert!(table.columns().contains_key("email"));
148 assert!(table.columns().contains_key("balance"));
149 assert_eq!(table.columns().len(), 2);
150 }
151
152 #[test]
153 fn test_columns_access() {
154 let ds = MockTableSource::new();
155 let table = Table::<MockTableSource, EmptyEntity>::new("test", ds)
156 .with_column_of::<String>("name")
157 .with_column_of::<i64>("age");
158
159 let columns = table.columns();
160 assert!(columns.contains_key("name"));
161 assert!(columns.contains_key("age"));
162 assert_eq!(columns.len(), 2);
163
164 let name_column = table.columns().get("name");
165 assert!(name_column.is_some());
166 assert_eq!(name_column.unwrap().name(), "name");
167
168 let missing_column = table.columns().get("missing");
169 assert!(missing_column.is_none());
170 }
171}