systemprompt_models/artifacts/table/
hints.rs1use super::column::Column;
2use crate::artifacts::traits::ArtifactSchema;
3use crate::artifacts::types::SortOrder;
4use serde_json::{json, Value as JsonValue};
5
6#[derive(Debug, Clone, Default)]
7pub struct TableHints {
8 pub columns: Vec<Column>,
9 pub sortable_columns: Vec<String>,
10 pub default_sort: Option<(String, SortOrder)>,
11 pub filterable: bool,
12 pub page_size: Option<usize>,
13 pub row_click_enabled: bool,
14}
15
16impl TableHints {
17 pub fn new() -> Self {
18 Self::default()
19 }
20
21 pub fn with_columns(mut self, columns: Vec<Column>) -> Self {
22 self.columns = columns;
23 self
24 }
25
26 pub fn with_sortable(mut self, columns: Vec<String>) -> Self {
27 self.sortable_columns = columns;
28 self
29 }
30
31 pub fn with_default_sort(mut self, column: String, order: SortOrder) -> Self {
32 self.default_sort = Some((column, order));
33 self
34 }
35
36 pub const fn filterable(mut self) -> Self {
37 self.filterable = true;
38 self
39 }
40
41 pub const fn with_page_size(mut self, size: usize) -> Self {
42 self.page_size = Some(size);
43 self
44 }
45
46 pub const fn with_row_click_enabled(mut self, enabled: bool) -> Self {
47 self.row_click_enabled = enabled;
48 self
49 }
50}
51
52impl ArtifactSchema for TableHints {
53 fn generate_schema(&self) -> JsonValue {
54 let mut hints = json!({
55 "columns": self.columns.iter().map(Column::name).collect::<Vec<_>>(),
56 "sortable_columns": self.sortable_columns,
57 "filterable": self.filterable,
58 });
59
60 if let Some((col, order)) = &self.default_sort {
61 hints["default_sort"] = json!({
62 "column": col,
63 "order": order
64 });
65 }
66
67 if let Some(size) = self.page_size {
68 hints["page_size"] = json!(size);
69 }
70
71 if self.row_click_enabled {
72 hints["row_click_enabled"] = json!(true);
73 }
74
75 let column_types: serde_json::Map<String, JsonValue> = self
76 .columns
77 .iter()
78 .map(|c| (c.name().to_string(), json!(c.column_type())))
79 .collect();
80 hints["column_types"] = json!(column_types);
81
82 hints
83 }
84}