-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
294 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// This file is part of rss. | ||
// | ||
// Copyright © 2015-2020 The rust-syndication Developers | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the MIT License and/or Apache 2.0 License. | ||
|
||
use std::collections::HashMap; | ||
use std::io::Write; | ||
|
||
pub use atom_syndication::Link; | ||
use quick_xml::events::{BytesStart, Event}; | ||
use quick_xml::Error as XmlError; | ||
use quick_xml::Writer; | ||
|
||
use crate::extension::Extension; | ||
use crate::toxml::ToXml; | ||
|
||
/// The Atom XML namespace. | ||
pub const NAMESPACE: &str = "http://www.w3.org/2005/Atom"; | ||
|
||
/// An Atom element extension. | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[derive(Default, Debug, Clone, PartialEq)] | ||
#[cfg_attr(feature = "builders", derive(Builder))] | ||
#[cfg_attr(feature = "builders", builder(setter(into), default))] | ||
pub struct AtomExtension { | ||
/// Links | ||
pub links: Vec<Link>, | ||
} | ||
|
||
impl AtomExtension { | ||
/// Retrieve links | ||
pub fn links(&self) -> &[Link] { | ||
&self.links | ||
} | ||
|
||
/// Set links | ||
pub fn set_links<V>(&mut self, links: V) | ||
where | ||
V: Into<Vec<Link>>, | ||
{ | ||
self.links = links.into(); | ||
} | ||
} | ||
|
||
impl AtomExtension { | ||
/// Creates an `AtomExtension` using the specified `HashMap`. | ||
pub fn from_map(mut map: HashMap<String, Vec<Extension>>) -> Self { | ||
let mut ext = Self::default(); | ||
|
||
ext.links = map | ||
.remove("link") | ||
.unwrap_or_default() | ||
.into_iter() | ||
.filter_map(|mut link_ext| { | ||
let href = link_ext.attrs.remove("href")?; | ||
|
||
let mut link = Link::default(); | ||
link.href = href; | ||
if let Some(rel) = link_ext.attrs.remove("rel") { | ||
link.rel = rel; | ||
} | ||
link.hreflang = link_ext.attrs.remove("hreflang"); | ||
link.mime_type = link_ext.attrs.remove("type"); | ||
link.title = link_ext.attrs.remove("title"); | ||
link.length = link_ext.attrs.remove("length"); | ||
Some(link) | ||
}) | ||
.collect(); | ||
|
||
ext | ||
} | ||
} | ||
|
||
impl ToXml for AtomExtension { | ||
fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError> { | ||
for link in &self.links { | ||
let name = b"link"; | ||
let mut element = BytesStart::borrowed(name, name.len()); | ||
element.push_attribute(("href", &*link.href)); | ||
element.push_attribute(("rel", &*link.rel)); | ||
|
||
if let Some(ref hreflang) = link.hreflang { | ||
element.push_attribute(("hreflang", &**hreflang)); | ||
} | ||
|
||
if let Some(ref mime_type) = link.mime_type { | ||
element.push_attribute(("type", &**mime_type)); | ||
} | ||
|
||
if let Some(ref title) = link.title { | ||
element.push_attribute(("title", &**title)); | ||
} | ||
|
||
if let Some(ref length) = link.length { | ||
element.push_attribute(("length", &**length)); | ||
} | ||
|
||
writer.write_event(Event::Empty(element))?; | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0"?> | ||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> | ||
<channel> | ||
<title>Liftoff News</title> | ||
<link>http://liftoff.msfc.nasa.gov/</link> | ||
<description>Liftoff to Space Exploration.</description> | ||
<language>en-us</language> | ||
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate> | ||
<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate> | ||
<docs>http://blogs.law.harvard.edu/tech/rss</docs> | ||
<generator>Weblog Editor 2.0</generator> | ||
<managingEditor>editor@example.com</managingEditor> | ||
<webMaster>webmaster@example.com</webMaster> | ||
<atom:link href="http://liftoff.msfc.nasa.gov/rss" rel="self" type="application/rss+xml" /> | ||
<item> | ||
<title>Star City</title> | ||
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link> | ||
<atom:link href="http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp" /> | ||
<description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>.</description> | ||
<pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate> | ||
<guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid> | ||
<atom:link href="http://liftoff.msfc.nasa.gov/2003/05/30.html#item572" rel="related" /> | ||
</item> | ||
<item> | ||
<description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm">partial eclipse of the Sun</a> on Saturday, May 31st.</description> | ||
<pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate> | ||
<guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid> | ||
</item> | ||
<item> | ||
<title>The Engine That Does More</title> | ||
<link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link> | ||
<atom:link href="http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp" /> | ||
<description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description> | ||
<pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate> | ||
<guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid> | ||
</item> | ||
</channel> | ||
</rss> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters