Skip to main content

rust_css_parser/parser/utils/
config.rs

1//! Port of `src/parser/utils/config.ts`: the shorthand properties configuration (`src/data/properties.json`).
2
3use std::sync::OnceLock;
4
5pub static PROPERTIES_JSON: &str = include_str!("../../data/properties.json");
6
7/// the properties configuration (`getConfig()`)
8pub fn get_config() -> &'static serde_json::Value {
9    static CONFIG: OnceLock<serde_json::Value> = OnceLock::new();
10    CONFIG.get_or_init(|| serde_json::from_str(PROPERTIES_JSON).expect("properties.json"))
11}
12
13/// the groups of the properties configuration (`properties`, `map`, `property`) indexed by name: the lookups of the
14/// minifier hash the name once with the fast hasher instead of the json object lookups
15pub struct ConfigIndex {
16    pub properties: crate::utils::fx::HashMap<&'static str, &'static serde_json::Value>,
17    pub map: crate::utils::fx::HashMap<&'static str, &'static serde_json::Value>,
18    pub property: crate::utils::fx::HashMap<&'static str, &'static serde_json::Value>,
19}
20
21/// `null`: the value of a missing key (`config[group][name]`)
22pub static NULL: serde_json::Value = serde_json::Value::Null;
23
24impl ConfigIndex {
25    /// `config[group][name]`: `null` when the name is not defined
26    pub fn get(map: &crate::utils::fx::HashMap<&'static str, &'static serde_json::Value>, name: &str) -> &'static serde_json::Value {
27        map.get(name).copied().unwrap_or(&NULL)
28    }
29}
30
31/// the index of the properties configuration
32pub fn config_index() -> &'static ConfigIndex {
33    static INDEX: OnceLock<ConfigIndex> = OnceLock::new();
34    INDEX.get_or_init(|| {
35        let config = get_config();
36        let group = |key: &str| {
37            config[key].as_object().map(|o| o.iter().map(|(k, v)| (k.as_str(), v)).collect()).unwrap_or_default()
38        };
39        ConfigIndex { properties: group("properties"), map: group("map"), property: group("property") }
40    })
41}