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
use std::io::{BufRead, Write};

use quick_xml::errors::Error as XmlError;
use quick_xml::events::{Event, BytesStart};
use quick_xml::events::attributes::Attributes;
use quick_xml::reader::Reader;
use quick_xml::writer::Writer;

use error::Error;
use fromxml::FromXml;
use toxml::ToXml;

/// Represents a category in an Atom feed
#[derive(Debug, Default, Clone, PartialEq, Builder)]
#[builder(setter(into), default)]
pub struct Category {
    /// Identifies the category.
    term: String,
    /// Identifies the categorization scheme via a URI.
    scheme: Option<String>,
    /// A human-readable label for display.
    label: Option<String>,
}

impl Category {
    /// Return the term that identifies this category.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Category;
    ///
    /// let mut category = Category::default();
    /// category.set_term("technology");
    /// assert_eq!(category.term(), "technology");
    /// ```
    pub fn term(&self) -> &str {
        self.term.as_str()
    }

    /// Set the term that identifies this category.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Category;
    ///
    /// let mut category = Category::default();
    /// category.set_term("technology");
    /// ```
    pub fn set_term<V>(&mut self, term: V)
    where
        V: Into<String>,
    {
        self.term = term.into();
    }

    /// Return the categorization scheme URI.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Category;
    ///
    /// let mut category = Category::default();
    /// category.set_scheme("http://example.com/scheme".to_string());
    /// assert_eq!(category.scheme(), Some("http://example.com/scheme"));
    /// ```
    pub fn scheme(&self) -> Option<&str> {
        self.scheme.as_ref().map(|s| s.as_str())
    }

    /// Set the categorization scheme URI.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Category;
    ///
    /// let mut category = Category::default();
    /// category.set_scheme("http://example.com/scheme".to_string());
    /// ```
    pub fn set_scheme<V>(&mut self, scheme: V)
    where
        V: Into<Option<String>>,
    {
        self.scheme = scheme.into();
    }

    /// Return the label for this category.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Category;
    ///
    /// let mut category = Category::default();
    /// category.set_scheme("Technology".to_string());
    /// assert_eq!(category.scheme(), Some("Technology"));
    /// ```

    pub fn label(&self) -> Option<&str> {
        self.label.as_ref().map(|s| s.as_str())
    }

    /// Set the label for this category.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Category;
    ///
    /// let mut category = Category::default();
    /// category.set_scheme("Technology".to_string());
    /// ```
    pub fn set_label<V>(&mut self, label: V)
    where
        V: Into<Option<String>>,
    {
        self.label = label.into();
    }
}

impl FromXml for Category {
    fn from_xml<B: BufRead>(reader: &mut Reader<B>, mut atts: Attributes) -> Result<Self, Error> {
        let mut category = Category::default();

        for attr in atts.with_checks(false) {
            if let Ok(att) = attr {
                match att.key {
                    b"term" => category.term = att.unescape_and_decode_value(reader)?,
                    b"scheme" => category.scheme = Some(att.unescape_and_decode_value(reader)?),
                    b"label" => category.label = Some(att.unescape_and_decode_value(reader)?),
                    _ => {}
                }
            }
        }

        reader.read_to_end(b"category", &mut Vec::new())?;

        Ok(category)
    }
}

impl ToXml for Category {
    fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError> {
        let name = b"category";
        let mut element = BytesStart::borrowed(name, name.len());
        element.push_attribute(("term", &*self.term));

        if let Some(ref scheme) = self.scheme {
            element.push_attribute(("scheme", &**scheme));
        }

        if let Some(ref label) = self.label {
            element.push_attribute(("label", &**label));
        }

        writer.write_event(Event::Empty(element))?;

        Ok(())
    }
}