rss/
enclosure.rs

1use quick_xml::{XmlReader, Element};
2
3use fromxml::FromXml;
4use error::Error;
5
6/// A representation of the `<enclosure>` element.
7#[derive(Debug, Default, Clone, PartialEq)]
8pub struct Enclosure {
9    /// The URL of the enclosure.
10    pub url: String,
11    /// The length of the enclosure in bytes.
12    pub length: String,
13    /// The MIME type of the enclosure.
14    pub mime_type: String,
15}
16
17impl FromXml for Enclosure {
18    fn from_xml<R: ::std::io::BufRead>(mut reader: XmlReader<R>,
19                                       element: Element)
20                                       -> Result<(Self, XmlReader<R>), Error> {
21        let mut url = None;
22        let mut length = None;
23        let mut mime_type = None;
24
25        for attr in element.attributes().with_checks(false).unescaped() {
26            if let Ok(attr) = attr {
27                match attr.0 {
28                    b"url" if url.is_none() => {
29                        url = Some(try!(String::from_utf8(attr.1.into_owned())));
30                    }
31                    b"length" if length.is_none() => {
32                        length = Some(try!(String::from_utf8(attr.1.into_owned())));
33                    }
34                    b"type" if mime_type.is_none() => {
35                        mime_type = Some(try!(String::from_utf8(attr.1.into_owned())));
36                    }
37                    _ => {}
38                }
39            }
40        }
41
42        skip_element!(reader);
43
44        let url = url.unwrap_or_default();
45        let length = length.unwrap_or_default();
46        let mime_type = mime_type.unwrap_or_default();
47
48        Ok((Enclosure {
49            url: url,
50            length: length,
51            mime_type: mime_type,
52        }, reader))
53    }
54}