This commit is contained in:
annieversary 2022-07-02 22:33:48 +01:00
parent 444dadedba
commit d0c0b96eb3
2 changed files with 10 additions and 8 deletions

View File

@ -50,8 +50,7 @@ impl<'a> Class<'a> {
.replace('%', "\\%")
}
/// TODO return result
pub(crate) fn generate(&self, z: &Zephyr) -> String {
pub(crate) fn generate(&self, z: &Zephyr) -> Result<String, &'static str> {
let name = z
.names
.get(self.name)
@ -61,11 +60,11 @@ impl<'a> Class<'a> {
if let Some(val) = self.value {
let val = z.values.get(val).map(AsRef::as_ref).unwrap_or(val);
format!("{selector} {{ {name}: {val}; }}",)
Ok(format!("{selector} {{ {name}: {val}; }}",))
} else if let Some(v) = z.rules.get(name) {
format!("{selector} {{ {v} }}",)
Ok(format!("{selector} {{ {v} }}",))
} else {
panic!("{name} is not a no-variable rule, and no variables were provided");
Err("{name} is not a no-variable rule, and no variables were provided")
}
}
}

View File

@ -52,13 +52,16 @@ impl Zephyr {
.into_iter()
.flat_map(|s| s.split_ascii_whitespace())
// TODO skip duplicates, use hashset or smth
.flat_map(|c| self.generate_class(c))
// we ignore errors
.flat_map(|c| self.generate_class(c).ok().flatten())
.collect::<Vec<_>>()
.join("")
}
pub fn generate_class(&self, class: &str) -> Option<String> {
parse_class(class).map(|c| c.generate(self))
/// this one returns an error if parsing or generating fails
// TODO add an error type
pub fn generate_class(&self, class: &str) -> Result<Option<String>, &'static str> {
parse_class(class).map(|c| c.generate(self)).transpose()
}
}