dampen_cli/commands/check/
custom_widgets.rs1use serde::{Deserialize, Serialize};
2use std::collections::{HashMap, HashSet};
3use std::fs;
4use std::path::Path;
5
6use super::errors::CheckError;
7
8#[derive(Debug, Clone, Serialize, Deserialize, Default)]
10pub struct CustomWidgetConfig {
11 #[serde(default)]
12 pub allowed_attributes: HashSet<String>,
13}
14
15#[derive(Debug, Clone, Default)]
17pub struct CustomWidgetRegistry {
18 widgets: HashMap<String, CustomWidgetConfig>,
19}
20
21impl CustomWidgetRegistry {
22 pub fn new() -> Self {
24 Self {
25 widgets: HashMap::new(),
26 }
27 }
28
29 pub fn load_from_json(path: &Path) -> Result<Self, CheckError> {
52 let content = fs::read_to_string(path).map_err(|e| CheckError::Io(e))?;
53 let widgets: HashMap<String, CustomWidgetConfig> =
54 serde_json::from_str(&content).map_err(|e| {
55 CheckError::CustomWidgetConfigLoadError {
56 path: path.to_path_buf(),
57 source: e,
58 }
59 })?;
60
61 Ok(Self { widgets })
62 }
63
64 pub fn has_widget(&self, widget_name: &str) -> bool {
66 self.widgets.contains_key(widget_name)
67 }
68
69 pub fn is_attribute_allowed(&self, widget_name: &str, attribute: &str) -> bool {
81 self.widgets
82 .get(widget_name)
83 .map(|config| config.allowed_attributes.contains(attribute))
84 .unwrap_or(false)
85 }
86
87 pub fn get_allowed_attributes(&self, widget_name: &str) -> Vec<&str> {
97 self.widgets
98 .get(widget_name)
99 .map(|config| {
100 config
101 .allowed_attributes
102 .iter()
103 .map(|s| s.as_str())
104 .collect()
105 })
106 .unwrap_or_default()
107 }
108
109 pub fn add_widget(&mut self, name: String, config: CustomWidgetConfig) {
111 self.widgets.insert(name, config);
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn test_empty_registry() {
121 let registry = CustomWidgetRegistry::new();
122 assert!(!registry.has_widget("CustomWidget"));
123 assert!(!registry.is_attribute_allowed("CustomWidget", "value"));
124 }
125
126 #[test]
127 fn test_add_widget() {
128 let mut registry = CustomWidgetRegistry::new();
129 let mut config = CustomWidgetConfig::default();
130 config.allowed_attributes.insert("value".to_string());
131 config.allowed_attributes.insert("mode".to_string());
132
133 registry.add_widget("CustomWidget".to_string(), config);
134
135 assert!(registry.has_widget("CustomWidget"));
136 assert!(registry.is_attribute_allowed("CustomWidget", "value"));
137 assert!(registry.is_attribute_allowed("CustomWidget", "mode"));
138 assert!(!registry.is_attribute_allowed("CustomWidget", "unknown"));
139 }
140
141 #[test]
142 fn test_get_allowed_attributes() {
143 let mut registry = CustomWidgetRegistry::new();
144 let mut config = CustomWidgetConfig::default();
145 config.allowed_attributes.insert("value".to_string());
146 config.allowed_attributes.insert("mode".to_string());
147
148 registry.add_widget("CustomWidget".to_string(), config);
149
150 let attrs = registry.get_allowed_attributes("CustomWidget");
151 assert_eq!(attrs.len(), 2);
152 assert!(attrs.contains(&"value"));
153 assert!(attrs.contains(&"mode"));
154 }
155
156 #[test]
157 fn test_get_allowed_attributes_unknown_widget() {
158 let registry = CustomWidgetRegistry::new();
159 let attrs = registry.get_allowed_attributes("UnknownWidget");
160 assert!(attrs.is_empty());
161 }
162}