Skip to content

Commit

Permalink
reformat all code
Browse files Browse the repository at this point in the history
  • Loading branch information
AnderEnder committed Jun 5, 2020
1 parent 3557c96 commit 72dbe42
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 69 deletions.
9 changes: 6 additions & 3 deletions src/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

use std::io::{BufRead, Write};

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

Expand Down Expand Up @@ -97,7 +97,10 @@ impl Category {

impl Category {
/// Builds a Category from source XML
pub fn from_xml<R: BufRead>(reader: &mut Reader<R>, mut atts: Attributes) -> Result<Self, Error> {
pub fn from_xml<R: BufRead>(
reader: &mut Reader<R>,
mut atts: Attributes,
) -> Result<Self, Error> {
let mut category = Category::default();

for attr in atts.with_checks(false) {
Expand Down
36 changes: 21 additions & 15 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ use std::collections::HashMap;
use std::io::{BufRead, Write};
use std::str::{self, FromStr};

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

use crate::category::Category;
use crate::cloud::Cloud;
use crate::error::Error;
use crate::extension::ExtensionMap;
use crate::extension::dublincore;
use crate::extension::itunes;
use crate::extension::syndication;
use crate::extension::util::{extension_name, parse_extension};
use crate::extension::ExtensionMap;
use crate::image::Image;
use crate::item::Item;
use crate::textinput::TextInput;
Expand Down Expand Up @@ -990,7 +990,8 @@ impl Channel {
match reader.read_event(&mut buf)? {
Event::Start(element) => match element.name() {
b"channel" => {
let inner = Channel::from_xml(&namespaces, &mut reader, element.attributes())?;
let inner =
Channel::from_xml(&namespaces, &mut reader, element.attributes())?;
channel = Some(inner);
}
b"item" => {
Expand Down Expand Up @@ -1101,7 +1102,11 @@ impl ToString for Channel {

impl Channel {
/// Builds a Channel from source XML
pub fn from_xml<R: BufRead>(namespaces: &HashMap<String, String>, reader: &mut Reader<R>, _: Attributes) -> Result<Self, Error> {
pub fn from_xml<R: BufRead>(
namespaces: &HashMap<String, String>,
reader: &mut Reader<R>,
_: Attributes,
) -> Result<Self, Error> {
let mut channel = Channel::default();
let mut buf = Vec::new();
let mut skip_buf = Vec::new();
Expand Down Expand Up @@ -1216,16 +1221,17 @@ impl Channel {
// Process each of the namespaces we know (note that the values are not removed prior and reused to support pass-through of unknown extensions)
for (prefix, namespace) in namespaces {
match namespace.as_ref() {
itunes::NAMESPACE => {
channel.extensions.remove(prefix).map(|v| channel.itunes_ext = Some(itunes::ITunesChannelExtension::from_map(v)))
},
dublincore::NAMESPACE => {
channel.extensions.remove(prefix).map(|v| channel.dublin_core_ext = Some(dublincore::DublinCoreExtension::from_map(v)))
},
syndication::NAMESPACE => {
channel.extensions.remove(prefix).map(|v| channel.syndication_ext = Some(syndication::SyndicationExtension::from_map(v)))
},
_ => None
itunes::NAMESPACE => channel.extensions.remove(prefix).map(|v| {
channel.itunes_ext = Some(itunes::ITunesChannelExtension::from_map(v))
}),
dublincore::NAMESPACE => channel.extensions.remove(prefix).map(|v| {
channel.dublin_core_ext = Some(dublincore::DublinCoreExtension::from_map(v))
}),
syndication::NAMESPACE => channel.extensions.remove(prefix).map(|v| {
channel.syndication_ext =
Some(syndication::SyndicationExtension::from_map(v))
}),
_ => None,
};
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

use std::io::{BufRead, Write};

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

Expand Down Expand Up @@ -198,7 +198,10 @@ impl Cloud {

impl Cloud {
/// Builds a Cloud from source XML
pub fn from_xml<R: BufRead>(reader: &mut Reader<R>, mut atts: Attributes) -> Result<Self, Error> {
pub fn from_xml<R: BufRead>(
reader: &mut Reader<R>,
mut atts: Attributes,
) -> Result<Self, Error> {
let mut cloud = Cloud::default();

for attr in atts.with_checks(false) {
Expand Down
9 changes: 6 additions & 3 deletions src/enclosure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

use std::io::{BufRead, Write};

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

Expand Down Expand Up @@ -130,7 +130,10 @@ impl Enclosure {

impl Enclosure {
/// Builds an Enclosure from source XML
pub fn from_xml<R: BufRead>(reader: &mut Reader<R>, mut atts: Attributes) -> Result<Self, Error> {
pub fn from_xml<R: BufRead>(
reader: &mut Reader<R>,
mut atts: Attributes,
) -> Result<Self, Error> {
let mut enclosure = Enclosure::default();

for attr in atts.with_checks(false) {
Expand Down
2 changes: 1 addition & 1 deletion src/extension/dublincore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::io::Write;
use quick_xml::Error as XmlError;
use quick_xml::Writer;

use crate::extension::Extension;
use crate::extension::util::remove_extension_values;
use crate::extension::Extension;

use crate::toxml::{ToXml, WriterExt};

Expand Down
2 changes: 1 addition & 1 deletion src/extension/itunes/itunes_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::Error as XmlError;
use quick_xml::Writer;

use std::ops::Deref;
use crate::toxml::ToXml;
use std::ops::Deref;

/// A category for an iTunes podcast.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
4 changes: 2 additions & 2 deletions src/extension/itunes/itunes_channel_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use std::collections::HashMap;
use std::io::Write;

use quick_xml::Error as XmlError;
use quick_xml::events::{BytesStart, Event};
use quick_xml::Error as XmlError;
use quick_xml::Writer;

use super::{parse_categories, parse_image, parse_owner};
use crate::extension::Extension;
use crate::extension::itunes::{ITunesCategory, ITunesOwner};
use crate::extension::util::remove_extension_value;
use crate::extension::Extension;
use crate::toxml::{ToXml, WriterExt};

/// An iTunes channel element extension.
Expand Down
4 changes: 2 additions & 2 deletions src/extension/itunes/itunes_item_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use std::collections::HashMap;
use std::io::Write;

use quick_xml::Error as XmlError;
use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::Error as XmlError;
use quick_xml::Writer;

use super::parse_image;
use crate::extension::Extension;
use crate::extension::util::remove_extension_value;
use crate::extension::Extension;
use crate::toxml::{ToXml, WriterExt};

/// An iTunes item element extension.
Expand Down
2 changes: 1 addition & 1 deletion src/extension/itunes/itunes_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

use std::io::Write;

use quick_xml::Error as XmlError;
use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::Error as XmlError;
use quick_xml::Writer;

use crate::toxml::{ToXml, WriterExt};
Expand Down
2 changes: 1 addition & 1 deletion src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::collections::HashMap;
use std::io::Write;
use std::str;

use quick_xml::Error as XmlError;
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Error as XmlError;
use quick_xml::Writer;

use crate::toxml::ToXml;
Expand Down
36 changes: 28 additions & 8 deletions src/extension/syndication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl FromStr for UpdatePeriod {
"weekly" => Ok(UpdatePeriod::WEEKLY),
"monthly" => Ok(UpdatePeriod::MONTHLY),
"yearly" => Ok(UpdatePeriod::YEARLY),
_ => Err(())
_ => Err(()),
}
}
}
Expand Down Expand Up @@ -108,11 +108,21 @@ impl SyndicationExtension {
}

/// Serialises this extension to the nominated writer
pub fn to_xml<W: Write>(&self, namespaces: &HashMap<String, String>, writer: &mut Writer<W>) -> Result<(), XmlError> {
pub fn to_xml<W: Write>(
&self,
namespaces: &HashMap<String, String>,
writer: &mut Writer<W>,
) -> Result<(), XmlError> {
for (prefix, namespace) in namespaces {
if NAMESPACE == namespace {
writer.write_text_element(format!("{}:updatePeriod", prefix), &self.period.to_string())?;
writer.write_text_element(format!("{}:updateFrequency", prefix), &format!("{}", self.frequency))?;
writer.write_text_element(
format!("{}:updatePeriod", prefix),
&self.period.to_string(),
)?;
writer.write_text_element(
format!("{}:updateFrequency", prefix),
&format!("{}", self.frequency),
)?;
writer.write_text_element(format!("{}:updateBase", prefix), &self.base)?;
}
}
Expand All @@ -122,13 +132,19 @@ impl SyndicationExtension {

impl Default for SyndicationExtension {
fn default() -> Self {
SyndicationExtension { period: UpdatePeriod::DAILY, frequency: 1, base: String::from("1970-01-01T00:00+00:00") }
SyndicationExtension {
period: UpdatePeriod::DAILY,
frequency: 1,
base: String::from("1970-01-01T00:00+00:00"),
}
}
}

/// Retrieves the extensions for the nominated field and runs the callback if there is at least 1 extension value
fn with_first_ext_value<'a, F>(map: &'a HashMap<String, Vec<Extension>>, field: &str, f: F)
where F: FnOnce(&'a str) {
where
F: FnOnce(&'a str),
{
if let Some(extensions) = map.get(field) {
if !extensions.is_empty() {
if let Some(v) = extensions[0].value.as_ref() {
Expand All @@ -143,8 +159,12 @@ impl SyndicationExtension {
pub fn from_map(map: HashMap<String, Vec<Extension>>) -> Self {
let mut syn = SyndicationExtension::default();

with_first_ext_value(&map, "updatePeriod", |value| syn.period = value.parse().unwrap());
with_first_ext_value(&map, "updateFrequency", |value| syn.frequency = value.parse().unwrap());
with_first_ext_value(&map, "updatePeriod", |value| {
syn.period = value.parse().unwrap()
});
with_first_ext_value(&map, "updateFrequency", |value| {
syn.frequency = value.parse().unwrap()
});
with_first_ext_value(&map, "updateBase", |value| syn.base = value.to_owned());

syn
Expand Down
9 changes: 6 additions & 3 deletions src/guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

use std::io::{BufRead, Write};

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

Expand Down Expand Up @@ -107,7 +107,10 @@ impl Default for Guid {

impl Guid {
/// Builds a Guid from source XML
pub fn from_xml<R: BufRead>(reader: &mut Reader<R>, mut atts: Attributes) -> Result<Self, Error> {
pub fn from_xml<R: BufRead>(
reader: &mut Reader<R>,
mut atts: Attributes,
) -> Result<Self, Error> {
let mut guid = Guid::default();

for attr in atts.with_checks(false) {
Expand Down
4 changes: 2 additions & 2 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

use std::io::{BufRead, Write};

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

Expand Down
27 changes: 16 additions & 11 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

use std::io::{BufRead, Write};

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

use crate::category::Category;
use crate::enclosure::Enclosure;
use crate::error::Error;
use crate::extension::ExtensionMap;
use crate::extension::dublincore;
use crate::extension::itunes;
use crate::extension::util::{extension_name, parse_extension};
use crate::extension::ExtensionMap;
use crate::guid::Guid;
use crate::source::Source;
use crate::toxml::{ToXml, WriterExt};
Expand Down Expand Up @@ -539,7 +539,11 @@ impl Item {

impl Item {
/// Builds an Item from source XML
pub fn from_xml<R: BufRead>(namespaces: &HashMap<String, String>, reader: &mut Reader<R>, _: Attributes) -> Result<Self, Error> {
pub fn from_xml<R: BufRead>(
namespaces: &HashMap<String, String>,
reader: &mut Reader<R>,
_: Attributes,
) -> Result<Self, Error> {
let mut item = Item::default();
let mut buf = Vec::new();

Expand Down Expand Up @@ -594,13 +598,14 @@ impl Item {
// Process each of the namespaces we know (note that the values are not removed prior and reused to support pass-through of unknown extensions)
for (prefix, namespace) in namespaces {
match namespace.as_ref() {
itunes::NAMESPACE => {
item.extensions.remove(prefix).map(|v| item.itunes_ext = Some(itunes::ITunesItemExtension::from_map(v)))
},
dublincore::NAMESPACE => {
item.extensions.remove(prefix).map(|v| item.dublin_core_ext = Some(dublincore::DublinCoreExtension::from_map(v)))
},
_ => None
itunes::NAMESPACE => item
.extensions
.remove(prefix)
.map(|v| item.itunes_ext = Some(itunes::ITunesItemExtension::from_map(v))),
dublincore::NAMESPACE => item.extensions.remove(prefix).map(|v| {
item.dublin_core_ext = Some(dublincore::DublinCoreExtension::from_map(v))
}),
_ => None,
};
}
}
Expand Down

0 comments on commit 72dbe42

Please sign in to comment.