rustyle_css/
style.rs

1use dashmap::DashMap;
2use std::sync::LazyLock;
3
4/// Global style registry for tracking all registered styles (thread-safe)
5static STYLE_REGISTRY: LazyLock<DashMap<String, String>> = LazyLock::new(|| DashMap::new());
6
7/// Global styles registry (for @keyframes, @layer, etc.)
8static GLOBAL_STYLE_REGISTRY: LazyLock<DashMap<usize, String>> = LazyLock::new(|| DashMap::new());
9
10/// Style registry wrapper for backward compatibility
11pub struct StyleRegistry;
12
13impl StyleRegistry {
14    /// Get or initialize the global style registry
15    pub fn get() -> &'static DashMap<String, String> {
16        &STYLE_REGISTRY
17    }
18
19    /// Register a style with a class name
20    pub fn register(class_name: String, css: String) {
21        STYLE_REGISTRY.insert(class_name, css);
22    }
23
24    /// Get all registered styles as CSS
25    pub fn get_all_styles() -> String {
26        STYLE_REGISTRY
27            .iter()
28            .map(|entry| entry.value().clone())
29            .collect::<Vec<_>>()
30            .join("\n")
31    }
32}
33
34/// Register a scoped style (called by macros at compile time)
35pub fn register_style(class_name: &str, css: &str) {
36    StyleRegistry::register(class_name.to_string(), css.to_string());
37}
38
39/// Register a global style (for @keyframes, @layer, etc.)
40pub fn register_global_style(css: &str) {
41    let index = GLOBAL_STYLE_REGISTRY.len();
42    GLOBAL_STYLE_REGISTRY.insert(index, css.to_string());
43}
44
45/// Get all global styles
46pub fn get_all_global_styles() -> String {
47    let mut styles: Vec<String> = GLOBAL_STYLE_REGISTRY
48        .iter()
49        .map(|entry| entry.value().clone())
50        .collect();
51    styles.sort(); // Sort for consistent output
52    styles.join("\n")
53}
54
55/// Represents a scoped style with its class name and CSS
56#[derive(Clone, Debug)]
57pub struct Style {
58    pub class_name: String,
59    pub css: String,
60}
61
62impl Style {
63    /// Create a new style
64    pub fn new(class_name: String, css: String) -> Self {
65        Self { class_name, css }
66    }
67
68    /// Get the class name for use in HTML
69    pub fn class(&self) -> &str {
70        &self.class_name
71    }
72}