|
1 | | -pub enum Color { |
2 | | - Bg = "background", |
3 | | - BgColor = "background-color", |
4 | | - Color = "color", |
| 1 | +use once_cell::sync::Lazy; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::hash::{DefaultHasher, Hasher}; |
| 4 | +use std::sync::Mutex; |
| 5 | + |
| 6 | +#[derive(Clone)] |
| 7 | +pub enum PropertyType { |
| 8 | + Single(String), |
| 9 | + Multi(Vec<String>), |
| 10 | +} |
| 11 | + |
| 12 | +impl From<&str> for PropertyType { |
| 13 | + fn from(value: &str) -> Self { |
| 14 | + PropertyType::Single(value.to_string()) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +impl From<String> for PropertyType { |
| 19 | + fn from(value: String) -> Self { |
| 20 | + PropertyType::Single(value) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl From<[&str; 2]> for PropertyType { |
| 25 | + fn from(value: [&str; 2]) -> Self { |
| 26 | + PropertyType::Multi(value.iter().map(|v| v.to_string()).collect()) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +static GLOBAL_STYLE_PROPERTY: Lazy<Mutex<HashMap<&str, PropertyType>>> = Lazy::new(|| { |
| 31 | + Mutex::new({ |
| 32 | + let mut map = HashMap::new(); |
| 33 | + |
| 34 | + for (key, value) in [ |
| 35 | + ("bg", "background"), |
| 36 | + ("bgColor", "background-color"), |
| 37 | + ("color", "color"), |
| 38 | + ("m", "margin"), |
| 39 | + ("mt", "margin-top"), |
| 40 | + ("mr", "margin-right"), |
| 41 | + ("mb", "margin-bottom"), |
| 42 | + ("ml", "margin-left"), |
| 43 | + ("p", "padding"), |
| 44 | + ("pt", "padding-top"), |
| 45 | + ("pr", "padding-right"), |
| 46 | + ("pb", "padding-bottom"), |
| 47 | + ("pl", "padding-left"), |
| 48 | + ("w", "width"), |
| 49 | + ("h", "height"), |
| 50 | + ("minW", "min-width"), |
| 51 | + ("minH", "min-height"), |
| 52 | + ("maxW", "max-width"), |
| 53 | + ("maxH", "max-height"), |
| 54 | + ] { |
| 55 | + map.insert(key, value.into()); |
| 56 | + } |
| 57 | + |
| 58 | + for (key, value) in [ |
| 59 | + ("mx", ["margin-left", "margin-right"]), |
| 60 | + ("my", ["margin-top", "margin-bottom"]), |
| 61 | + ("px", ["padding-left", "padding-right"]), |
| 62 | + ("py", ["padding-top", "padding-bottom"]), |
| 63 | + ] { |
| 64 | + map.insert(key, value.into()); |
| 65 | + } |
| 66 | + map |
| 67 | + }) |
| 68 | +}); |
| 69 | +fn to_kebab_case(value: &str) -> String { |
| 70 | + value |
| 71 | + .chars() |
| 72 | + .enumerate() |
| 73 | + .map(|(i, c)| { |
| 74 | + if c.is_uppercase() { |
| 75 | + if i == 0 { |
| 76 | + c.to_ascii_lowercase().to_string() |
| 77 | + } else { |
| 78 | + format!("-{}", c.to_ascii_lowercase()) |
| 79 | + } |
| 80 | + } else { |
| 81 | + c.to_string() |
| 82 | + } |
| 83 | + }) |
| 84 | + .collect() |
| 85 | +} |
| 86 | + |
| 87 | +pub fn convert_property(property: &str) -> PropertyType { |
| 88 | + GLOBAL_STYLE_PROPERTY |
| 89 | + .lock() |
| 90 | + .unwrap() |
| 91 | + .get(property) |
| 92 | + .cloned() |
| 93 | + .unwrap_or_else(|| to_kebab_case(property).into()) |
| 94 | +} |
| 95 | + |
| 96 | +pub fn sheet_to_classname(property: &str, level: u8, value: Option<&str>) -> String { |
| 97 | + let mut hasher = DefaultHasher::new(); |
| 98 | + hasher.write(format!("{}-{}-{}", property, level, value.unwrap_or("")).as_bytes()); |
| 99 | + format!("d{}", hasher.finish()) |
| 100 | +} |
| 101 | + |
| 102 | +pub fn sheet_to_variable_name(property: &str, level: u8) -> String { |
| 103 | + let mut hasher = DefaultHasher::new(); |
| 104 | + hasher.write(format!("{}-{}", property, level).as_bytes()); |
| 105 | + format!("--d{}", hasher.finish()) |
5 | 106 | } |
0 commit comments