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)]
146#[non_exhaustive]
147pub struct ColumnVariant {
148 pub key: &'static str,
150 pub calculated: bool,
152 pub field_name: &'static str,
153 pub field_type: RustType,
154 pub title: &'static str,
155 pub width: f32,
156 pub sortable: bool,
157 pub fixed: ColumnFixed,
158}
159
160impl ColumnVariant {
161 pub const fn with_key(mut self, key: &'static str) -> Self {
162 self.key = key;
163 self
164 }
165 pub const fn with_calculated(mut self, calculated: bool) -> Self {
166 self.calculated = calculated;
167 self
168 }
169
170 pub const fn new(
171 field_name: &'static str,
172 field_type: RustType,
173 title: &'static str,
174 width: f32,
175 sortable: bool,
176 fixed: ColumnFixed,
177 ) -> Self {
178 Self {
179 key: field_name,
180 calculated: false,
181 field_name,
182 field_type,
183 title,
184 width,
185 sortable,
186 fixed,
187 }
188 }
189}
190
191#[derive(Clone, Copy, Debug, Default, Display, EnumString, Eq, IntoStaticStr, PartialEq)]
193#[strum(serialize_all = "snake_case", const_into_str)]
194pub enum ColumnFixed {
195 #[default]
196 None,
197 Left,
198 Right,
199}
200
201pub use inventory;
202
203#[cfg(test)]
204mod tests {
205 use super::{
206 ColumnFixed, ColumnVariant, ComponentFieldName, ComponentShapeUse, FilterVariant,
207 GpuiTableShape, RegistryFilterType, RustPath, RustType, TableId,
208 };
209
210 #[test]
211 fn table_id_wrapper_exposes_borrowed_identifier() {
212 let table_id = TableId::new("purchase_order");
213
214 assert_eq!(table_id.as_str(), "purchase_order");
215 assert_eq!(table_id.to_string(), "purchase_order");
216 assert_eq!(String::from(table_id), "purchase_order");
217 }
218
219 #[test]
220 #[should_panic(expected = "table ids must not be empty")]
221 fn table_ids_reject_empty_values() {
222 _ = TableId::new("");
223 }
224
225 #[test]
226 #[should_panic(expected = "table ids must use lowercase ASCII")]
227 fn table_ids_reject_unstable_text() {
228 _ = TableId::new("Purchase Order");
229 }
230
231 #[test]
232 fn table_shape_exposes_typed_table_id() {
233 let shape = GpuiTableShape::new(
234 "PurchaseOrder",
235 "purchase_order",
236 "Purchase Orders",
237 false,
238 &[],
239 &[],
240 false,
241 "src/purchase_order.rs",
242 );
243
244 assert_eq!(shape.table_id().as_str(), "purchase_order");
245 }
246
247 #[test]
248 fn filter_variant_records_neutral_shape_use_metadata() {
249 let variant = FilterVariant::new(
250 ComponentShapeUse::new(
251 ComponentFieldName::new("status"),
252 RustPath::from_macro_tokens_unchecked("crate::StatusFilterShape"),
253 ),
254 RegistryFilterType::Faceted,
255 RustPath::from_macro_tokens_unchecked("crate::StatusFilter"),
256 );
257
258 assert_eq!(variant.shape_use.field_name().as_str(), "status");
259 assert_eq!(
260 variant.shape_use.shape_path().as_str(),
261 "crate::StatusFilterShape"
262 );
263 assert_eq!(variant.component_path.as_str(), "crate::StatusFilter");
264 }
265
266 #[test]
267 fn filter_variant_records_field_type_in_neutral_shape_use() {
268 let variant = FilterVariant::new(
269 ComponentShapeUse::new(
270 ComponentFieldName::new("status"),
271 RustPath::from_macro_tokens_unchecked("crate::StatusFilterShape"),
272 )
273 .with_field_type(RustType::from_macro_tokens_unchecked("crate::Status")),
274 RegistryFilterType::Faceted,
275 RustPath::from_macro_tokens_unchecked("crate::StatusFilter"),
276 );
277
278 assert_eq!(
279 variant.shape_use.field_type().map(RustType::as_str),
280 Some("crate::Status")
281 );
282 }
283
284 #[test]
285 fn registry_filter_type_names_are_stable_schema_metadata() {
286 assert_eq!(RegistryFilterType::Faceted.as_str(), "faceted");
287 assert_eq!(RegistryFilterType::DateRange.as_str(), "date_range");
288 assert_eq!(RegistryFilterType::NumberRange.as_str(), "number_range");
289 assert_eq!(RegistryFilterType::Text.as_str(), "text");
290 }
291
292 #[test]
293 fn column_metadata_and_fixed_position_names_are_stable() {
294 let column = ColumnVariant::new(
295 "name",
296 RustType::from_macro_tokens_unchecked("String"),
297 "Name",
298 160.0,
299 true,
300 ColumnFixed::Left,
301 );
302
303 assert_eq!(column.field_name, "name");
304 assert_eq!(column.field_type.as_str(), "String");
305 assert_eq!(column.title, "Name");
306 assert_eq!(column.width, 160.0);
307 assert!(column.sortable);
308 assert_eq!(column.fixed, ColumnFixed::Left);
309
310 assert_eq!(ColumnFixed::default(), ColumnFixed::None);
311 assert_eq!(ColumnFixed::None.to_string(), "none");
312 assert_eq!(ColumnFixed::Left.to_string(), "left");
313 assert_eq!(ColumnFixed::Right.to_string(), "right");
314 assert_eq!("none".parse(), Ok(ColumnFixed::None));
315 assert_eq!("left".parse(), Ok(ColumnFixed::Left));
316 assert_eq!("right".parse(), Ok(ColumnFixed::Right));
317 assert!("center".parse::<ColumnFixed>().is_err());
318 }
319}