From 5ad1f8b90d36bbf82a801338cc8a58fa16bb021c Mon Sep 17 00:00:00 2001 From: annieversary Date: Wed, 6 Jul 2022 12:34:49 +0100 Subject: [PATCH] implement special classes --- src/class.rs | 8 +++++++- src/defaults.rs | 35 +++++++++++++++++++++++++++++++++++ src/lib.rs | 24 +++++++++++++++++++++++- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/class.rs b/src/class.rs index 57d6f28..b10ac05 100644 --- a/src/class.rs +++ b/src/class.rs @@ -62,7 +62,13 @@ impl<'a> Class<'a> { if let Some(val) = self.value { let val = z.values.get(val).map(AsRef::as_ref).unwrap_or(val); - Ok(format!("{selector} {{ {name}: {val}; }}",)) + + if let Some(fun) = z.specials.get(name) { + let v = fun(val); + Ok(format!("{selector} {{ {v} }}",)) + } else { + Ok(format!("{selector} {{ {name}: {val}; }}",)) + } } else if let Some(v) = z.rules.get(name) { Ok(format!("{selector} {{ {v} }}",)) } else { diff --git a/src/defaults.rs b/src/defaults.rs index c261cbe..a657808 100644 --- a/src/defaults.rs +++ b/src/defaults.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; +use crate::SpecialRule; + pub(crate) fn default_rules() -> HashMap { vec![ ("flex", "display: flex;"), @@ -70,3 +72,36 @@ pub(crate) fn default_pseudos() -> HashMap { .map(|(a, b)| (a.to_string(), b.to_string())) .collect() } + +pub(crate) fn default_specials() -> HashMap { + vec![ + ("mx", { + fn fun<'a>(val: &'a str) -> String { + format!("margin-left: {val}; margin-right: {val};") + } + Box::new(fun) as SpecialRule + }), + ("my", { + fn fun<'a>(val: &'a str) -> String { + format!("margin-top: {val}; margin-bottom: {val};") + } + Box::new(fun) as SpecialRule + }), + ("px", { + fn fun<'a>(val: &'a str) -> String { + format!("padding-left: {val}; padding-right: {val};") + } + Box::new(fun) as SpecialRule + }), + ("py", { + fn fun<'a>(val: &'a str) -> String { + format!("padding-top: {val}; padding-bottom: {val};") + } + Box::new(fun) as SpecialRule + }), + // TODO + ] + .into_iter() + .map(|(a, b)| (a.to_string(), b)) + .collect() +} diff --git a/src/lib.rs b/src/lib.rs index aa5c23c..8f9fe2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,15 +13,24 @@ mod parse; pub mod inventory; pub struct Zephyr { - /// for non-value rules + /// for non-value classes pub rules: HashMap, + /// special rules. Fn(Value) -> Properties + pub specials: HashMap, + /// list of name short-hands pub names: HashMap, + /// list of value short-hands pub values: HashMap, + /// list of modifier short-hands pub modifiers: HashMap, + /// list of pseudo-element short-hands pub pseudos: HashMap, } +/// Value -> Rules +pub type SpecialRule = Box String>; + impl Zephyr { /// builds a `Zephyr` with the default ruleset pub fn new() -> Self { @@ -31,6 +40,7 @@ impl Zephyr { values: default_values(), modifiers: default_modifiers(), pseudos: default_pseudos(), + specials: default_specials(), } } @@ -42,6 +52,7 @@ impl Zephyr { values: HashMap::new(), modifiers: HashMap::new(), pseudos: HashMap::new(), + specials: HashMap::new(), } } @@ -162,4 +173,15 @@ mod tests { ); assert_eq!(classes_separate, classes_joined); } + + #[test] + fn generate_specials_works() { + let z = Zephyr::new(); + + let classes = z.generate_classes(["mx[1rem]"]); + assert_eq!( + classes, + r#".mx\[1rem\] { margin-left: 1rem; margin-right: 1rem; }"# + ); + } }