Apply clippy suggestions.
This commit is contained in:
parent
5c5faafc72
commit
e6b0cfccb5
@ -37,7 +37,7 @@ lazy_static! {
|
|||||||
static ref DB_PATH: PathBuf = TEMPDIR.path().join("hammond.db");
|
static ref DB_PATH: PathBuf = TEMPDIR.path().join("hammond.db");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get an r2d2 SqliteConnection.
|
/// Get an r2d2 `SqliteConnection`.
|
||||||
pub(crate) fn connection() -> Pool {
|
pub(crate) fn connection() -> Pool {
|
||||||
POOL.clone()
|
POOL.clone()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,8 @@ use models::{IndexState, Update};
|
|||||||
use models::{NewEpisode, NewPodcast, Podcast};
|
use models::{NewEpisode, NewPodcast, Podcast};
|
||||||
use pipeline::*;
|
use pipeline::*;
|
||||||
|
|
||||||
|
type InsertUpdate = (Vec<NewEpisode>, Vec<(NewEpisode, i32)>);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
/// Wrapper struct that hold a `Source` id and the `rss::Channel`
|
/// Wrapper struct that hold a `Source` id and the `rss::Channel`
|
||||||
/// that corresponds to the `Source.uri` field.
|
/// that corresponds to the `Source.uri` field.
|
||||||
@ -53,8 +55,8 @@ impl Feed {
|
|||||||
let (insert, update): (Vec<_>, Vec<_>) = items
|
let (insert, update): (Vec<_>, Vec<_>) = items
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|item| glue(item, pd.id()).ok())
|
.filter_map(|item| glue(item, pd.id()).ok())
|
||||||
.filter(|state| match state {
|
.filter(|state| match *state {
|
||||||
&IndexState::NotChanged => false,
|
IndexState::NotChanged => false,
|
||||||
_ => true,
|
_ => true,
|
||||||
})
|
})
|
||||||
.partition_map(|state| match state {
|
.partition_map(|state| match state {
|
||||||
@ -79,27 +81,28 @@ impl Feed {
|
|||||||
fn index_channel_items_async(&self, pd: &Podcast) -> Box<Future<Item = (), Error = Error>> {
|
fn index_channel_items_async(&self, pd: &Podcast) -> Box<Future<Item = (), Error = Error>> {
|
||||||
let fut = self.get_stuff(pd)
|
let fut = self.get_stuff(pd)
|
||||||
.and_then(|(insert, update)| {
|
.and_then(|(insert, update)| {
|
||||||
info!("Indexing {} episodes.", insert.len());
|
if !insert.is_empty() {
|
||||||
dbqueries::index_new_episodes(insert.as_slice())?;
|
info!("Indexing {} episodes.", insert.len());
|
||||||
|
dbqueries::index_new_episodes(insert.as_slice())?;
|
||||||
|
}
|
||||||
Ok((insert, update))
|
Ok((insert, update))
|
||||||
})
|
})
|
||||||
.map(|(_, update)| {
|
.map(|(_, update)| {
|
||||||
info!("Updating {} episodes.", update.len());
|
if !update.is_empty() {
|
||||||
update.iter().for_each(|&(ref ep, rowid)| {
|
info!("Updating {} episodes.", update.len());
|
||||||
if let Err(err) = ep.update(rowid) {
|
update.iter().for_each(|&(ref ep, rowid)| {
|
||||||
error!("Failed to index episode: {:?}.", ep.title());
|
if let Err(err) = ep.update(rowid) {
|
||||||
error!("Error msg: {}", err);
|
error!("Failed to index episode: {:?}.", ep.title());
|
||||||
};
|
error!("Error msg: {}", err);
|
||||||
})
|
};
|
||||||
|
})
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Box::new(fut)
|
Box::new(fut)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_stuff(
|
fn get_stuff(&self, pd: &Podcast) -> Box<Future<Item = InsertUpdate, Error = Error>> {
|
||||||
&self,
|
|
||||||
pd: &Podcast,
|
|
||||||
) -> Box<Future<Item = (Vec<NewEpisode>, Vec<(NewEpisode, i32)>), Error = Error>> {
|
|
||||||
let (insert, update): (Vec<_>, Vec<_>) = self.channel
|
let (insert, update): (Vec<_>, Vec<_>) = self.channel
|
||||||
.items()
|
.items()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|||||||
@ -79,22 +79,20 @@ impl Index for NewEpisode {
|
|||||||
fn index(&self) -> Result<()> {
|
fn index(&self) -> Result<()> {
|
||||||
let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?;
|
let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?;
|
||||||
|
|
||||||
match exists {
|
if exists {
|
||||||
false => self.insert(),
|
let old = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?;
|
||||||
true => {
|
|
||||||
let old = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?;
|
|
||||||
|
|
||||||
// This is messy
|
// This is messy
|
||||||
if (self.title() != old.title()) || (self.uri() != old.uri())
|
if (self.title() != old.title()) || (self.uri() != old.uri())
|
||||||
|| (self.duration() != old.duration())
|
|| (self.duration() != old.duration())
|
||||||
|| (self.epoch() != old.epoch())
|
|| (self.epoch() != old.epoch()) || (self.guid() != old.guid())
|
||||||
|| (self.guid() != old.guid())
|
{
|
||||||
{
|
self.update(old.rowid())
|
||||||
self.update(old.rowid())
|
} else {
|
||||||
} else {
|
Ok(())
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
self.insert()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -61,21 +61,20 @@ impl Index for NewPodcast {
|
|||||||
fn index(&self) -> Result<()> {
|
fn index(&self) -> Result<()> {
|
||||||
let exists = dbqueries::podcast_exists(self.source_id)?;
|
let exists = dbqueries::podcast_exists(self.source_id)?;
|
||||||
|
|
||||||
match exists {
|
if exists {
|
||||||
false => self.insert(),
|
let old = dbqueries::get_podcast_from_source_id(self.source_id)?;
|
||||||
true => {
|
|
||||||
let old = dbqueries::get_podcast_from_source_id(self.source_id)?;
|
|
||||||
|
|
||||||
// This is messy
|
// This is messy
|
||||||
if (self.link() != old.link()) || (self.title() != old.title())
|
if (self.link() != old.link()) || (self.title() != old.title())
|
||||||
|| (self.image_uri() != old.image_uri())
|
|| (self.image_uri() != old.image_uri())
|
||||||
|| (self.description() != old.description())
|
|| (self.description() != old.description())
|
||||||
{
|
{
|
||||||
self.update(old.id())
|
self.update(old.id())
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
self.insert()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,9 +71,9 @@ impl Source {
|
|||||||
/// Helper method to easily save/"sync" current state of self to the Database.
|
/// Helper method to easily save/"sync" current state of self to the Database.
|
||||||
pub fn save(&self) -> Result<Source> {
|
pub fn save(&self) -> Result<Source> {
|
||||||
let db = connection();
|
let db = connection();
|
||||||
let tempdb = db.get()?;
|
let con = db.get()?;
|
||||||
|
|
||||||
Ok(self.save_changes::<Source>(&*tempdb)?)
|
Ok(self.save_changes::<Source>(&*con)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extract Etag and LastModifier from res, and update self and the
|
/// 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<()> {
|
fn update_etag(&mut self, res: &Response) -> Result<()> {
|
||||||
let headers = res.headers();
|
let headers = res.headers();
|
||||||
|
|
||||||
let etag = headers.get::<ETag>();
|
let etag = headers.get::<ETag>().map(|x| x.tag());
|
||||||
let lmod = headers.get::<LastModified>();
|
let lmod = headers.get::<LastModified>().map(|x| format!("{}", x));
|
||||||
|
|
||||||
if self.http_etag() != etag.map(|x| x.tag()) || self.last_modified != lmod.map(|x| {
|
if (self.http_etag() != etag) || (self.last_modified != lmod) {
|
||||||
format!("{}", x)
|
self.set_http_etag(etag);
|
||||||
}) {
|
self.set_last_modified(lmod);
|
||||||
self.set_http_etag(etag.map(|x| x.tag()));
|
|
||||||
self.set_last_modified(lmod.map(|x| format!("{}", x)));
|
|
||||||
self.save()?;
|
self.save()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +108,7 @@ impl Source {
|
|||||||
///
|
///
|
||||||
/// Consumes `self` and Returns the corresponding `Feed` Object.
|
/// Consumes `self` and Returns the corresponding `Feed` Object.
|
||||||
// TODO: Refactor into TryInto once it lands on stable.
|
// TODO: Refactor into TryInto once it lands on stable.
|
||||||
pub fn to_feed(
|
pub fn into_feed(
|
||||||
mut self,
|
mut self,
|
||||||
client: &Client<HttpsConnector<HttpConnector>>,
|
client: &Client<HttpsConnector<HttpConnector>>,
|
||||||
ignore_etags: bool,
|
ignore_etags: bool,
|
||||||
@ -118,7 +116,7 @@ impl Source {
|
|||||||
let id = self.id();
|
let id = self.id();
|
||||||
let feed = self.request_constructor(client, ignore_etags)
|
let feed = self.request_constructor(client, ignore_etags)
|
||||||
.map_err(From::from)
|
.map_err(From::from)
|
||||||
.and_then(move |res| {
|
.and_then(move |res| -> Result<Response> {
|
||||||
self.update_etag(&res)?;
|
self.update_etag(&res)?;
|
||||||
Ok(res)
|
Ok(res)
|
||||||
})
|
})
|
||||||
@ -126,7 +124,7 @@ impl Source {
|
|||||||
match_status(res.status())?;
|
match_status(res.status())?;
|
||||||
Ok(res)
|
Ok(res)
|
||||||
})
|
})
|
||||||
.and_then(|res| response_to_channel(res))
|
.and_then(response_to_channel)
|
||||||
.map(move |chan| Feed::from_channel_source(chan, id));
|
.map(move |chan| Feed::from_channel_source(chan, id));
|
||||||
|
|
||||||
Box::new(feed)
|
Box::new(feed)
|
||||||
@ -170,9 +168,9 @@ fn response_to_channel(res: Response) -> Box<Future<Item = Channel, Error = Erro
|
|||||||
.and_then(|iter| {
|
.and_then(|iter| {
|
||||||
let utf_8_bytes = iter.collect::<Vec<u8>>();
|
let utf_8_bytes = iter.collect::<Vec<u8>>();
|
||||||
let buf = String::from_utf8_lossy(&utf_8_bytes).into_owned();
|
let buf = String::from_utf8_lossy(&utf_8_bytes).into_owned();
|
||||||
let chan = Channel::from_str(&buf).map_err(From::from);
|
Channel::from_str(&buf).map_err(From::from)
|
||||||
chan
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Box::new(chan)
|
Box::new(chan)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,7 +221,7 @@ mod tests {
|
|||||||
let url = "http://www.newrustacean.com/feed.xml";
|
let url = "http://www.newrustacean.com/feed.xml";
|
||||||
let source = Source::from_url(url).unwrap();
|
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());
|
assert!(core.run(feed).is_ok());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,7 +24,7 @@ use std;
|
|||||||
///
|
///
|
||||||
/// Messy temp diagram:
|
/// Messy temp diagram:
|
||||||
/// Source -> GET Request -> Update Etags -> Check Status -> Parse xml/Rss ->
|
/// 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<S: IntoIterator<Item = Source>>(sources: S, ignore_etags: bool) -> Result<()> {
|
pub fn pipeline<S: IntoIterator<Item = Source>>(sources: S, ignore_etags: bool) -> Result<()> {
|
||||||
let mut core = Core::new()?;
|
let mut core = Core::new()?;
|
||||||
let handle = core.handle();
|
let handle = core.handle();
|
||||||
@ -35,7 +35,7 @@ pub fn pipeline<S: IntoIterator<Item = Source>>(sources: S, ignore_etags: bool)
|
|||||||
|
|
||||||
let list = sources
|
let list = sources
|
||||||
.into_iter()
|
.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()))
|
.map(|fut| fut.and_then(|feed| feed.index_async()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -53,15 +53,15 @@ fn determine_ep_state(ep: NewEpisodeMinimal, item: &rss::Item) -> Result<IndexSt
|
|||||||
let exists = dbqueries::episode_exists(ep.title(), ep.podcast_id())?;
|
let exists = dbqueries::episode_exists(ep.title(), ep.podcast_id())?;
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return Ok(IndexState::Index(ep.into_new_episode(item)));
|
Ok(IndexState::Index(ep.into_new_episode(item)))
|
||||||
} else {
|
} else {
|
||||||
let old = dbqueries::get_episode_minimal_from_pk(ep.title(), ep.podcast_id())?;
|
let old = dbqueries::get_episode_minimal_from_pk(ep.title(), ep.podcast_id())?;
|
||||||
let rowid = old.rowid();
|
let rowid = old.rowid();
|
||||||
|
|
||||||
if ep != old.into() {
|
if ep != old.into() {
|
||||||
return Ok(IndexState::Update((ep.into_new_episode(item), rowid)));
|
Ok(IndexState::Update((ep.into_new_episode(item), rowid)))
|
||||||
} else {
|
} else {
|
||||||
return Ok(IndexState::NotChanged);
|
Ok(IndexState::NotChanged)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ fn determine_ep_state(ep: NewEpisodeMinimal, item: &rss::Item) -> Result<IndexSt
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub(crate) fn glue(item: &rss::Item, id: i32) -> Result<IndexState<NewEpisode>> {
|
pub(crate) fn glue(item: &rss::Item, id: i32) -> Result<IndexState<NewEpisode>> {
|
||||||
let e = NewEpisodeMinimal::new(item, id)?;
|
let e = NewEpisodeMinimal::new(item, id)?;
|
||||||
determine_ep_state(e, &item)
|
determine_ep_state(e, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -100,7 +100,7 @@ where
|
|||||||
Err((r, _, rest)) => (Err(r), rest),
|
Err((r, _, rest)) => (Err(r), rest),
|
||||||
};
|
};
|
||||||
done.push(r);
|
done.push(r);
|
||||||
if rest.len() == 0 {
|
if rest.is_empty() {
|
||||||
Ok(Loop::Break(done))
|
Ok(Loop::Break(done))
|
||||||
} else {
|
} else {
|
||||||
Ok(Loop::Continue((rest, done)))
|
Ok(Loop::Continue((rest, done)))
|
||||||
|
|||||||
@ -138,7 +138,7 @@ pub fn get_download_folder(pd_title: &str) -> Result<String> {
|
|||||||
/// and deletes all of the downloaded content.
|
/// and deletes all of the downloaded content.
|
||||||
/// TODO: Write Tests
|
/// TODO: Write Tests
|
||||||
pub fn delete_show(pd: &Podcast) -> Result<()> {
|
pub fn delete_show(pd: &Podcast) -> Result<()> {
|
||||||
dbqueries::remove_feed(&pd)?;
|
dbqueries::remove_feed(pd)?;
|
||||||
info!("{} was removed succesfully.", pd.title());
|
info!("{} was removed succesfully.", pd.title());
|
||||||
|
|
||||||
let fold = get_download_folder(pd.title())?;
|
let fold = get_download_folder(pd.title())?;
|
||||||
|
|||||||
@ -239,7 +239,7 @@ mod tests {
|
|||||||
// Copy it's id
|
// Copy it's id
|
||||||
let sid = source.id();
|
let sid = source.id();
|
||||||
// Convert Source it into a future Feed and index it
|
// 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();
|
let feed = core.run(future).unwrap();
|
||||||
feed.index().unwrap();
|
feed.index().unwrap();
|
||||||
|
|
||||||
|
|||||||
@ -147,7 +147,7 @@ mod tests {
|
|||||||
// Copy it's id
|
// Copy it's id
|
||||||
let sid = source.id();
|
let sid = source.id();
|
||||||
// Convert Source it into a future Feed and index it
|
// 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();
|
let feed = core.run(future).unwrap();
|
||||||
feed.index().unwrap();
|
feed.index().unwrap();
|
||||||
|
|
||||||
|
|||||||
@ -104,7 +104,7 @@ mod tests {
|
|||||||
// Copy it's id
|
// Copy it's id
|
||||||
let sid = source.id();
|
let sid = source.id();
|
||||||
// Convert Source it into a future Feed and index it
|
// 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();
|
let feed = core.run(future).unwrap();
|
||||||
feed.index().unwrap();
|
feed.index().unwrap();
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user