1use dashmap::DashMap;
2use std::sync::LazyLock;
3
4static STYLE_REGISTRY: LazyLock<DashMap<String, String>> = LazyLock::new(|| DashMap::new());
6
7static GLOBAL_STYLE_REGISTRY: LazyLock<DashMap<usize, String>> = LazyLock::new(|| DashMap::new());
9
10pub struct StyleRegistry;
12
13impl StyleRegistry {
14 pub fn get() -> &'static DashMap<String, String> {
16 &STYLE_REGISTRY
17 }
18
19 pub fn register(class_name: String, css: String) {
21 STYLE_REGISTRY.insert(class_name, css);
22 }
23
24 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
34pub fn register_style(class_name: &str, css: &str) {
36 StyleRegistry::register(class_name.to_string(), css.to_string());
37}
38
39pub fn register_global_style(css: &str) {
41 let index = GLOBAL_STYLE_REGISTRY.len();
42 GLOBAL_STYLE_REGISTRY.insert(index, css.to_string());
43}
44
45pub 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(); styles.join("\n")
53}
54
55#[derive(Clone, Debug)]
57pub struct Style {
58 pub class_name: String,
59 pub css: String,
60}
61
62impl Style {
63 pub fn new(class_name: String, css: String) -> Self {
65 Self { class_name, css }
66 }
67
68 pub fn class(&self) -> &str {
70 &self.class_name
71 }
72}