1
//! `userSpaceOnUse` or `objectBoundingBox` values.
2

            
3
use cssparser::Parser;
4

            
5
use crate::error::*;
6
use crate::parse_identifiers;
7
use crate::parsers::Parse;
8

            
9
/// Defines the units to be used for things that can consider a
10
/// coordinate system in terms of the current transformation, or in
11
/// terms of the current object's bounding box.
12
122
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
13
pub enum CoordUnits {
14
    UserSpaceOnUse,
15
    ObjectBoundingBox,
16
}
17

            
18
impl Parse for CoordUnits {
19
394
    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> {
20
787
        Ok(parse_identifiers!(
21
            parser,
22
            "userSpaceOnUse" => CoordUnits::UserSpaceOnUse,
23
            "objectBoundingBox" => CoordUnits::ObjectBoundingBox,
24
1
        )?)
25
394
    }
26
}
27

            
28
/// Creates a newtype around `CoordUnits`, with a default value.
29
///
30
/// SVG attributes that can take `userSpaceOnUse` or
31
/// `objectBoundingBox` values often have different default values
32
/// depending on the type of SVG element.  We use this macro to create
33
/// a newtype for each SVG element and attribute that requires values
34
/// of this type.  The newtype provides an `impl Default` with the
35
/// specified `$default` value.
36
#[doc(hidden)]
37
#[macro_export]
38
macro_rules! coord_units {
39
    ($name:ident, $default:expr) => {
40
500039
        #[derive(Debug, Copy, Clone, PartialEq, Eq)]
41
3
        pub struct $name(pub CoordUnits);
42

            
43
        impl Default for $name {
44
500156
            fn default() -> Self {
45
500156
                $name($default)
46
500156
            }
47
        }
48

            
49
        impl From<$name> for CoordUnits {
50
267
            fn from(u: $name) -> Self {
51
                u.0
52
267
            }
53
        }
54

            
55
        impl $crate::parsers::Parse for $name {
56
234
            fn parse<'i>(
57
                parser: &mut ::cssparser::Parser<'i, '_>,
58
            ) -> Result<Self, $crate::error::ParseError<'i>> {
59
234
                Ok($name($crate::coord_units::CoordUnits::parse(parser)?))
60
234
            }
61
        }
62
    };
63
}
64

            
65
#[cfg(test)]
66
mod tests {
67
    use super::*;
68

            
69
1
    coord_units!(MyUnits, CoordUnits::ObjectBoundingBox);
70

            
71
    #[test]
72
2
    fn parsing_invalid_strings_yields_error() {
73
1
        assert!(MyUnits::parse_str("").is_err());
74
1
        assert!(MyUnits::parse_str("foo").is_err());
75
2
    }
76

            
77
    #[test]
78
2
    fn parses_paint_server_units() {
79
1
        assert_eq!(
80
1
            MyUnits::parse_str("userSpaceOnUse").unwrap(),
81
            MyUnits(CoordUnits::UserSpaceOnUse)
82
        );
83
1
        assert_eq!(
84
1
            MyUnits::parse_str("objectBoundingBox").unwrap(),
85
            MyUnits(CoordUnits::ObjectBoundingBox)
86
        );
87
2
    }
88

            
89
    #[test]
90
2
    fn has_correct_default() {
91
1
        assert_eq!(MyUnits::default(), MyUnits(CoordUnits::ObjectBoundingBox));
92
2
    }
93

            
94
    #[test]
95
2
    fn converts_to_coord_units() {
96
1
        assert_eq!(
97
1
            CoordUnits::from(MyUnits(CoordUnits::ObjectBoundingBox)),
98
            CoordUnits::ObjectBoundingBox
99
        );
100
2
    }
101
}