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 #[allow(clippy::result_large_err)]
52 pub fn load_from_json(path: &Path) -> Result<Self, CheckError> {
53 let content = fs::read_to_string(path).map_err(CheckError::Io)?;
54 let widgets: HashMap<String, CustomWidgetConfig> =
55 serde_json::from_str(&content).map_err(|e| {
56 CheckError::CustomWidgetConfigLoadError {
57 path: path.to_path_buf(),
58 source: e,
59 }
60 })?;
61
62 Ok(Self { widgets })
63 }
64
65 pub fn has_widget(&self, widget_name: &str) -> bool {
67 self.widgets.contains_key(widget_name)
68 }
69
70 pub fn is_attribute_allowed(&self, widget_name: &str, attribute: &str) -> bool {
82 self.widgets
83 .get(widget_name)
84 .map(|config| config.allowed_attributes.contains(attribute))
85 .unwrap_or(false)
86 }
87
88 pub fn get_allowed_attributes(&self, widget_name: &str) -> Vec<&str> {
98 self.widgets
99 .get(widget_name)
100 .map(|config| {
101 config
102 .allowed_attributes
103 .iter()
104 .map(|s| s.as_str())
105 .collect()
106 })
107 .unwrap_or_default()
108 }
109
110 pub fn add_widget(&mut self, name: String, config: CustomWidgetConfig) {
112 self.widgets.insert(name, config);
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn test_empty_registry() {
122 let registry = CustomWidgetRegistry::new();
123 assert!(!registry.has_widget("CustomWidget"));
124 assert!(!registry.is_attribute_allowed("CustomWidget", "value"));
125 }
126
127 #[test]
128 fn test_add_widget() {
129 let mut registry = CustomWidgetRegistry::new();
130 let mut config = CustomWidgetConfig::default();
131 config.allowed_attributes.insert("value".to_string());
132 config.allowed_attributes.insert("mode".to_string());
133
134 registry.add_widget("CustomWidget".to_string(), config);
135
136 assert!(registry.has_widget("CustomWidget"));
137 assert!(registry.is_attribute_allowed("CustomWidget", "value"));
138 assert!(registry.is_attribute_allowed("CustomWidget", "mode"));
139 assert!(!registry.is_attribute_allowed("CustomWidget", "unknown"));
140 }
141
142 #[test]
143 fn test_get_allowed_attributes() {
144 let mut registry = CustomWidgetRegistry::new();
145 let mut config = CustomWidgetConfig::default();
146 config.allowed_attributes.insert("value".to_string());
147 config.allowed_attributes.insert("mode".to_string());
148
149 registry.add_widget("CustomWidget".to_string(), config);
150
151 let attrs = registry.get_allowed_attributes("CustomWidget");
152 assert_eq!(attrs.len(), 2);
153 assert!(attrs.contains(&"value"));
154 assert!(attrs.contains(&"mode"));
155 }
156
157 #[test]
158 fn test_get_allowed_attributes_unknown_widget() {
159 let registry = CustomWidgetRegistry::new();
160 let attrs = registry.get_allowed_attributes("UnknownWidget");
161 assert!(attrs.is_empty());
162 }
163}