atom_syndication/person.rs
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
use std::borrow::Cow;
use std::io::{BufRead, Write};
use quick_xml::events::attributes::Attributes;
use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::Reader;
use quick_xml::Writer;
use crate::error::{Error, XmlError};
use crate::fromxml::FromXml;
use crate::toxml::{ToXmlNamed, WriterExt};
use crate::util::{atom_text, decode, skip};
/// Represents a person in an Atom feed
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, Default, Clone, PartialEq)]
#[cfg_attr(feature = "builders", derive(Builder))]
#[cfg_attr(
feature = "builders",
builder(
setter(into),
default,
build_fn(name = "build_impl", private, error = "never::Never")
)
)]
pub struct Person {
/// A human-readable name for the person.
pub name: String,
/// An email address for the person.
pub email: Option<String>,
/// A Web page for the person.
pub 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_deref()
}
/// 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_deref()
}
/// 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_into(&mut buf).map_err(XmlError::new)? {
Event::Start(element) => match decode(element.name().as_ref(), reader)? {
Cow::Borrowed("name") => person.name = atom_text(reader)?.unwrap_or_default(),
Cow::Borrowed("email") => person.email = atom_text(reader)?,
Cow::Borrowed("uri") => person.uri = atom_text(reader)?,
_ => skip(element.name(), reader)?,
},
Event::End(_) => break,
Event::Eof => return Err(Error::Eof),
_ => {}
}
buf.clear();
}
Ok(person)
}
}
impl ToXmlNamed for Person {
fn to_xml_named<W>(&self, writer: &mut Writer<W>, name: &str) -> Result<(), XmlError>
where
W: Write,
{
writer
.write_event(Event::Start(BytesStart::new(name)))
.map_err(XmlError::new)?;
writer.write_text_element("name", &self.name)?;
if let Some(ref email) = self.email {
writer.write_text_element("email", email)?;
}
if let Some(ref uri) = self.uri {
writer.write_text_element("uri", uri)?;
}
writer
.write_event(Event::End(BytesEnd::new(name)))
.map_err(XmlError::new)?;
Ok(())
}
}
#[cfg(feature = "builders")]
impl PersonBuilder {
/// Builds a new `Person`.
pub fn build(&self) -> Person {
self.build_impl().unwrap()
}
}