svgtypes/
funciri.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::{Error, Stream};

/// Representation of the [`<IRI>`] type.
///
/// [`<IRI>`]: https://www.w3.org/TR/SVG11/types.html#DataTypeIRI
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct IRI<'a>(pub &'a str);

impl<'a> IRI<'a> {
    /// Parsers a `IRI` from a string.
    ///
    /// By the SVG spec, the ID must contain only [Name] characters,
    /// but since no one fallows this it will parse any characters.
    ///
    /// We can't use the `FromStr` trait because it requires
    /// an owned value as a return type.
    ///
    /// [Name]: https://www.w3.org/TR/xml/#NT-Name
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(text: &'a str) -> Result<Self, Error> {
        let mut s = Stream::from(text);
        let link = s.parse_iri()?;
        s.skip_spaces();
        if !s.at_end() {
            return Err(Error::UnexpectedData(s.calc_char_pos()));
        }

        Ok(Self(link))
    }
}

/// Representation of the [`<FuncIRI>`] type.
///
/// [`<FuncIRI>`]: https://www.w3.org/TR/SVG11/types.html#DataTypeFuncIRI
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct FuncIRI<'a>(pub &'a str);

impl<'a> FuncIRI<'a> {
    /// Parsers a `FuncIRI` from a string.
    ///
    /// By the SVG spec, the ID must contain only [Name] characters,
    /// but since no one fallows this it will parse any characters.
    ///
    /// We can't use the `FromStr` trait because it requires
    /// an owned value as a return type.
    ///
    /// [Name]: https://www.w3.org/TR/xml/#NT-Name
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(text: &'a str) -> Result<Self, Error> {
        let mut s = Stream::from(text);
        let link = s.parse_func_iri()?;
        s.skip_spaces();
        if !s.at_end() {
            return Err(Error::UnexpectedData(s.calc_char_pos()));
        }

        Ok(Self(link))
    }
}

impl<'a> Stream<'a> {
    pub fn parse_iri(&mut self) -> Result<&'a str, Error> {
        self.skip_spaces();
        self.consume_byte(b'#')?;
        let link = self.consume_bytes(|_, c| c != b' ');
        if link.is_empty() {
            return Err(Error::InvalidValue);
        }
        Ok(link)
    }

    pub fn parse_func_iri(&mut self) -> Result<&'a str, Error> {
        self.skip_spaces();
        self.consume_string(b"url(")?;
        self.skip_spaces();

        let quote = match self.curr_byte() {
            Ok(b'\'') | Ok(b'"') => self.curr_byte().ok(),
            _ => None,
        };
        if quote.is_some() {
            self.advance(1);
            self.skip_spaces();
        }
        self.consume_byte(b'#')?;
        let link = if let Some(quote) = quote {
            self.consume_bytes(|_, c| c != quote).trim_end()
        } else {
            self.consume_bytes(|_, c| c != b' ' && c != b')')
        };
        if link.is_empty() {
            return Err(Error::InvalidValue);
        }
        // Non-paired quotes is an error.
        if link.contains('\'') || link.contains('"') {
            return Err(Error::InvalidValue);
        }
        self.skip_spaces();
        if let Some(quote) = quote {
            self.consume_byte(quote)?;
            self.skip_spaces();
        }
        self.consume_byte(b')')?;
        Ok(link)
    }
}

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

    #[test]
    fn parse_iri_1() {
        assert_eq!(IRI::from_str("#id").unwrap(), IRI("id"));
    }

    #[test]
    fn parse_iri_2() {
        assert_eq!(IRI::from_str("   #id   ").unwrap(), IRI("id"));
    }

    #[test]
    fn parse_iri_3() {
        // Trailing data is ok for the Stream, by not for IRI.
        assert_eq!(Stream::from("   #id   text").parse_iri().unwrap(), "id");
        assert_eq!(IRI::from_str("   #id   text").unwrap_err().to_string(),
                   "unexpected data at position 10");
    }

    #[test]
    fn parse_iri_4() {
        assert_eq!(IRI::from_str("#1").unwrap(), IRI("1"));
    }

    #[test]
    fn parse_err_iri_1() {
        assert_eq!(IRI::from_str("# id").unwrap_err().to_string(), "invalid value");
    }

    #[test]
    fn parse_func_iri_1() {
        assert_eq!(FuncIRI::from_str("url(#id)").unwrap(), FuncIRI("id"));
    }

    #[test]
    fn parse_func_iri_2() {
        assert_eq!(FuncIRI::from_str("url(#1)").unwrap(), FuncIRI("1"));
    }

    #[test]
    fn parse_func_iri_3() {
        assert_eq!(FuncIRI::from_str("    url(    #id    )   ").unwrap(), FuncIRI("id"));
    }

    #[test]
    fn parse_func_iri_4() {
        // Trailing data is ok for the Stream, by not for FuncIRI.
        assert_eq!(Stream::from("url(#id) qwe").parse_func_iri().unwrap(), "id");
        assert_eq!(FuncIRI::from_str("url(#id) qwe").unwrap_err().to_string(),
                   "unexpected data at position 10");
    }

    #[test]
    fn parse_func_iri_5() {
        assert_eq!(FuncIRI::from_str("url('#id')").unwrap(), FuncIRI("id"));
        assert_eq!(FuncIRI::from_str("url(' #id ')").unwrap(), FuncIRI("id"));
    }

    #[test]
    fn parse_func_iri_6() {
        assert_eq!(FuncIRI::from_str("url(\"#id\")").unwrap(), FuncIRI("id"));
        assert_eq!(FuncIRI::from_str("url(\" #id \")").unwrap(), FuncIRI("id"));
    }

    #[test]
    fn parse_err_func_iri_1() {
        assert_eq!(FuncIRI::from_str("url ( #1 )").unwrap_err().to_string(),
                   "expected 'url(' not 'url ' at position 1");
    }

    #[test]
    fn parse_err_func_iri_2() {
        assert_eq!(FuncIRI::from_str("url(#)").unwrap_err().to_string(), "invalid value");
    }

    #[test]
    fn parse_err_func_iri_3() {
        assert_eq!(FuncIRI::from_str("url(# id)").unwrap_err().to_string(),
                   "invalid value");
    }

    #[test]
    fn parse_err_func_iri_4() {
        // If single quotes are present around the ID, they should be on both sides
        assert_eq!(FuncIRI::from_str("url('#id)").unwrap_err().to_string(),
                   "unexpected end of stream");
        assert_eq!(FuncIRI::from_str("url(#id')").unwrap_err().to_string(),
                   "invalid value");
    }
}