1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
use properties::parse::{UnitRepr, unit, fn_name}; use nom::alpha; use std::str; #[derive(Debug, Clone, PartialEq)] pub struct TransformFunction<'a, 'b, 'c> { pub args: Vec<UnitRepr<'a, 'b>>, pub name: &'c str, } named!(args(&[u8]) -> Vec<UnitRepr>, delimited!( char!('('), separated_list!(char!(','), unit), char!(')') ) ); named!(pub transform_parse(&[u8]) -> TransformFunction, do_parse!( name: fn_name >> args: args >> (TransformFunction { name: str::from_utf8(name).unwrap(), args, }) )); #[cfg(test)] mod tests { use super::*; #[test] fn transform_function_parse() { use properties::parse::{LengthRepr, AngleRepr}; let my_str = "func(10px,10deg)"; let parsed = transform_parse(my_str.as_bytes()).expect("Can't parse transform").1; let expected = TransformFunction { args: vec![ UnitRepr::Length(LengthRepr { value: "10", unit: "point", }), UnitRepr::Angle(AngleRepr { value: "10", angle: "degrees", }), ], name: "func", }; assert_eq!(parsed, expected); } }