1use std::collections::{
2 BTreeMap,
3 HashMap,
4};
5
6use serde::{
7 Deserialize,
8 Serialize,
9};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub enum ConfigFieldType {
13 Text,
14 Password,
15 TextArea,
16 Boolean,
17 Select,
18 MultiSelect,
19 Number,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(rename_all = "snake_case")]
24pub enum ConfigSection {
25 Authentication,
26 Connection,
27 Settings,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct ConfigField {
32 pub key: String,
33 pub label: String,
34 pub description: Option<String>,
35 pub field_type: ConfigFieldType,
36 pub required: bool,
37 pub default_value: Option<serde_json::Value>,
38 pub options: Option<Vec<String>>,
39 pub validation_regex: Option<String>,
40 pub validation_message: Option<String>,
41 pub section: ConfigSection,
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
45#[serde(rename_all = "snake_case")]
46pub enum AuthMode {
47 None,
48 Token,
49 SignIn,
50}
51
52pub const AUTH_MODE_KEY: &str = "auth_mode";
53
54pub fn is_no_auth(config: &HashMap<String, String>) -> bool {
55 config
56 .get(AUTH_MODE_KEY)
57 .is_some_and(|mode| mode.trim() == "none")
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct ConfigSchema {
62 pub fields: Vec<ConfigField>,
63 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
64 pub auth_modes: BTreeMap<String, Vec<AuthMode>>,
65 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
66 pub no_auth: bool,
67}
68
69impl ConfigSchema {
70 pub fn new() -> Self {
71 Self {
72 fields: Vec::new(),
73 auth_modes: BTreeMap::new(),
74 no_auth: false,
75 }
76 }
77
78 pub fn add_field(mut self, field: ConfigField) -> Self {
79 self.fields.push(field);
80 self
81 }
82
83 pub fn add_auth_field(mut self, modes: &[AuthMode], field: ConfigField) -> Self {
84 self.auth_modes.insert(field.key.clone(), modes.to_vec());
85 self.fields.push(field);
86 self
87 }
88
89 pub fn allow_no_auth(mut self) -> Self {
90 self.no_auth = true;
91 self
92 }
93}
94
95impl Default for ConfigSchema {
96 fn default() -> Self {
97 Self::new()
98 }
99}
100
101#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
102pub enum ColumnDataType {
103 String,
104 Number,
105 DateTime,
106 Duration,
107 Status,
108 Badge,
109 Url,
110 Json,
111 Boolean,
112 Custom(String),
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
116pub enum CellRenderer {
117 Text,
118 Badge,
119 DateTime,
120 Duration,
121 StatusBadge,
122 Commit,
123 Avatar,
124 TruncatedText,
125 Link,
126 JsonViewer,
127 Custom(String),
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
131pub enum ColumnVisibility {
132 Always,
133 WhenPresent,
134 WhenCapability(String),
135 Conditional {
136 field: String,
137 equals: serde_json::Value,
138 },
139}
140
141#[derive(Debug, Clone, Serialize, Deserialize)]
142pub struct ColumnDefinition {
143 pub id: String,
144 pub label: String,
145 pub description: Option<String>,
146 pub field_path: String,
147 pub data_type: ColumnDataType,
148 pub renderer: CellRenderer,
149 pub visibility: ColumnVisibility,
150 #[serde(default = "default_visible_true")]
151 pub default_visible: bool,
152 pub width: Option<u32>,
153 pub sort: ColumnSort,
154 pub filterable: bool,
155}
156
157fn default_visible_true() -> bool {
158 true
159}
160
161#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
162#[serde(rename_all = "snake_case")]
163pub enum ColumnSort {
164 #[default]
165 None,
166 Value,
167 HistoryOrder,
168}
169
170#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct TableDefinition {
172 pub id: String,
173 pub name: String,
174 pub description: Option<String>,
175 pub columns: Vec<ColumnDefinition>,
176}
177
178#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct TableSchema {
180 pub tables: Vec<TableDefinition>,
181}
182
183impl TableSchema {
184 pub fn new() -> Self {
185 Self { tables: Vec::new() }
186 }
187
188 pub fn add_table(mut self, table: TableDefinition) -> Self {
189 self.tables.push(table);
190 self
191 }
192
193 pub fn get_table(&self, table_id: &str) -> Option<&TableDefinition> {
194 self.tables.iter().find(|t| t.id == table_id)
195 }
196}
197
198impl Default for TableSchema {
199 fn default() -> Self {
200 Self::new()
201 }
202}
203
204#[cfg(test)]
205mod tests;