svgtypes/
directional_position.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
use crate::{Error, Length, LengthUnit, Stream};

/// List of all SVG directional positions.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DirectionalPosition {
    /// The `top` position.
    Top,
    /// The `center` position.
    Center,
    /// The `bottom` position.
    Bottom,
    /// The `right` position.
    Right,
    /// The `left` position.
    Left,
}

impl DirectionalPosition {
    /// Checks whether the value can be a horizontal position.
    #[inline]
    pub fn is_horizontal(&self) -> bool {
        match self {
            DirectionalPosition::Center
            | DirectionalPosition::Left
            | DirectionalPosition::Right => true,
            _ => false,
        }
    }

    /// Checks whether the value can be a vertical position.
    #[inline]
    pub fn is_vertical(&self) -> bool {
        match self {
            DirectionalPosition::Center
            | DirectionalPosition::Top
            | DirectionalPosition::Bottom => true,
            _ => false,
        }
    }
}

impl From<DirectionalPosition> for Length {
    fn from(value: DirectionalPosition) -> Self {
        match value {
            DirectionalPosition::Left | DirectionalPosition::Top => {
                Length::new(0.0, LengthUnit::Percent)
            }
            DirectionalPosition::Right | DirectionalPosition::Bottom => {
                Length::new(100.0, LengthUnit::Percent)
            }
            DirectionalPosition::Center => Length::new(50.0, LengthUnit::Percent),
        }
    }
}

impl std::str::FromStr for DirectionalPosition {
    type Err = Error;

    #[inline]
    fn from_str(text: &str) -> Result<Self, Error> {
        let mut s = Stream::from(text);
        let dir_pos = s.parse_directional_position()?;

        if !s.at_end() {
            return Err(Error::UnexpectedData(s.calc_char_pos()));
        }

        Ok(dir_pos)
    }
}

impl<'a> Stream<'a> {
    /// Parses a directional position [`left`, `center`, `right`, `bottom`, `top`] from the stream.
    pub fn parse_directional_position(&mut self) -> Result<DirectionalPosition, Error> {
        self.skip_spaces();

        if self.starts_with(b"left") {
            self.advance(4);
            return Ok(DirectionalPosition::Left);
        } else if self.starts_with(b"right") {
            self.advance(5);
            return Ok(DirectionalPosition::Right);
        } else if self.starts_with(b"top") {
            self.advance(3);
            return Ok(DirectionalPosition::Top);
        } else if self.starts_with(b"bottom") {
            self.advance(6);
            return Ok(DirectionalPosition::Bottom);
        } else if self.starts_with(b"center") {
            self.advance(6);
            return Ok(DirectionalPosition::Center);
        } else {
            return Err(Error::InvalidString(
                vec![
                    self.slice_tail().to_string(),
                    "left".to_string(),
                    "right".to_string(),
                    "top".to_string(),
                    "bottom".to_string(),
                    "center".to_string(),
                ],
                self.calc_char_pos(),
            ));
        }
    }
}

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

    macro_rules! test_p {
        ($name:ident, $text:expr, $result:expr) => (
            #[test]
            fn $name() {
                assert_eq!(DirectionalPosition::from_str($text).unwrap(), $result);
            }
        )
    }

    test_p!(parse_1,  "left", DirectionalPosition::Left);
    test_p!(parse_2,  "right", DirectionalPosition::Right);
    test_p!(parse_3,  "center", DirectionalPosition::Center);
    test_p!(parse_4,  "top", DirectionalPosition::Top);
    test_p!(parse_5,  "bottom", DirectionalPosition::Bottom);

    #[test]
    fn parse_6() {
        let mut s = Stream::from("left,");
        assert_eq!(s.parse_directional_position().unwrap(), DirectionalPosition::Left);
    }

    #[test]
    fn parse_7() {
        let mut s = Stream::from("left ,");
        assert_eq!(s.parse_directional_position().unwrap(), DirectionalPosition::Left);
    }

    #[test]
    fn parse_16() {
        let mut s = Stream::from("left center");
        assert_eq!(s.parse_directional_position().unwrap(), DirectionalPosition::Left);
    }

    #[test]
    fn err_1() {
        let mut s = Stream::from("something");
        assert_eq!(s.parse_directional_position().unwrap_err().to_string(),
                   "expected 'left', 'right', 'top', 'bottom', 'center' not 'something' at position 1");
    }
}