1use std::collections::HashMap;
2use dxc_types::namespace::Block;
3
4static DEFAULT_NAMESPACE: &str = "dxc";
5static STATE_PREFIX: &str = "is-";
6
7fn bem(namespace: String, block: Block, block_suffix: String, element: String, modifier: String) -> String {
8 let mut cls = format! {"{}-{}", namespace ,block };
9
10 if block_suffix != String::new() {
11 cls = format! {"{}-{}", cls, block_suffix };
12 }
13 if element != String::new() {
14 cls = format! {"{}__{}", cls, element };
15 }
16 if modifier != String::new() {
17 cls = format! {"{}--{}", cls, modifier };
18 }
19 return cls;
20}
21
22pub fn use_get_derived_namespace(namespace_overrides: Option<String>) -> String {
25 match namespace_overrides {
26 Some(namespace) => namespace,
27 _ => String::from(DEFAULT_NAMESPACE),
28 }
29}
30
31#[derive(Clone, Debug)]
32pub struct UseNamespace {
33 block: Block,
34 namespace_overrides: Option<String>,
35}
36
37impl UseNamespace {
38 pub fn new(block: Block, namespace_overrides: Option<String>) -> Self {
39 UseNamespace {
40 block,
41 namespace_overrides,
42 }
43 }
44
45 pub fn namespace(&self) -> String {
46 use_get_derived_namespace(self.namespace_overrides.clone())
47 }
48
49 pub fn b(&self) -> String {
50 bem(self.namespace(), self.block, String::new(), String::new(), String::new())
51 }
52
53 pub fn b_(&self, block_suffix: String) -> String {
54 bem(self.namespace(), self.block, block_suffix, String::new(), String::new())
55 }
56
57 pub fn e_(&self, element: String) -> String {
58 bem(self.namespace(), self.block, String::new(), element, String::new())
59 }
60
61 pub fn m_(&self, modifier: String) -> String {
62 if modifier == String::new() {
63 String::new()
64 } else {
65 bem(self.namespace(), self.block, String::new(), String::new(), modifier)
66 }
67 }
68
69 pub fn be_(&self, block_suffix: String, element: String) -> String {
70 bem(self.namespace(), self.block, block_suffix, element, String::new())
71 }
72
73 pub fn em_(&self, element: String, modifier: String) -> String {
74 bem(self.namespace(), self.block, String::new(), element, modifier)
75 }
76
77 pub fn bm_(&self, block_suffix: String, modifier: String) -> String {
78 bem(self.namespace(), self.block, block_suffix, String::new(), modifier)
79 }
80
81 pub fn bem_(&self, block_suffix: String, element: String, modifier: String) -> String {
82 bem(
83 self.namespace(),
84 self.block,
85 block_suffix,
86 element,
87 modifier,
88 )
89 }
90
91 pub fn is_(&self, name: String, state: Option<bool>) -> String {
92 match state {
93 Some(true) => format!("{}{}", STATE_PREFIX, name),
94 Some(false) => String::new(),
95 None => format!("{}{}", STATE_PREFIX, name),
96 }
97 }
98
99 pub fn css_var(&self, object: &HashMap<String, String>) -> HashMap<String, String> {
100 object
101 .iter()
102 .map(|(key, value)| {
103 let css_var_name = format!("--{}-{}", self.namespace(), key);
104 (css_var_name, value.to_string())
105 })
106 .collect()
107 }
108
109 pub fn css_var_block(&self, object: HashMap<String, String>) -> String {
110 object
111 .iter()
112 .map(|(key, value)| {
113 format!("--{}-{}-{}: {};", self.namespace(), self.block, key, value)
114 })
115 .collect::<Vec<String>>()
116 .join(" ")
117 }
118
119 pub fn css_var_name(&self, name: String) -> String {
120 format!("--{}-{}", self.namespace(), name)
121 }
122
123 pub fn css_var_block_name(&self, name: String) -> String {
124 format!("--{}-{}-{}", self.namespace(), self.block, name)
125 }
126}