From e6b0cfccb519f1539ef58d06de91ac31d2bfd5ca Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 19 Jan 2018 10:32:25 +0200 Subject: [PATCH] Apply clippy suggestions. --- hammond-data/src/database.rs | 2 +- hammond-data/src/feed.rs | 33 ++++++++++++++------------ hammond-data/src/models/new_episode.rs | 26 ++++++++++---------- hammond-data/src/models/new_podcast.rs | 25 ++++++++++--------- hammond-data/src/models/source.rs | 28 ++++++++++------------ hammond-data/src/pipeline.rs | 14 +++++------ hammond-data/src/utils.rs | 2 +- hammond-downloader/src/downloader.rs | 2 +- hammond-gtk/src/manager.rs | 2 +- hammond-gtk/src/utils.rs | 2 +- 10 files changed, 67 insertions(+), 69 deletions(-) diff --git a/hammond-data/src/database.rs b/hammond-data/src/database.rs index 7cd3e6e..581a2a8 100644 --- a/hammond-data/src/database.rs +++ b/hammond-data/src/database.rs @@ -37,7 +37,7 @@ lazy_static! { static ref DB_PATH: PathBuf = TEMPDIR.path().join("hammond.db"); } -/// Get an r2d2 SqliteConnection. +/// Get an r2d2 `SqliteConnection`. pub(crate) fn connection() -> Pool { POOL.clone() } diff --git a/hammond-data/src/feed.rs b/hammond-data/src/feed.rs index 44ef022..bd401a9 100644 --- a/hammond-data/src/feed.rs +++ b/hammond-data/src/feed.rs @@ -11,6 +11,8 @@ use models::{IndexState, Update}; use models::{NewEpisode, NewPodcast, Podcast}; use pipeline::*; +type InsertUpdate = (Vec, Vec<(NewEpisode, i32)>); + #[derive(Debug)] /// Wrapper struct that hold a `Source` id and the `rss::Channel` /// that corresponds to the `Source.uri` field. @@ -53,8 +55,8 @@ impl Feed { let (insert, update): (Vec<_>, Vec<_>) = items .into_iter() .filter_map(|item| glue(item, pd.id()).ok()) - .filter(|state| match state { - &IndexState::NotChanged => false, + .filter(|state| match *state { + IndexState::NotChanged => false, _ => true, }) .partition_map(|state| match state { @@ -79,27 +81,28 @@ impl Feed { fn index_channel_items_async(&self, pd: &Podcast) -> Box> { let fut = self.get_stuff(pd) .and_then(|(insert, update)| { - info!("Indexing {} episodes.", insert.len()); - dbqueries::index_new_episodes(insert.as_slice())?; + if !insert.is_empty() { + info!("Indexing {} episodes.", insert.len()); + dbqueries::index_new_episodes(insert.as_slice())?; + } Ok((insert, update)) }) .map(|(_, update)| { - info!("Updating {} episodes.", update.len()); - update.iter().for_each(|&(ref ep, rowid)| { - if let Err(err) = ep.update(rowid) { - error!("Failed to index episode: {:?}.", ep.title()); - error!("Error msg: {}", err); - }; - }) + if !update.is_empty() { + info!("Updating {} episodes.", update.len()); + update.iter().for_each(|&(ref ep, rowid)| { + if let Err(err) = ep.update(rowid) { + error!("Failed to index episode: {:?}.", ep.title()); + error!("Error msg: {}", err); + }; + }) + } }); Box::new(fut) } - fn get_stuff( - &self, - pd: &Podcast, - ) -> Box, Vec<(NewEpisode, i32)>), Error = Error>> { + fn get_stuff(&self, pd: &Podcast) -> Box> { let (insert, update): (Vec<_>, Vec<_>) = self.channel .items() .into_iter() diff --git a/hammond-data/src/models/new_episode.rs b/hammond-data/src/models/new_episode.rs index cae96db..e1a46ab 100644 --- a/hammond-data/src/models/new_episode.rs +++ b/hammond-data/src/models/new_episode.rs @@ -79,22 +79,20 @@ impl Index for NewEpisode { fn index(&self) -> Result<()> { let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?; - match exists { - false => self.insert(), - true => { - let old = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?; + if exists { + let old = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?; - // This is messy - if (self.title() != old.title()) || (self.uri() != old.uri()) - || (self.duration() != old.duration()) - || (self.epoch() != old.epoch()) - || (self.guid() != old.guid()) - { - self.update(old.rowid()) - } else { - Ok(()) - } + // This is messy + if (self.title() != old.title()) || (self.uri() != old.uri()) + || (self.duration() != old.duration()) + || (self.epoch() != old.epoch()) || (self.guid() != old.guid()) + { + self.update(old.rowid()) + } else { + Ok(()) } + } else { + self.insert() } } } diff --git a/hammond-data/src/models/new_podcast.rs b/hammond-data/src/models/new_podcast.rs index c44402d..1c9eb01 100644 --- a/hammond-data/src/models/new_podcast.rs +++ b/hammond-data/src/models/new_podcast.rs @@ -61,21 +61,20 @@ impl Index for NewPodcast { fn index(&self) -> Result<()> { let exists = dbqueries::podcast_exists(self.source_id)?; - match exists { - false => self.insert(), - true => { - let old = dbqueries::get_podcast_from_source_id(self.source_id)?; + if exists { + let old = dbqueries::get_podcast_from_source_id(self.source_id)?; - // This is messy - if (self.link() != old.link()) || (self.title() != old.title()) - || (self.image_uri() != old.image_uri()) - || (self.description() != old.description()) - { - self.update(old.id()) - } else { - Ok(()) - } + // This is messy + if (self.link() != old.link()) || (self.title() != old.title()) + || (self.image_uri() != old.image_uri()) + || (self.description() != old.description()) + { + self.update(old.id()) + } else { + Ok(()) } + } else { + self.insert() } } } diff --git a/hammond-data/src/models/source.rs b/hammond-data/src/models/source.rs index 2d58193..8af9681 100644 --- a/hammond-data/src/models/source.rs +++ b/hammond-data/src/models/source.rs @@ -71,9 +71,9 @@ impl Source { /// Helper method to easily save/"sync" current state of self to the Database. pub fn save(&self) -> Result { let db = connection(); - let tempdb = db.get()?; + let con = db.get()?; - Ok(self.save_changes::(&*tempdb)?) + Ok(self.save_changes::(&*con)?) } /// Extract Etag and LastModifier from res, and update self and the @@ -81,14 +81,12 @@ impl Source { fn update_etag(&mut self, res: &Response) -> Result<()> { let headers = res.headers(); - let etag = headers.get::(); - let lmod = headers.get::(); + let etag = headers.get::().map(|x| x.tag()); + let lmod = headers.get::().map(|x| format!("{}", x)); - if self.http_etag() != etag.map(|x| x.tag()) || self.last_modified != lmod.map(|x| { - format!("{}", x) - }) { - self.set_http_etag(etag.map(|x| x.tag())); - self.set_last_modified(lmod.map(|x| format!("{}", x))); + if (self.http_etag() != etag) || (self.last_modified != lmod) { + self.set_http_etag(etag); + self.set_last_modified(lmod); self.save()?; } @@ -110,7 +108,7 @@ impl Source { /// /// Consumes `self` and Returns the corresponding `Feed` Object. // TODO: Refactor into TryInto once it lands on stable. - pub fn to_feed( + pub fn into_feed( mut self, client: &Client>, ignore_etags: bool, @@ -118,7 +116,7 @@ impl Source { let id = self.id(); let feed = self.request_constructor(client, ignore_etags) .map_err(From::from) - .and_then(move |res| { + .and_then(move |res| -> Result { self.update_etag(&res)?; Ok(res) }) @@ -126,7 +124,7 @@ impl Source { match_status(res.status())?; Ok(res) }) - .and_then(|res| response_to_channel(res)) + .and_then(response_to_channel) .map(move |chan| Feed::from_channel_source(chan, id)); Box::new(feed) @@ -170,9 +168,9 @@ fn response_to_channel(res: Response) -> Box>(); let buf = String::from_utf8_lossy(&utf_8_bytes).into_owned(); - let chan = Channel::from_str(&buf).map_err(From::from); - chan + Channel::from_str(&buf).map_err(From::from) }); + Box::new(chan) } @@ -223,7 +221,7 @@ mod tests { let url = "http://www.newrustacean.com/feed.xml"; let source = Source::from_url(url).unwrap(); - let feed = source.to_feed(&client, true); + let feed = source.into_feed(&client, true); assert!(core.run(feed).is_ok()); } diff --git a/hammond-data/src/pipeline.rs b/hammond-data/src/pipeline.rs index a2d1685..311ff7c 100644 --- a/hammond-data/src/pipeline.rs +++ b/hammond-data/src/pipeline.rs @@ -24,7 +24,7 @@ use std; /// /// Messy temp diagram: /// Source -> GET Request -> Update Etags -> Check Status -> Parse xml/Rss -> -/// Convert rss::Channel into Feed -> Index Podcast -> Index Episodes. +/// Convert `rss::Channel` into Feed -> Index Podcast -> Index Episodes. pub fn pipeline>(sources: S, ignore_etags: bool) -> Result<()> { let mut core = Core::new()?; let handle = core.handle(); @@ -35,7 +35,7 @@ pub fn pipeline>(sources: S, ignore_etags: bool) let list = sources .into_iter() - .map(|s| s.to_feed(&client, ignore_etags)) + .map(|s| s.into_feed(&client, ignore_etags)) .map(|fut| fut.and_then(|feed| feed.index_async())) .collect(); @@ -53,15 +53,15 @@ fn determine_ep_state(ep: NewEpisodeMinimal, item: &rss::Item) -> Result Result Result> { let e = NewEpisodeMinimal::new(item, id)?; - determine_ep_state(e, &item) + determine_ep_state(e, item) } #[allow(dead_code)] @@ -100,7 +100,7 @@ where Err((r, _, rest)) => (Err(r), rest), }; done.push(r); - if rest.len() == 0 { + if rest.is_empty() { Ok(Loop::Break(done)) } else { Ok(Loop::Continue((rest, done))) diff --git a/hammond-data/src/utils.rs b/hammond-data/src/utils.rs index c75f1a2..d48dc73 100644 --- a/hammond-data/src/utils.rs +++ b/hammond-data/src/utils.rs @@ -138,7 +138,7 @@ pub fn get_download_folder(pd_title: &str) -> Result { /// and deletes all of the downloaded content. /// TODO: Write Tests pub fn delete_show(pd: &Podcast) -> Result<()> { - dbqueries::remove_feed(&pd)?; + dbqueries::remove_feed(pd)?; info!("{} was removed succesfully.", pd.title()); let fold = get_download_folder(pd.title())?; diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 95586d4..821fbce 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -239,7 +239,7 @@ mod tests { // Copy it's id let sid = source.id(); // Convert Source it into a future Feed and index it - let future = source.to_feed(&client, true); + let future = source.into_feed(&client, true); let feed = core.run(future).unwrap(); feed.index().unwrap(); diff --git a/hammond-gtk/src/manager.rs b/hammond-gtk/src/manager.rs index 1916383..476edd7 100644 --- a/hammond-gtk/src/manager.rs +++ b/hammond-gtk/src/manager.rs @@ -147,7 +147,7 @@ mod tests { // Copy it's id let sid = source.id(); // Convert Source it into a future Feed and index it - let future = source.to_feed(&client, true); + let future = source.into_feed(&client, true); let feed = core.run(future).unwrap(); feed.index().unwrap(); diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 7ff58ad..5f11ce7 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -104,7 +104,7 @@ mod tests { // Copy it's id let sid = source.id(); // Convert Source it into a future Feed and index it - let future = source.to_feed(&client, true); + let future = source.into_feed(&client, true); let feed = core.run(future).unwrap(); feed.index().unwrap();