1use fxhash::FxHashMap;
2use smol_str::SmolStr;
3
4use crate::{Lock, Lockable, Shared, StyleAttribute, StyleSelectors, StyleSpecificity};
5
6type RuleAttributes = FxHashMap<SmolStr, (StyleAttribute, StyleSpecificity)>;
7
8#[derive(Clone, Debug, Default)]
9pub struct StyleCache {
10 attributes: Shared<Lock<FxHashMap<StyleSelectors, RuleAttributes>>>,
11}
12
13impl StyleCache {
14 pub fn new() -> Self {
15 Self::default()
16 }
17
18 pub fn clear(&self) {
19 let mut attributes = self.attributes.lock_mut();
20 attributes.clear();
21 }
22
23 pub fn insert(
24 &self,
25 selectors: &StyleSelectors,
26 attribute: StyleAttribute,
27 specificity: StyleSpecificity,
28 ) {
29 let mut attributes = self.attributes.lock_mut();
30 let attributes = attributes.entry(selectors.clone()).or_default();
31 attributes.insert(attribute.key.clone(), (attribute, specificity));
32 }
33
34 pub fn get_attribute(
35 &self,
36 selectors: &StyleSelectors,
37 key: &str,
38 ) -> Option<(StyleAttribute, StyleSpecificity)> {
39 let attributes = self.attributes.lock_mut();
40 let attributes = attributes.get(selectors)?;
41 let attribute = attributes.get(key)?;
42 Some(attribute.clone())
43 }
44}