From e39a89d63d4f2d55c8074d4049f2e6659fd06b81 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 13 Oct 2017 02:49:14 +0300 Subject: [PATCH] Replaced some map/fold with for_each now that it hit stable! --- hammond-data/src/index_feed.rs | 34 ++++++++++++---------------- hammond-downloader/src/downloader.rs | 16 ++++++------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/hammond-data/src/index_feed.rs b/hammond-data/src/index_feed.rs index 0eb1e36..de632dd 100644 --- a/hammond-data/src/index_feed.rs +++ b/hammond-data/src/index_feed.rs @@ -262,11 +262,9 @@ mod tests { "http://feeds.feedburner.com/linuxunplugged", ]; - inpt.iter() - .map(|feed| { - index_source(&db, &NewSource::new_with_uri(feed)).unwrap() - }) - .fold((), |(), _| ()); + inpt.iter().for_each(|feed| { + index_source(&db, &NewSource::new_with_uri(feed)).unwrap() + }); index_loop(db, true).unwrap(); @@ -303,22 +301,20 @@ mod tests { ), ]; - urls.iter() - .map(|&(path, url)| { - let tempdb = m.lock().unwrap(); - // Create and insert a Source into db - let s = insert_return_source(&tempdb, url).unwrap(); - drop(tempdb); + urls.iter().for_each(|&(path, url)| { + let tempdb = m.lock().unwrap(); + // Create and insert a Source into db + let s = insert_return_source(&tempdb, url).unwrap(); + drop(tempdb); - // open the xml file - let feed = fs::File::open(path).unwrap(); - // parse it into a channel - let chan = rss::Channel::read_from(BufReader::new(feed)).unwrap(); + // open the xml file + let feed = fs::File::open(path).unwrap(); + // parse it into a channel + let chan = rss::Channel::read_from(BufReader::new(feed)).unwrap(); - // Index the channel - complete_index(m.clone(), &chan, &s).unwrap(); - }) - .fold((), |(), _| ()); + // Index the channel + complete_index(m.clone(), &chan, &s).unwrap(); + }); // Assert the index rows equal the controlled results let tempdb = m.lock().unwrap(); diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index b311b4a..0f9f030 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -55,12 +55,12 @@ pub fn download_to(target: &str, url: &str) -> Result<()> { } // Initial messy prototype, queries load alot of not needed stuff. +// TODO: Refactor pub fn latest_dl(connection: &SqliteConnection, limit: u32) -> Result<()> { let pds = dbqueries::get_podcasts(connection)?; - pds.iter() - // TODO when for_each reaches stable: - // Remove all the ugly folds(_) and replace map() with for_each(). + let _: Vec<_> = pds.iter() + // This could be for_each instead of map. .map(|x| -> Result<()> { let mut eps = if limit == 0 { dbqueries::get_pd_episodes(connection, x)? @@ -75,17 +75,15 @@ pub fn latest_dl(connection: &SqliteConnection, limit: u32) -> Result<()> { DirBuilder::new().recursive(true).create(&dl_fold).unwrap(); // Download the episodes - eps.iter_mut() + let _ :Vec<_>= eps.iter_mut() .map(|y| -> Result<()> { // Check if its alrdy downloaded if y.local_uri().is_some() { - // Not idiomatic but I am still fighting the borrow-checker. - if Path::new(y.local_uri().unwrap().to_owned().as_str()).exists() { + if Path::new(y.local_uri().unwrap()).exists() { return Ok(()); } y.set_local_uri(None); y.save_changes::(connection)?; - () }; // Unreliable and hacky way to extract the file extension from the url. @@ -101,11 +99,11 @@ pub fn latest_dl(connection: &SqliteConnection, limit: u32) -> Result<()> { y.save_changes::(connection)?; Ok(()) }) - .fold((), |(), _| ()); + .collect(); Ok(()) }) - .fold((), |(), _| ()); + .collect(); Ok(()) }