From 6bf65f2d92c652afcddc5f3f1f9a26b1256bec0c Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sun, 1 Oct 2017 10:13:45 +0300 Subject: [PATCH] Split complete_index_source into higher order functions. --- src/index_feed.rs | 27 ++++++++++++++++++++++----- src/lib.rs | 3 ++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/index_feed.rs b/src/index_feed.rs index 34a2739..ee7365e 100644 --- a/src/index_feed.rs +++ b/src/index_feed.rs @@ -97,14 +97,30 @@ fn complete_index_from_source( let mut buf = String::new(); req.read_to_string(&mut buf)?; let chan = rss::Channel::from_str(&buf)?; - let pd = feedparser::parse_podcast(&chan, source.id())?; let tempdb = mutex.lock().unwrap(); - let pd = insert_return_podcast(&tempdb, &pd)?; + let pd = index_channel(&tempdb, &chan, &source)?; drop(tempdb); - let foo: Vec<_> = chan.items() - .par_iter() + index_channel_items(mutex.clone(), chan.items(), &pd)?; + + Ok(()) +} + +fn index_channel(db: &SqliteConnection, chan: &rss::Channel, parent: &Source) -> Result { + let pd = feedparser::parse_podcast(&chan, parent.id())?; + // Convert NewPodcast to Podcast + let pd = insert_return_podcast(db, &pd)?; + Ok(pd) +} + +// TODO: Propagate the erros from the maps up the chain. +fn index_channel_items( + mutex: Arc>, + i: &[rss::Item], + pd: &Podcast, +) -> Result<()> { + let foo: Vec<_> = i.par_iter() .map(|x| feedparser::parse_episode(&x, pd.id()).unwrap()) .collect(); @@ -113,7 +129,6 @@ fn complete_index_from_source( let db = dbmutex.lock().unwrap(); index_episode(&db, &x).unwrap(); }); - Ok(()) } @@ -195,6 +210,8 @@ mod tests { /// Create and return a Temporary DB. /// Will be destroed once the returned variable(s) is dropped. + // TODO: make it an Iterator so it will give a unique db_path each time. + // And will also be able to run tests in parallel. fn get_temp_db() -> TempDB { let tmp_dir = tempdir::TempDir::new("hammond_unit_test").unwrap(); let db_path = tmp_dir.path().join("foo_tests.db"); diff --git a/src/lib.rs b/src/lib.rs index 70139d3..8370f60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,6 @@ pub mod dbqueries; pub mod errors { use reqwest; - use std::io; use rss; use chrono; use hyper; @@ -47,6 +46,8 @@ pub mod errors { use diesel::migrations::RunMigrationsError; use diesel::result; use regex; + + use std::io; // use std::sync; error_chain! {