pipedash_plugin_api/
columns.rs1use crate::schema::{
2 CellRenderer,
3 ColumnDataType,
4 ColumnDefinition,
5 ColumnSort,
6 ColumnVisibility,
7 TableDefinition,
8};
9
10impl ColumnDefinition {
11 pub fn new(
12 id: &str, label: &str, field_path: &str, data_type: ColumnDataType, renderer: CellRenderer,
13 ) -> Self {
14 Self {
15 id: id.to_string(),
16 label: label.to_string(),
17 description: None,
18 field_path: field_path.to_string(),
19 data_type,
20 renderer,
21 visibility: ColumnVisibility::Always,
22 default_visible: true,
23 width: None,
24 sort: ColumnSort::None,
25 filterable: false,
26 }
27 }
28
29 pub fn badge(id: &str, label: &str, field_path: &str) -> Self {
30 Self::new(
31 id,
32 label,
33 field_path,
34 ColumnDataType::String,
35 CellRenderer::Badge,
36 )
37 }
38
39 pub fn text(id: &str, label: &str, field_path: &str) -> Self {
40 Self::new(
41 id,
42 label,
43 field_path,
44 ColumnDataType::String,
45 CellRenderer::Text,
46 )
47 }
48
49 pub fn metadata(
50 id: &str, label: &str, description: &str, renderer: CellRenderer, default_visible: bool,
51 ) -> Self {
52 let mut column = Self::new(
53 id,
54 label,
55 &format!("metadata.{id}"),
56 ColumnDataType::String,
57 renderer,
58 )
59 .described(description)
60 .with_width(140)
61 .sortable();
62 column.default_visible = default_visible;
63 column
64 }
65
66 pub fn described(mut self, description: &str) -> Self {
67 self.description = Some(description.to_string());
68 self
69 }
70
71 pub fn with_width(mut self, width: u32) -> Self {
72 self.width = Some(width);
73 self
74 }
75
76 pub fn when_present(mut self) -> Self {
77 self.visibility = ColumnVisibility::WhenPresent;
78 self
79 }
80
81 pub fn hidden(mut self) -> Self {
82 self.default_visible = false;
83 self
84 }
85
86 pub fn sortable(mut self) -> Self {
87 self.sort = ColumnSort::Value;
88 self
89 }
90
91 pub fn history_order(mut self) -> Self {
92 self.sort = ColumnSort::HistoryOrder;
93 self
94 }
95
96 pub fn filterable(mut self) -> Self {
97 self.filterable = true;
98 self
99 }
100}
101
102pub fn trigger_event_column(description: &str) -> ColumnDefinition {
103 ColumnDefinition::badge("event", "Trigger", "metadata.event")
104 .described(description)
105 .with_width(130)
106 .sortable()
107}
108
109impl TableDefinition {
110 pub fn new(id: &str, name: &str, description: &str, columns: Vec<ColumnDefinition>) -> Self {
111 Self {
112 id: id.to_string(),
113 name: name.to_string(),
114 description: Some(description.to_string()),
115 columns,
116 }
117 }
118
119 pub fn column_mut(&mut self, id: &str) -> Option<&mut ColumnDefinition> {
120 self.columns.iter_mut().find(|column| column.id == id)
121 }
122
123 pub fn insert_after(
124 &mut self, anchor_id: &str, columns: impl IntoIterator<Item = ColumnDefinition>,
125 ) {
126 let at = self
127 .columns
128 .iter()
129 .position(|column| column.id == anchor_id)
130 .map_or(self.columns.len(), |index| index + 1);
131 self.columns.splice(at..at, columns);
132 }
133}
134
135#[cfg(test)]
136mod tests;