css_math/
lib.rs

1/// Tokenizer and validation checks CSS Math statements (i.e. the string between the parens in calc() expressions)
2mod token;
3
4mod tokenize;
5use tokenize::tokenize;
6
7mod validate;
8use validate::validate;
9
10pub fn is_css_math(input: &str) -> bool {
11    match tokenize(input) {
12        Ok(tokens) => validate(&tokens),
13        Err(_) => {
14            false
15        }
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22	use test_case::test_case;
23
24	#[test_case("var(--typescale-3) * 1.5")]
25	fn valid(input: &str) {
26		assert_eq!(true, is_css_math(input))
27	}
28
29	#[test_case("12px px" ; "consecutive units without a number")]
30	#[test_case("2% rem" ; "consecutive units without a number #2")]
31	#[test_case("2px rem px" ; "consecutive units without a number #3")]
32	#[test_case("15. * 2px" ; "invalid number")]
33	fn invalid(input: &str) {
34		assert_eq!(false, is_css_math(input))
35	}
36}