openkit 0.1.3

A cross-platform CSS-styled UI framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! CSS loading and management.
//!
//! Provides functionality to load custom CSS from files and strings,
//! allowing users to override the framework's default styles.
//!
//! # Example
//!
//! ```rust,ignore
//! use openkit::prelude::*;
//! use openkit::css::StyleManager;
//!
//! let mut styles = StyleManager::new();
//!
//! // Load from file
//! styles.load_file("./styles/custom.css")?;
//!
//! // Load from string
//! styles.load_css(r#"
//!     .my-button {
//!         background-color: #3b82f6;
//!         border-radius: 8px;
//!     }
//!     .my-button:hover {
//!         background-color: #2563eb;
//!     }
//! "#)?;
//!
//! // Use in app
//! App::new()
//!     .styles(styles)
//!     .run(|| { /* ... */ });
//! ```

use crate::css::{CssParser, StyleSheet, StyleRule};
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// Manages CSS stylesheets for the application.
///
/// StyleManager handles loading, parsing, and cascading of CSS from multiple sources:
/// - Framework default styles
/// - Theme styles (light/dark)
/// - User custom styles
/// - Inline styles
#[derive(Debug, Clone, Default)]
pub struct StyleManager {
    /// Default framework styles (lowest priority)
    default_styles: StyleSheet,
    /// Theme-specific styles
    theme_styles: StyleSheet,
    /// User-provided custom styles (highest priority before inline)
    custom_styles: Vec<StyleSheet>,
    /// Named style modules
    modules: HashMap<String, StyleSheet>,
    /// CSS custom properties (variables)
    variables: HashMap<String, String>,
    /// Whether to watch files for changes (hot reload)
    watch_files: bool,
    /// Loaded file paths for hot reload
    loaded_files: Vec<String>,
}

impl StyleManager {
    /// Create a new StyleManager with default framework styles.
    pub fn new() -> Self {
        let mut manager = Self::default();
        manager.load_default_styles();
        manager
    }

    /// Create an empty StyleManager without default styles.
    pub fn empty() -> Self {
        Self::default()
    }

    /// Load the framework's default styles.
    fn load_default_styles(&mut self) {
        let default_css = include_str!("default.css");
        match CssParser::parse_stylesheet(default_css) {
            Ok(sheet) => {
                self.default_styles = sheet;
            }
            Err(e) => {
                log::warn!("Failed to parse default CSS: {:?}", e);
            }
        }
    }

    /// Load CSS from a file path.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the CSS file
    ///
    /// # Returns
    ///
    /// Result indicating success or an error message
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// styles.load_file("./assets/custom.css")?;
    /// styles.load_file("./theme/dark.css")?;
    /// ```
    pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> Result<(), CssLoadError> {
        let path = path.as_ref();
        let css = fs::read_to_string(path)
            .map_err(|e| CssLoadError::FileRead {
                path: path.display().to_string(),
                error: e.to_string(),
            })?;

        let sheet = CssParser::parse_stylesheet(&css)
            .map_err(|e| CssLoadError::Parse {
                source: path.display().to_string(),
                error: format!("{:?}", e),
            })?;

        self.custom_styles.push(sheet);
        self.loaded_files.push(path.display().to_string());

        Ok(())
    }

    /// Load CSS from a string.
    ///
    /// # Arguments
    ///
    /// * `css` - CSS content as a string
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// styles.load_css(r#"
    ///     .primary-button {
    ///         background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    ///         color: white;
    ///         padding: 12px 24px;
    ///         border-radius: 8px;
    ///     }
    /// "#)?;
    /// ```
    pub fn load_css(&mut self, css: &str) -> Result<(), CssLoadError> {
        let sheet = CssParser::parse_stylesheet(css)
            .map_err(|e| CssLoadError::Parse {
                source: "<inline>".to_string(),
                error: format!("{:?}", e),
            })?;

        self.custom_styles.push(sheet);
        Ok(())
    }

    /// Load CSS as a named module that can be enabled/disabled.
    ///
    /// # Arguments
    ///
    /// * `name` - Module name for reference
    /// * `css` - CSS content
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// styles.load_module("animations", r#"
    ///     @keyframes fadeIn {
    ///         from { opacity: 0; }
    ///         to { opacity: 1; }
    ///     }
    /// "#)?;
    /// ```
    pub fn load_module(&mut self, name: &str, css: &str) -> Result<(), CssLoadError> {
        let sheet = CssParser::parse_stylesheet(css)
            .map_err(|e| CssLoadError::Parse {
                source: format!("module:{}", name),
                error: format!("{:?}", e),
            })?;

        self.modules.insert(name.to_string(), sheet);
        Ok(())
    }

    /// Load a CSS module from a file.
    pub fn load_module_file<P: AsRef<Path>>(&mut self, name: &str, path: P) -> Result<(), CssLoadError> {
        let path = path.as_ref();
        let css = fs::read_to_string(path)
            .map_err(|e| CssLoadError::FileRead {
                path: path.display().to_string(),
                error: e.to_string(),
            })?;

        self.load_module(name, &css)
    }

    /// Remove a loaded module.
    pub fn unload_module(&mut self, name: &str) -> bool {
        self.modules.remove(name).is_some()
    }

    /// Check if a module is loaded.
    pub fn has_module(&self, name: &str) -> bool {
        self.modules.contains_key(name)
    }

    /// Set theme-specific styles.
    pub fn set_theme_styles(&mut self, css: &str) -> Result<(), CssLoadError> {
        let sheet = CssParser::parse_stylesheet(css)
            .map_err(|e| CssLoadError::Parse {
                source: "<theme>".to_string(),
                error: format!("{:?}", e),
            })?;

        self.theme_styles = sheet;
        Ok(())
    }

    /// Set a CSS custom property (variable).
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// styles.set_variable("--primary-color", "#3b82f6");
    /// styles.set_variable("--border-radius", "8px");
    /// ```
    pub fn set_variable(&mut self, name: &str, value: &str) {
        self.variables.insert(name.to_string(), value.to_string());
    }

    /// Get a CSS custom property value.
    pub fn get_variable(&self, name: &str) -> Option<&String> {
        self.variables.get(name)
    }

    /// Set multiple CSS variables at once.
    pub fn set_variables(&mut self, vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>) {
        for (name, value) in vars {
            self.variables.insert(name.into(), value.into());
        }
    }

    /// Get the combined stylesheet with proper cascade order.
    ///
    /// Order (lowest to highest priority):
    /// 1. Default framework styles
    /// 2. Theme styles
    /// 3. Modules (in insertion order)
    /// 4. Custom styles (in load order)
    pub fn combined_stylesheet(&self) -> StyleSheet {
        let mut combined = StyleSheet::default();

        // Add default styles
        combined.merge(self.default_styles.clone());

        // Add theme styles
        combined.merge(self.theme_styles.clone());

        // Add modules
        for sheet in self.modules.values() {
            combined.merge(sheet.clone());
        }

        // Add custom styles (highest priority)
        for sheet in &self.custom_styles {
            combined.merge(sheet.clone());
        }

        combined
    }

    /// Get all rules matching a selector pattern.
    pub fn get_rules(&self, pattern: &str) -> Vec<StyleRule> {
        let combined = self.combined_stylesheet();
        combined.rules
            .into_iter()
            .filter(|rule| {
                // Simple pattern matching - could be enhanced
                rule.selector.parts.iter().any(|part| {
                    match part {
                        crate::css::SelectorPart::Class(name) => name.contains(pattern),
                        crate::css::SelectorPart::Type(name) => name.contains(pattern),
                        crate::css::SelectorPart::Id(name) => name.contains(pattern),
                        _ => false,
                    }
                })
            })
            .collect()
    }

    /// Clear all custom styles (keeps default and theme styles).
    pub fn clear_custom(&mut self) {
        self.custom_styles.clear();
        self.loaded_files.clear();
    }

    /// Clear all styles including defaults.
    pub fn clear_all(&mut self) {
        self.default_styles = StyleSheet::default();
        self.theme_styles = StyleSheet::default();
        self.custom_styles.clear();
        self.modules.clear();
        self.variables.clear();
        self.loaded_files.clear();
    }

    /// Reload all loaded files (useful for hot reload).
    pub fn reload_files(&mut self) -> Result<(), CssLoadError> {
        let files = self.loaded_files.clone();
        self.custom_styles.clear();
        self.loaded_files.clear();

        for file in files {
            self.load_file(&file)?;
        }

        Ok(())
    }

    /// Enable file watching for hot reload.
    pub fn enable_watch(&mut self) {
        self.watch_files = true;
    }

    /// Disable file watching.
    pub fn disable_watch(&mut self) {
        self.watch_files = false;
    }

    /// Get the number of loaded stylesheets.
    pub fn stylesheet_count(&self) -> usize {
        1 + // default
        (if self.theme_styles.rules.is_empty() { 0 } else { 1 }) +
        self.modules.len() +
        self.custom_styles.len()
    }

    /// Get the total number of CSS rules.
    pub fn rule_count(&self) -> usize {
        self.default_styles.rules.len() +
        self.theme_styles.rules.len() +
        self.modules.values().map(|s| s.rules.len()).sum::<usize>() +
        self.custom_styles.iter().map(|s| s.rules.len()).sum::<usize>()
    }
}

/// Errors that can occur when loading CSS.
#[derive(Debug, Clone)]
pub enum CssLoadError {
    /// Failed to read a CSS file
    FileRead {
        path: String,
        error: String,
    },
    /// Failed to parse CSS
    Parse {
        source: String,
        error: String,
    },
    /// Invalid CSS value
    InvalidValue {
        property: String,
        value: String,
    },
}

impl std::fmt::Display for CssLoadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CssLoadError::FileRead { path, error } => {
                write!(f, "Failed to read CSS file '{}': {}", path, error)
            }
            CssLoadError::Parse { source, error } => {
                write!(f, "Failed to parse CSS from {}: {}", source, error)
            }
            CssLoadError::InvalidValue { property, value } => {
                write!(f, "Invalid value '{}' for property '{}'", value, property)
            }
        }
    }
}

impl std::error::Error for CssLoadError {}

/// Builder for creating style configurations.
pub struct StyleBuilder {
    manager: StyleManager,
}

impl StyleBuilder {
    pub fn new() -> Self {
        Self {
            manager: StyleManager::new(),
        }
    }

    /// Start with no default styles.
    pub fn empty() -> Self {
        Self {
            manager: StyleManager::empty(),
        }
    }

    /// Load CSS from a file.
    pub fn file<P: AsRef<Path>>(mut self, path: P) -> Result<Self, CssLoadError> {
        self.manager.load_file(path)?;
        Ok(self)
    }

    /// Load CSS from a string.
    pub fn css(mut self, css: &str) -> Result<Self, CssLoadError> {
        self.manager.load_css(css)?;
        Ok(self)
    }

    /// Load a named module.
    pub fn module(mut self, name: &str, css: &str) -> Result<Self, CssLoadError> {
        self.manager.load_module(name, css)?;
        Ok(self)
    }

    /// Set a CSS variable.
    pub fn var(mut self, name: &str, value: &str) -> Self {
        self.manager.set_variable(name, value);
        self
    }

    /// Set multiple variables.
    pub fn vars(mut self, vars: impl IntoIterator<Item = (&'static str, &'static str)>) -> Self {
        for (name, value) in vars {
            self.manager.set_variable(name, value);
        }
        self
    }

    /// Build the StyleManager.
    pub fn build(self) -> StyleManager {
        self.manager
    }
}

impl Default for StyleBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_load_css_string() {
        let mut manager = StyleManager::new();
        let result = manager.load_css(r#"
            .test-class {
                color: red;
                padding: 10px;
            }
        "#);
        assert!(result.is_ok());
        assert!(manager.rule_count() > 0);
    }

    #[test]
    fn test_css_variables() {
        let mut manager = StyleManager::new();
        manager.set_variable("--primary", "#3b82f6");
        manager.set_variable("--radius", "8px");

        assert_eq!(manager.get_variable("--primary"), Some(&"#3b82f6".to_string()));
        assert_eq!(manager.get_variable("--radius"), Some(&"8px".to_string()));
        assert_eq!(manager.get_variable("--unknown"), None);
    }

    #[test]
    fn test_modules() {
        let mut manager = StyleManager::new();

        manager.load_module("buttons", ".btn { padding: 8px; }").unwrap();
        assert!(manager.has_module("buttons"));

        manager.unload_module("buttons");
        assert!(!manager.has_module("buttons"));
    }

    #[test]
    fn test_style_builder() {
        let manager = StyleBuilder::new()
            .css(".custom { color: blue; }").unwrap()
            .var("--accent", "#f00")
            .build();

        assert!(manager.rule_count() > 0);
        assert_eq!(manager.get_variable("--accent"), Some(&"#f00".to_string()));
    }

    #[test]
    fn test_clear_custom() {
        let mut manager = StyleManager::new();
        let initial_count = manager.rule_count();

        manager.load_css(".extra { color: green; }").unwrap();
        assert!(manager.rule_count() > initial_count);

        manager.clear_custom();
        assert_eq!(manager.rule_count(), initial_count);
    }
}