Split complete_index_source into higher order functions.
This commit is contained in:
parent
f70f707a69
commit
6bf65f2d92
@ -97,14 +97,30 @@ fn complete_index_from_source(
|
|||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
req.read_to_string(&mut buf)?;
|
req.read_to_string(&mut buf)?;
|
||||||
let chan = rss::Channel::from_str(&buf)?;
|
let chan = rss::Channel::from_str(&buf)?;
|
||||||
let pd = feedparser::parse_podcast(&chan, source.id())?;
|
|
||||||
|
|
||||||
let tempdb = mutex.lock().unwrap();
|
let tempdb = mutex.lock().unwrap();
|
||||||
let pd = insert_return_podcast(&tempdb, &pd)?;
|
let pd = index_channel(&tempdb, &chan, &source)?;
|
||||||
drop(tempdb);
|
drop(tempdb);
|
||||||
|
|
||||||
let foo: Vec<_> = chan.items()
|
index_channel_items(mutex.clone(), chan.items(), &pd)?;
|
||||||
.par_iter()
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn index_channel(db: &SqliteConnection, chan: &rss::Channel, parent: &Source) -> Result<Podcast> {
|
||||||
|
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<Mutex<SqliteConnection>>,
|
||||||
|
i: &[rss::Item],
|
||||||
|
pd: &Podcast,
|
||||||
|
) -> Result<()> {
|
||||||
|
let foo: Vec<_> = i.par_iter()
|
||||||
.map(|x| feedparser::parse_episode(&x, pd.id()).unwrap())
|
.map(|x| feedparser::parse_episode(&x, pd.id()).unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -113,7 +129,6 @@ fn complete_index_from_source(
|
|||||||
let db = dbmutex.lock().unwrap();
|
let db = dbmutex.lock().unwrap();
|
||||||
index_episode(&db, &x).unwrap();
|
index_episode(&db, &x).unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,6 +210,8 @@ mod tests {
|
|||||||
|
|
||||||
/// Create and return a Temporary DB.
|
/// Create and return a Temporary DB.
|
||||||
/// Will be destroed once the returned variable(s) is dropped.
|
/// 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 {
|
fn get_temp_db() -> TempDB {
|
||||||
let tmp_dir = tempdir::TempDir::new("hammond_unit_test").unwrap();
|
let tmp_dir = tempdir::TempDir::new("hammond_unit_test").unwrap();
|
||||||
let db_path = tmp_dir.path().join("foo_tests.db");
|
let db_path = tmp_dir.path().join("foo_tests.db");
|
||||||
|
|||||||
@ -39,7 +39,6 @@ pub mod dbqueries;
|
|||||||
pub mod errors {
|
pub mod errors {
|
||||||
|
|
||||||
use reqwest;
|
use reqwest;
|
||||||
use std::io;
|
|
||||||
use rss;
|
use rss;
|
||||||
use chrono;
|
use chrono;
|
||||||
use hyper;
|
use hyper;
|
||||||
@ -47,6 +46,8 @@ pub mod errors {
|
|||||||
use diesel::migrations::RunMigrationsError;
|
use diesel::migrations::RunMigrationsError;
|
||||||
use diesel::result;
|
use diesel::result;
|
||||||
use regex;
|
use regex;
|
||||||
|
|
||||||
|
use std::io;
|
||||||
// use std::sync;
|
// use std::sync;
|
||||||
|
|
||||||
error_chain! {
|
error_chain! {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user