Skip to content

Enables the parsing of elements defined in another namespace in Person constructs #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use quick_xml::Reader;
use quick_xml::Writer;

use crate::error::{Error, XmlError};
use crate::extension::util::{extension_name, parse_extension};
use crate::extension::ExtensionMap;
use crate::fromxml::FromXml;
use crate::toxml::{ToXmlNamed, WriterExt};
use crate::util::{atom_text, decode, skip};
Expand All @@ -30,6 +32,8 @@ pub struct Person {
pub email: Option<String>,
/// A Web page for the person.
pub uri: Option<String>,
/// A list of extensions for the person.
pub extensions: ExtensionMap,
}

impl Person {
Expand Down Expand Up @@ -128,6 +132,35 @@ impl Person {
{
self.uri = uri.into()
}

/// Return the extensions for this person.
///
/// # Examples
///
/// ```
/// use atom_syndication::{Person, extension::ExtensionMap};
///
/// let mut person = Person::default();
/// person.set_extensions(ExtensionMap::new());
/// assert_eq!(person.extensions().len(), 0);
/// ```
pub fn extensions(&self) -> &ExtensionMap {
&self.extensions
}

/// Set the extensions for this person.
///
/// # Examples
///
/// ```
/// use atom_syndication::{Person, extension::ExtensionMap};
///
/// let mut person = Person::default();
/// person.set_extensions(ExtensionMap::new());
/// ```
pub fn set_extensions(&mut self, extensions: ExtensionMap) {
self.extensions = extensions;
}
}

impl FromXml for Person {
Expand All @@ -141,7 +174,19 @@ impl FromXml for Person {
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)?,
n => {
if let Some((ns, name)) = extension_name(n.as_ref()) {
parse_extension(
reader,
element.attributes(),
ns,
name,
&mut person.extensions,
)?;
} else {
skip(element.name(), reader)?;
}
}
},
Event::End(_) => break,
Event::Eof => return Err(Error::Eof),
Expand Down Expand Up @@ -173,6 +218,12 @@ impl ToXmlNamed for Person {
writer.write_text_element("uri", uri)?;
}

for map in self.extensions.values() {
for extensions in map.values() {
writer.write_objects(extensions)?;
}
}

writer
.write_event(Event::End(BytesEnd::new(name)))
.map_err(XmlError::new)?;
Expand Down
1 change: 1 addition & 0 deletions tests/data/person.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<name>John Doe</name>
<email>[email protected]</email>
<uri>http://example.com</uri>
<ext:name exattr="exvalue" xmlns:ext="http://www.example.com">Example Name</ext:name>
</author>
</feed>
11 changes: 11 additions & 0 deletions tests/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ fn read_person() {
assert_eq!(person.name(), "John Doe");
assert_eq!(person.email(), Some("[email protected]"));
assert_eq!(person.uri(), Some("http://example.com"));

// Person extensions
assert!(person.extensions().contains_key("ext"));
let map = person.extensions().get("ext").unwrap();
assert!(map.contains_key("name"));
let name = map.get("name").unwrap().first().unwrap();
assert_eq!(name.value(), Some("Example Name"));
assert_eq!(
name.attrs().get("exattr").map(String::as_str),
Some("exvalue")
);
}

#[test]
Expand Down