diff --git a/src/class.rs b/src/class.rs index 5698baf..4edaddb 100644 --- a/src/class.rs +++ b/src/class.rs @@ -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 { 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") } } } diff --git a/src/lib.rs b/src/lib.rs index 0b0d6d1..f37c70a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::>() .join("") } - pub fn generate_class(&self, class: &str) -> Option { - 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, &'static str> { + parse_class(class).map(|c| c.generate(self)).transpose() } }