fix nomenclature to be correct
This commit is contained in:
parent
4136af68d8
commit
86e99939a0
16
src/class.rs
16
src/class.rs
|
@ -2,7 +2,7 @@ use crate::{Zephyr, ZephyrError};
|
|||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub(crate) struct Class<'a> {
|
||||
pub name: &'a str,
|
||||
pub property: &'a str,
|
||||
pub value: Option<&'a str>,
|
||||
/// if true, no replacements will be done on `value`
|
||||
pub value_literal: bool,
|
||||
|
@ -57,11 +57,11 @@ impl<'a> Class<'a> {
|
|||
}
|
||||
|
||||
pub(crate) fn generate(&self, z: &Zephyr) -> Result<String, ZephyrError> {
|
||||
let name = z
|
||||
.names
|
||||
.get(self.name)
|
||||
let property = z
|
||||
.properties
|
||||
.get(self.property)
|
||||
.map(AsRef::as_ref)
|
||||
.unwrap_or(self.name);
|
||||
.unwrap_or(self.property);
|
||||
let selector = self.selector(z);
|
||||
|
||||
if let Some(val) = self.value {
|
||||
|
@ -75,13 +75,13 @@ impl<'a> Class<'a> {
|
|||
.replace('_', " ")
|
||||
};
|
||||
|
||||
if let Some(fun) = z.specials.get(name) {
|
||||
if let Some(fun) = z.specials.get(property) {
|
||||
let v = fun(&val);
|
||||
Ok(format!("{selector}{{{v}}}",))
|
||||
} else {
|
||||
Ok(format!("{selector}{{{name}:{val};}}"))
|
||||
Ok(format!("{selector}{{{property}:{val};}}"))
|
||||
}
|
||||
} else if let Some(v) = z.rules.get(name) {
|
||||
} else if let Some(v) = z.declarations.get(property) {
|
||||
Ok(format!("{selector}{{{v}}}"))
|
||||
} else {
|
||||
Err(ZephyrError::ValueMissing)
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use crate::SpecialRule;
|
||||
use crate::SpecialDeclaration;
|
||||
|
||||
pub(crate) fn default_rules() -> HashMap<String, String> {
|
||||
pub(crate) fn default_declarations() -> HashMap<String, String> {
|
||||
vec![
|
||||
("flex", "display:flex;"),
|
||||
("flex-row", "display:flex;flex-direction:row;"),
|
||||
|
@ -16,7 +16,7 @@ pub(crate) fn default_rules() -> HashMap<String, String> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn default_names() -> HashMap<String, String> {
|
||||
pub(crate) fn default_properties() -> HashMap<String, String> {
|
||||
vec![
|
||||
("w", "width"),
|
||||
("h", "height"),
|
||||
|
@ -79,12 +79,12 @@ macro_rules! special {
|
|||
fn fun<'a>($val: &'a str) -> String {
|
||||
format!($string)
|
||||
}
|
||||
Box::new(fun) as SpecialRule
|
||||
Box::new(fun) as SpecialDeclaration
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) fn default_specials() -> HashMap<String, SpecialRule> {
|
||||
pub(crate) fn default_specials() -> HashMap<String, SpecialDeclaration> {
|
||||
vec![
|
||||
special!("mx", val, "margin-left:{val};margin-right:{val};"),
|
||||
special!("my", val, "margin-top:{val};margin-bottom:{val};"),
|
||||
|
|
30
src/lib.rs
30
src/lib.rs
|
@ -14,22 +14,22 @@ pub mod inventory;
|
|||
|
||||
pub struct Zephyr {
|
||||
/// for non-value classes
|
||||
pub rules: HashMap<String, String>,
|
||||
/// special rules. Fn(Value) -> Properties
|
||||
pub specials: HashMap<String, SpecialRule>,
|
||||
pub declarations: HashMap<String, String>,
|
||||
/// special declarations. Fn(Value) -> declarations
|
||||
pub specials: HashMap<String, SpecialDeclaration>,
|
||||
|
||||
/// list of name short-hands
|
||||
pub names: HashMap<String, String>,
|
||||
/// list of property short-hands
|
||||
pub properties: HashMap<String, String>,
|
||||
/// list of value short-hands
|
||||
pub values: HashMap<String, String>,
|
||||
/// list of modifier short-hands
|
||||
/// list of pseudo-class short-hands
|
||||
pub modifiers: HashMap<String, String>,
|
||||
/// list of pseudo-element short-hands
|
||||
pub pseudos: HashMap<String, String>,
|
||||
}
|
||||
|
||||
/// Value -> Rules
|
||||
pub type SpecialRule = Box<dyn Fn(&str) -> String>;
|
||||
/// Value -> declarations
|
||||
pub type SpecialDeclaration = Box<dyn Fn(&str) -> String>;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum ZephyrError {
|
||||
|
@ -43,8 +43,8 @@ impl Zephyr {
|
|||
/// builds a `Zephyr` with the default ruleset
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
rules: default_rules(),
|
||||
names: default_names(),
|
||||
declarations: default_declarations(),
|
||||
properties: default_properties(),
|
||||
values: default_values(),
|
||||
modifiers: default_modifiers(),
|
||||
pseudos: default_pseudos(),
|
||||
|
@ -55,8 +55,8 @@ impl Zephyr {
|
|||
/// builds a `Zephyr` without the default ruleset
|
||||
pub fn new_without_defaults() -> Self {
|
||||
Self {
|
||||
rules: HashMap::new(),
|
||||
names: HashMap::new(),
|
||||
declarations: HashMap::new(),
|
||||
properties: HashMap::new(),
|
||||
values: HashMap::new(),
|
||||
modifiers: HashMap::new(),
|
||||
pseudos: HashMap::new(),
|
||||
|
@ -125,7 +125,7 @@ mod tests {
|
|||
let z = Zephyr::new();
|
||||
|
||||
let class = Class {
|
||||
name: "m",
|
||||
property: "m",
|
||||
value: Some("1rem"),
|
||||
modifiers: vec![].into(),
|
||||
pseudo: None,
|
||||
|
@ -136,7 +136,7 @@ mod tests {
|
|||
assert_eq!(css, r#".m\[1rem\]{margin:1rem;}"#);
|
||||
|
||||
let class = Class {
|
||||
name: "m",
|
||||
property: "m",
|
||||
value: Some("1rem"),
|
||||
modifiers: vec!["focus"].into(),
|
||||
pseudo: None,
|
||||
|
@ -147,7 +147,7 @@ mod tests {
|
|||
assert_eq!(css, r#".m\[1rem\]focus:focus{margin:1rem;}"#);
|
||||
|
||||
let class = Class {
|
||||
name: "m",
|
||||
property: "m",
|
||||
value: Some("1rem"),
|
||||
modifiers: vec!["focus", "hover", "odd"].into(),
|
||||
pseudo: None,
|
||||
|
|
16
src/parse.rs
16
src/parse.rs
|
@ -17,7 +17,7 @@ pub(crate) fn parse_class<'a>(original: &'a str) -> Result<Class<'a>, ZephyrErro
|
|||
};
|
||||
|
||||
return Ok(Class {
|
||||
name: &class[0..p],
|
||||
property: &class[0..p],
|
||||
value: None,
|
||||
modifiers: mods.into(),
|
||||
pseudo,
|
||||
|
@ -35,7 +35,7 @@ pub(crate) fn parse_class<'a>(original: &'a str) -> Result<Class<'a>, ZephyrErro
|
|||
};
|
||||
|
||||
return Ok(Class {
|
||||
name: &class[0..start],
|
||||
property: &class[0..start],
|
||||
value: Some(&class[start + 1..end]),
|
||||
modifiers: mods.into(),
|
||||
pseudo,
|
||||
|
@ -60,7 +60,7 @@ pub(crate) fn parse_class<'a>(original: &'a str) -> Result<Class<'a>, ZephyrErro
|
|||
};
|
||||
|
||||
return Ok(Class {
|
||||
name: &class[0..start],
|
||||
property: &class[0..start],
|
||||
value: Some(&class[start + 1..end]),
|
||||
modifiers: mods.into(),
|
||||
pseudo,
|
||||
|
@ -70,7 +70,7 @@ pub(crate) fn parse_class<'a>(original: &'a str) -> Result<Class<'a>, ZephyrErro
|
|||
}
|
||||
_ => {
|
||||
return Ok(Class {
|
||||
name: &class[0..],
|
||||
property: &class[0..],
|
||||
value: None,
|
||||
modifiers: vec![].into(),
|
||||
pseudo,
|
||||
|
@ -91,12 +91,12 @@ mod tests {
|
|||
|
||||
fn check(
|
||||
class: &str,
|
||||
(name, value, modifiers, pseudo): (&str, Option<&str>, Vec<&str>, Option<&str>),
|
||||
(property, value, modifiers, pseudo): (&str, Option<&str>, Vec<&str>, Option<&str>),
|
||||
) {
|
||||
assert_eq!(
|
||||
parse_class(class),
|
||||
Ok(Class {
|
||||
name,
|
||||
property,
|
||||
value,
|
||||
modifiers: modifiers.into(),
|
||||
pseudo,
|
||||
|
@ -107,12 +107,12 @@ mod tests {
|
|||
}
|
||||
fn check_literal(
|
||||
class: &str,
|
||||
(name, value, modifiers, pseudo): (&str, Option<&str>, Vec<&str>, Option<&str>),
|
||||
(property, value, modifiers, pseudo): (&str, Option<&str>, Vec<&str>, Option<&str>),
|
||||
) {
|
||||
assert_eq!(
|
||||
parse_class(class),
|
||||
Ok(Class {
|
||||
name,
|
||||
property,
|
||||
value,
|
||||
modifiers: modifiers.into(),
|
||||
pseudo,
|
||||
|
|
Loading…
Reference in New Issue