Skip to main content

pipedash_plugin_api/
columns.rs

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