svgparser/
values_list.rs

1// Copyright 2018 Evgeniy Reizner
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std::fmt;
10
11use xmlparser::{
12    FromSpan,
13    Stream,
14    StrSpan,
15};
16
17use error::{
18    StreamResult,
19};
20use {
21    Length,
22    StreamExt,
23};
24
25/// Iterator over a list of [`<number>`] values.
26///
27/// [`<number>`]: https://www.w3.org/TR/SVG/types.html#DataTypeNumber
28#[derive(Copy, Clone, PartialEq)]
29pub struct NumberList<'a>(Stream<'a>);
30
31impl<'a> FromSpan<'a> for NumberList<'a> {
32    fn from_span(span: StrSpan<'a>) -> Self {
33        NumberList(Stream::from_span(span))
34    }
35}
36
37impl<'a> fmt::Debug for NumberList<'a> {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        write!(f, "NumberList({:?})", self.0.span())
40    }
41}
42
43impl<'a> Iterator for NumberList<'a> {
44    type Item = StreamResult<f64>;
45
46    fn next(&mut self) -> Option<Self::Item> {
47        if self.0.at_end() {
48            None
49        } else {
50            Some(self.0.parse_list_number())
51        }
52    }
53}
54
55/// Iterator over a list of [`<length>`] values.
56///
57/// [`<length>`]: https://www.w3.org/TR/SVG/types.html#DataTypeLength
58#[derive(Copy, Clone, PartialEq)]
59pub struct LengthList<'a>(Stream<'a>);
60
61impl<'a> LengthList<'a> {
62    /// Constructs a new `LengthList` from `StrSpan`.
63    pub fn from_span(span: StrSpan<'a>) -> LengthList<'a> {
64        LengthList(Stream::from_span(span))
65    }
66}
67
68impl<'a> fmt::Debug for LengthList<'a> {
69    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70        write!(f, "LengthList({:?})", self.0.span())
71    }
72}
73
74impl<'a> Iterator for LengthList<'a> {
75    type Item = StreamResult<Length>;
76
77    fn next(&mut self) -> Option<Self::Item> {
78        if self.0.at_end() {
79            None
80        } else {
81            Some(self.0.parse_list_length())
82        }
83    }
84}