implement literal values

This commit is contained in:
annieversary 2022-07-06 15:05:57 +01:00
parent a999690413
commit 19a51d5f71
3 changed files with 67 additions and 0 deletions

View File

@ -44,6 +44,8 @@ impl<'a> Class<'a> {
.replace('|', "\\|")
.replace('(', "\\(")
.replace(')', "\\)")
.replace('{', "\\{")
.replace('}', "\\}")
.replace('.', "\\.")
.replace('#', "\\#")
.replace('$', "\\$")

View File

@ -195,4 +195,16 @@ mod tests {
r#".border\[1px_solid_black\]{border:1px solid black;}"#
);
}
#[test]
fn generate_literals_works() {
let z = Zephyr::new();
// the curly brackets indicate that the value should not go through replacements
let classes = z.generate_classes(["border{1px_solid_black}", "w{full}"]);
assert_eq!(
classes,
r#".border\{1px_solid_black\}{border:1px_solid_black;}.w\{full\}{width:full;}"#
);
}
}

View File

@ -1,6 +1,9 @@
use crate::class::Class;
// TODO return error
pub(crate) fn parse_class<'a>(original: &'a str) -> Option<Class<'a>> {
// this code is kinda repetitive but idk
let (class, pseudo) = if let Some((class, pseudo)) = original.split_once('$') {
(class, Some(pseudo))
} else {
@ -24,6 +27,31 @@ pub(crate) fn parse_class<'a>(original: &'a str) -> Option<Class<'a>> {
});
}
match (pos(class, '{'), pos(class, '}')) {
(Some(start), Some(end)) if start <= end => {
let mods = if end + 1 == class.len() {
vec![]
} else {
class[end + 1..].split(',').collect()
};
return Some(Class {
name: &class[0..start],
value: Some(&class[start + 1..end]),
modifiers: mods.into(),
pseudo,
original,
value_literal: true,
});
}
// go to [...] case
(None, None) => {}
_ => {
// TODO return an error here
return None;
}
};
match (pos(class, '['), pos(class, ']')) {
(Some(start), Some(end)) if start <= end => {
let mods = if end + 1 == class.len() {
@ -78,6 +106,22 @@ mod tests {
})
);
}
fn check_literal(
class: &str,
(name, value, modifiers, pseudo): (&str, Option<&str>, Vec<&str>, Option<&str>),
) {
assert_eq!(
parse_class(class),
Some(Class {
name,
value,
modifiers: modifiers.into(),
pseudo,
original: class,
value_literal: true,
})
);
}
#[test]
fn parse_works() {
@ -97,6 +141,15 @@ mod tests {
);
}
#[test]
fn parse_literal_values() {
// testing out weird unicode stuffs
check_literal(
"hello{hey_hello}",
("hello", Some("hey_hello"), vec![], None),
);
}
#[test]
fn parse_modifier() {
check("a[b]hover", ("a", Some("b"), vec!["hover"], None));