rss/
image.rs

1use quick_xml::{XmlReader, Event, Element};
2
3use fromxml::FromXml;
4use error::Error;
5
6/// A representation of the `<image>` element.
7#[derive(Debug, Default, Clone, PartialEq)]
8pub struct Image {
9    /// The URL of the channel image.
10    pub url: String,
11    /// A description of the image. This is used in the HTML `alt` attribute.
12    pub title: String,
13    /// The URL that the image links to.
14    pub link: String,
15    /// The width of the image.
16    pub width: Option<String>,
17    /// The height of the image.
18    pub height: Option<String>,
19    /// The text for the HTML `title` attribute.
20    pub description: Option<String>,
21}
22
23impl FromXml for Image {
24    fn from_xml<R: ::std::io::BufRead>(mut reader: XmlReader<R>,
25                                       _: Element)
26                                       -> Result<(Self, XmlReader<R>), Error> {
27        let mut url = None;
28        let mut title = None;
29        let mut link = None;
30        let mut width = None;
31        let mut height = None;
32        let mut description = None;
33
34        while let Some(e) = reader.next() {
35            match e {
36                Ok(Event::Start(element)) => {
37                    match element.name() {
38                        b"url" => url = element_text!(reader),
39                        b"title" => title = element_text!(reader),
40                        b"link" => link = element_text!(reader),
41                        b"width" => width = element_text!(reader),
42                        b"height" => height = element_text!(reader),
43                        b"description" => description = element_text!(reader),
44                        _ => skip_element!(reader),
45                    }
46                }
47                Ok(Event::End(_)) => {
48                    let url = url.unwrap_or_default();
49                    let title = title.unwrap_or_default();
50                    let link = link.unwrap_or_default();
51
52                    return Ok((Image {
53                        url: url,
54                        title: title,
55                        link: link,
56                        width: width,
57                        height: height,
58                        description: description,
59                    }, reader))
60                }
61                Err(err) => return Err(err.into()),
62                _ => {}
63            }
64        }
65
66        Err(Error::EOF)
67    }
68}