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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use nom::{digit, IResult, Err, Needed, alpha};
use css_color_parser::Color as CssColor;
use properties::Color;
use std::str;

// Function name
named!(pub fn_name(&[u8]) -> &[u8], ws!(alpha));

// Unit representation and parser
#[derive(Debug, Clone, PartialEq)]
pub enum UnitRepr<'a, 'b> {
    Length(LengthRepr<'a, 'b>),
    Angle(AngleRepr<'a, 'b>),
}

named!(pub unit(&[u8]) -> UnitRepr, alt!(length | angle));

// Length representation and parser
#[derive(Debug, Clone, PartialEq)]
pub struct LengthRepr<'a, 'b> {
    pub value: &'a str,
    pub unit: &'b str,
}

named!(length_type<&[u8], &str>, alt!(
  tag!("%")   => { |_| "percent" } |
  tag!("n")   => { |_| "number" } | 
  tag!("px")  => { |_| "point" }
));

named!(pub length(&[u8]) -> UnitRepr, do_parse!(
  value: digit      >>
  unit: length_type >>
  (UnitRepr::Length(LengthRepr {
    value: str::from_utf8(value).unwrap(),
    unit
  }))
));

// Angle representation and parser
#[derive(Debug, Clone, PartialEq)]
pub struct AngleRepr<'a, 'b> {
    pub value: &'a str,
    pub angle: &'b str,
}

named!(angle_type<&[u8], &str>, alt!(
  tag!("rad") => { |_| "radians" } |
  tag!("deg") => { |_| "degrees" }
));

named!(pub angle(&[u8]) -> UnitRepr, do_parse!(
  value: digit      >>
  angle: angle_type >>
  (UnitRepr::Angle(AngleRepr {
    value: str::from_utf8(value).unwrap(),
    angle
  }))
));

// Stop representation and parse
#[derive(Debug, Clone, PartialEq)]
pub struct GradientStopRepr {
    pub color: Color,
    pub offset: f32,
}

fn take_color(input: &[u8]) -> IResult<&[u8], CssColor> {
    let color = str::from_utf8(input.clone()).unwrap();
    let color = color.parse::<CssColor>().or(Err(Err::Incomplete(Needed::Unknown)))?;
    Ok((&[], color))
}

fn prepare_gradient_stop(color: CssColor, offset: &[u8]) -> GradientStopRepr {
    let offset = str::from_utf8(offset.clone()).unwrap();
    let offset = offset.parse::<f32>().unwrap_or(0.0);
    let color = color.into();

    GradientStopRepr {
        offset,
        color,
    }
}

named!(pub gradient_stop(&[u8]) -> GradientStopRepr, do_parse!(
  color: map_res!(take_until!(" "), take_color) >>
  char!(' ')        >>
  offset: digit     >>
  char!('%')        >>
  (prepare_gradient_stop(color.1, offset))
));

// Tests of parse expressions
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn gradient_stop_parse_hex() {
        let stop_hex = "#FFF 10%";
        let parsed_hex = gradient_stop(stop_hex.as_bytes());
        let expected = GradientStopRepr {
            color: Color {
                red: 255,
                green: 255,
                blue: 255,
                alpha: 1.0,
            },
            offset: 10.0,
        };
        assert_eq!(parsed_hex.unwrap().1, expected);
    }

    #[test]
    fn gradient_stop_parse_rgb() {
        let stop_rgb = "rgb(10,10,10) 10%";
        let parsed_rgb = gradient_stop(stop_rgb.as_bytes());

        let expected = GradientStopRepr {
            color: Color {
                red: 10,
                green: 10,
                blue: 10,
                alpha: 1.0,
            },
            offset: 10.0,
        };

        assert_eq!(parsed_rgb.unwrap().1, expected);
    }

    #[test]
    fn gradient_stop_parse_rgba() {
        let stop_rgba = "rgba(10,10,10,0.1) 10%";
        let parsed_rgba = gradient_stop(stop_rgba.as_bytes());

        let expected = GradientStopRepr {
            color: Color {
                red: 10,
                green: 10,
                blue: 10,
                alpha: 0.1,
            },
            offset: 10.0,
        };

        assert_eq!(parsed_rgba.unwrap().1, expected);
    }
}