svgtypes/
paint_order.rs

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::stream::Stream;

/// [`paint-order`] property variants.
///
/// [`paint-order`]: https://www.w3.org/TR/SVG2/painting.html#PaintOrder
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[allow(missing_docs)]
pub enum PaintOrderKind {
    Fill,
    Stroke,
    Markers,
}

/// Representation of the [`paint-order`] property.
///
/// [`paint-order`]: https://www.w3.org/TR/SVG2/painting.html#PaintOrder
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct PaintOrder {
    /// The order.
    ///
    /// Guarantee to not have duplicates.
    pub order: [PaintOrderKind; 3],
}

impl Default for PaintOrder {
    #[inline]
    fn default() -> Self {
        Self {
            order: [
                PaintOrderKind::Fill,
                PaintOrderKind::Stroke,
                PaintOrderKind::Markers,
            ],
        }
    }
}

impl From<[PaintOrderKind; 3]> for PaintOrder {
    #[inline]
    fn from(order: [PaintOrderKind; 3]) -> Self {
        Self { order }
    }
}

impl std::str::FromStr for PaintOrder {
    type Err = ();

    /// Parses `PaintOrder` from a string.
    ///
    /// Never returns an error and fallbacks to the default value instead.
    fn from_str(text: &str) -> Result<Self, Self::Err> {
        let mut order = Vec::new();

        let mut left = vec![
            PaintOrderKind::Fill,
            PaintOrderKind::Stroke,
            PaintOrderKind::Markers,
        ];

        let mut s = Stream::from(text);
        while !s.at_end() && order.len() < 3 {
            s.skip_spaces();
            let name = s.consume_ascii_ident();
            s.skip_spaces();
            let name = match name {
                // `normal` is the special value that should short-circuit.
                "normal" => return Ok(PaintOrder::default()),
                "fill" => PaintOrderKind::Fill,
                "stroke" => PaintOrderKind::Stroke,
                "markers" => PaintOrderKind::Markers,
                _ => return Ok(PaintOrder::default()),
            };

            if let Some(index) = left.iter().position(|v| *v == name) {
                left.remove(index);
            }

            order.push(name);
        }

        s.skip_spaces();
        if !s.at_end() {
            // Any trailing data is an error.
            return Ok(PaintOrder::default());
        }

        if order.is_empty() {
            return Ok(PaintOrder::default());
        }

        // Any missing values should be added in the original order.
        while order.len() < 3 && !left.is_empty() {
            order.push(left.remove(0));
        }

        // Any duplicates is an error.
        if order[0] == order[1] || order[0] == order[2] || order[1] == order[2] {
            // Any trailing data is an error.
            return Ok(PaintOrder::default());
        }

        Ok(PaintOrder {
            order: [order[0], order[1], order[2]],
        })
    }
}

#[rustfmt::skip]
#[cfg(test)]
mod tests {
    use super::*;
    use std::str::FromStr;

    #[test]
    fn parse_1() {
        assert_eq!(PaintOrder::from_str("normal").unwrap(), PaintOrder::default());
    }

    #[test]
    fn parse_2() {
        assert_eq!(PaintOrder::from_str("qwe").unwrap(), PaintOrder::default());
    }

    #[test]
    fn parse_3() {
        assert_eq!(PaintOrder::from_str("").unwrap(), PaintOrder::default());
    }

    #[test]
    fn parse_4() {
        assert_eq!(PaintOrder::from_str("stroke qwe").unwrap(), PaintOrder::default());
    }

    #[test]
    fn parse_5() {
        assert_eq!(PaintOrder::from_str("stroke stroke").unwrap(), PaintOrder::default());
    }

    #[test]
    fn parse_6() {
        assert_eq!(PaintOrder::from_str("stroke").unwrap(), PaintOrder::from([
            PaintOrderKind::Stroke, PaintOrderKind::Fill, PaintOrderKind::Markers
        ]));
    }

    #[test]
    fn parse_7() {
        assert_eq!(PaintOrder::from_str("stroke markers").unwrap(), PaintOrder::from([
            PaintOrderKind::Stroke, PaintOrderKind::Markers, PaintOrderKind::Fill
        ]));
    }

    #[test]
    fn parse_8() {
        assert_eq!(PaintOrder::from_str("stroke markers fill").unwrap(), PaintOrder::from([
            PaintOrderKind::Stroke, PaintOrderKind::Markers, PaintOrderKind::Fill
        ]));
    }

    #[test]
    fn parse_9() {
        assert_eq!(PaintOrder::from_str("markers").unwrap(), PaintOrder::from([
            PaintOrderKind::Markers, PaintOrderKind::Fill, PaintOrderKind::Stroke
        ]));
    }

    #[test]
    fn parse_10() {
        assert_eq!(PaintOrder::from_str("  stroke\n").unwrap(), PaintOrder::from([
            PaintOrderKind::Stroke, PaintOrderKind::Fill, PaintOrderKind::Markers
        ]));
    }

    #[test]
    fn parse_11() {
        assert_eq!(PaintOrder::from_str("stroke stroke stroke stroke").unwrap(), PaintOrder::default());
    }
}