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