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

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

use error::Error;
use fromxml::FromXml;
use toxml::{ToXmlNamed, WriterExt};
use util::atom_text;

/// Represents a person in an Atom feed
#[derive(Debug, Default, Clone, PartialEq, Builder)]
#[builder(setter(into), default)]
pub struct Person {
    /// A human-readable name for the person.
    name: String,
    /// An email address for the person.
    email: Option<String>,
    /// A Web page for the person.
    uri: Option<String>,
}

impl Person {
    /// Return the name of this person.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Person;
    ///
    /// let mut person = Person::default();
    /// person.set_name("John Doe");
    /// assert_eq!(person.name(), "John Doe");
    /// ```
    pub fn name(&self) -> &str {
        self.name.as_str()
    }

    /// Return the name of this person.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Person;
    ///
    /// let mut person = Person::default();
    /// person.set_name("John Doe");
    /// ```
    pub fn set_name<V>(&mut self, name: V)
    where
        V: Into<String>,
    {
        self.name = name.into()
    }

    /// Return the email address for this person.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Person;
    ///
    /// let mut person = Person::default();
    /// person.set_email("johndoe@example.com".to_string());
    /// assert_eq!(person.email(), Some("johndoe@example.com"));
    /// ```
    pub fn email(&self) -> Option<&str> {
        self.email.as_ref().map(|s| s.as_str())
    }

    /// Set the email address for this person.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Person;
    ///
    /// let mut person = Person::default();
    /// person.set_email("johndoe@example.com".to_string());
    /// ```
    pub fn set_email<V>(&mut self, email: V)
    where
        V: Into<Option<String>>,
    {
        self.email = email.into()
    }

    /// Return the Web page for this person.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Person;
    ///
    /// let mut person = Person::default();
    /// person.set_uri("http://example.com".to_string());
    /// assert_eq!(person.uri(), Some("http://example.com"));
    /// ```
    pub fn uri(&self) -> Option<&str> {
        self.uri.as_ref().map(|s| s.as_str())
    }

    /// Set the Web page for this person.
    ///
    /// # Examples
    ///
    /// ```
    /// use atom_syndication::Person;
    ///
    /// let mut person = Person::default();
    /// person.set_uri("http://example.com".to_string());
    /// ```
    pub fn set_uri<V>(&mut self, uri: V)
    where
        V: Into<Option<String>>,
    {
        self.uri = uri.into()
    }
}

impl FromXml for Person {
    fn from_xml<B: BufRead>(reader: &mut Reader<B>, _: Attributes) -> Result<Self, Error> {
        let mut person = Person::default();
        let mut buf = Vec::new();

        loop {
            match reader.read_event(&mut buf)? {
                Event::Start(element) => {
                    match element.name() {
                        b"name" => person.name = atom_text(reader)?.unwrap_or_default(),
                        b"email" => person.email = atom_text(reader)?,
                        b"uri" => person.uri = atom_text(reader)?,
                        n => reader.read_to_end(n, &mut Vec::new())?,
                    }
                }
                Event::End(_) => break,
                Event::Eof => return Err(Error::Eof),
                _ => {}
            }

            buf.clear();
        }

        Ok(person)
    }
}

impl ToXmlNamed for Person {
    fn to_xml_named<W, N>(&self, writer: &mut Writer<W>, name: N) -> Result<(), XmlError>
    where
        W: Write,
        N: AsRef<[u8]>,
    {
        let name = name.as_ref();
        writer
            .write_event(Event::Start(BytesStart::borrowed(name, name.len())))?;
        writer.write_text_element(b"name", &*self.name)?;

        if let Some(ref email) = self.email {
            writer.write_text_element(b"email", &*email)?;
        }

        if let Some(ref uri) = self.uri {
            writer.write_text_element(b"uri", &*uri)?;
        }

        writer.write_event(Event::End(BytesEnd::borrowed(name)))?;

        Ok(())
    }
}