1use std::fmt;
2use strum::{Display, EnumString, IntoStaticStr};
3
4inventory::collect!(GpuiTableShape);
5
6pub use component_shape::{ComponentFieldName, ComponentShapeUse, RustPath, RustType};
7
8#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10pub struct TableId<'a> {
11 value: &'a str,
12}
13
14impl<'a> TableId<'a> {
15 pub const fn new(value: &'a str) -> Self {
24 assert_valid_table_id(value);
25 Self { value }
26 }
27
28 pub const fn as_str(&self) -> &'a str {
30 self.value
31 }
32}
33
34const fn assert_valid_table_id(value: &str) {
35 let bytes = value.as_bytes();
36 assert!(!bytes.is_empty(), "table ids must not be empty");
37
38 let mut index = 0;
39 while index < bytes.len() {
40 let byte = bytes[index];
41 assert!(
42 matches!(byte, b'a'..=b'z' | b'0'..=b'9' | b'_' | b'-'),
43 "table ids must use lowercase ASCII letters, digits, `_`, or `-`"
44 );
45 index += 1;
46 }
47}
48
49impl fmt::Display for TableId<'_> {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 f.write_str(self.value)
52 }
53}
54
55impl From<TableId<'_>> for String {
56 fn from(table_id: TableId<'_>) -> Self {
57 table_id.to_string()
58 }
59}
60
61#[derive(Debug)]
63pub struct GpuiTableShape {
64 pub struct_name: &'static str,
65 pub table_id: TableId<'static>,
66 pub table_title: &'static str,
67 pub fluent: bool,
68 pub columns: &'static [ColumnVariant],
69 pub filters: &'static [FilterVariant],
70 pub load_more: bool,
72 pub source_path: &'static str,
75}
76
77impl GpuiTableShape {
78 #[allow(clippy::too_many_arguments)]
79 pub const fn new(
80 struct_name: &'static str,
81 table_id: &'static str,
82 table_title: &'static str,
83 fluent: bool,
84 columns: &'static [ColumnVariant],
85 filters: &'static [FilterVariant],
86 load_more: bool,
87 source_path: &'static str,
88 ) -> Self {
89 Self {
90 struct_name,
91 table_id: TableId::new(table_id),
92 table_title,
93 fluent,
94 columns,
95 filters,
96 load_more,
97 source_path,
98 }
99 }
100
101 pub const fn table_id(&self) -> TableId<'static> {
102 self.table_id
103 }
104}
105
106#[derive(Debug)]
108pub struct FilterVariant {
109 pub shape_use: ComponentShapeUse,
110 pub filter_type: RegistryFilterType,
111 pub component_path: RustPath,
112}
113
114impl FilterVariant {
115 pub const fn new(
116 shape_use: ComponentShapeUse,
117 filter_type: RegistryFilterType,
118 component_path: RustPath,
119 ) -> Self {
120 Self {
121 shape_use,
122 filter_type,
123 component_path,
124 }
125 }
126}
127
128#[derive(Clone, Copy, Debug, Eq, IntoStaticStr, PartialEq)]
130#[strum(serialize_all = "snake_case", const_into_str)]
131pub enum RegistryFilterType {
132 Faceted,
133 DateRange,
134 NumberRange,
135 Text,
136}
137
138impl RegistryFilterType {
139 pub const fn as_str(self) -> &'static str {
140 self.into_str()
141 }
142}
143
144#[derive(Debug)]
146pub struct ColumnVariant {
147 pub field_name: &'static str,
148 pub field_type: RustType,
149 pub title: &'static str,
150 pub width: f32,
151 pub sortable: bool,
152 pub fixed: ColumnFixed,
153}
154
155impl ColumnVariant {
156 pub const fn new(
157 field_name: &'static str,
158 field_type: RustType,
159 title: &'static str,
160 width: f32,
161 sortable: bool,
162 fixed: ColumnFixed,
163 ) -> Self {
164 Self {
165 field_name,
166 field_type,
167 title,
168 width,
169 sortable,
170 fixed,
171 }
172 }
173}
174
175#[derive(Clone, Copy, Debug, Default, Display, EnumString, Eq, IntoStaticStr, PartialEq)]
177#[strum(serialize_all = "snake_case", const_into_str)]
178pub enum ColumnFixed {
179 #[default]
180 None,
181 Left,
182 Right,
183}
184
185pub use inventory;
186
187#[cfg(test)]
188mod tests {
189 use super::{
190 ColumnFixed, ColumnVariant, ComponentFieldName, ComponentShapeUse, FilterVariant,
191 GpuiTableShape, RegistryFilterType, RustPath, RustType, TableId,
192 };
193
194 #[test]
195 fn table_id_wrapper_exposes_borrowed_identifier() {
196 let table_id = TableId::new("purchase_order");
197
198 assert_eq!(table_id.as_str(), "purchase_order");
199 assert_eq!(table_id.to_string(), "purchase_order");
200 assert_eq!(String::from(table_id), "purchase_order");
201 }
202
203 #[test]
204 #[should_panic(expected = "table ids must not be empty")]
205 fn table_ids_reject_empty_values() {
206 _ = TableId::new("");
207 }
208
209 #[test]
210 #[should_panic(expected = "table ids must use lowercase ASCII")]
211 fn table_ids_reject_unstable_text() {
212 _ = TableId::new("Purchase Order");
213 }
214
215 #[test]
216 fn table_shape_exposes_typed_table_id() {
217 let shape = GpuiTableShape::new(
218 "PurchaseOrder",
219 "purchase_order",
220 "Purchase Orders",
221 false,
222 &[],
223 &[],
224 false,
225 "src/purchase_order.rs",
226 );
227
228 assert_eq!(shape.table_id().as_str(), "purchase_order");
229 }
230
231 #[test]
232 fn filter_variant_records_neutral_shape_use_metadata() {
233 let variant = FilterVariant::new(
234 ComponentShapeUse::new(
235 ComponentFieldName::new("status"),
236 RustPath::from_macro_tokens_unchecked("crate::StatusFilterShape"),
237 ),
238 RegistryFilterType::Faceted,
239 RustPath::from_macro_tokens_unchecked("crate::StatusFilter"),
240 );
241
242 assert_eq!(variant.shape_use.field_name().as_str(), "status");
243 assert_eq!(
244 variant.shape_use.shape_path().as_str(),
245 "crate::StatusFilterShape"
246 );
247 assert_eq!(variant.component_path.as_str(), "crate::StatusFilter");
248 }
249
250 #[test]
251 fn filter_variant_records_field_type_in_neutral_shape_use() {
252 let variant = FilterVariant::new(
253 ComponentShapeUse::new(
254 ComponentFieldName::new("status"),
255 RustPath::from_macro_tokens_unchecked("crate::StatusFilterShape"),
256 )
257 .with_field_type(RustType::from_macro_tokens_unchecked("crate::Status")),
258 RegistryFilterType::Faceted,
259 RustPath::from_macro_tokens_unchecked("crate::StatusFilter"),
260 );
261
262 assert_eq!(
263 variant.shape_use.field_type().map(RustType::as_str),
264 Some("crate::Status")
265 );
266 }
267
268 #[test]
269 fn registry_filter_type_names_are_stable_schema_metadata() {
270 assert_eq!(RegistryFilterType::Faceted.as_str(), "faceted");
271 assert_eq!(RegistryFilterType::DateRange.as_str(), "date_range");
272 assert_eq!(RegistryFilterType::NumberRange.as_str(), "number_range");
273 assert_eq!(RegistryFilterType::Text.as_str(), "text");
274 }
275
276 #[test]
277 fn column_metadata_and_fixed_position_names_are_stable() {
278 let column = ColumnVariant::new(
279 "name",
280 RustType::from_macro_tokens_unchecked("String"),
281 "Name",
282 160.0,
283 true,
284 ColumnFixed::Left,
285 );
286
287 assert_eq!(column.field_name, "name");
288 assert_eq!(column.field_type.as_str(), "String");
289 assert_eq!(column.title, "Name");
290 assert_eq!(column.width, 160.0);
291 assert!(column.sortable);
292 assert_eq!(column.fixed, ColumnFixed::Left);
293
294 assert_eq!(ColumnFixed::default(), ColumnFixed::None);
295 assert_eq!(ColumnFixed::None.to_string(), "none");
296 assert_eq!(ColumnFixed::Left.to_string(), "left");
297 assert_eq!(ColumnFixed::Right.to_string(), "right");
298 assert_eq!("none".parse(), Ok(ColumnFixed::None));
299 assert_eq!("left".parse(), Ok(ColumnFixed::Left));
300 assert_eq!("right".parse(), Ok(ColumnFixed::Right));
301 assert!("center".parse::<ColumnFixed>().is_err());
302 }
303}