Feed: Add Builder pattern.

This commit is contained in:
Jordan Petridis 2018-01-20 08:46:05 +02:00
parent b3c4de320b
commit ef52a026bc
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
4 changed files with 37 additions and 14 deletions

View File

@ -51,7 +51,12 @@ fn index_urls() -> Vec<Box<Future<Item = (), Error = Error>>> {
let s = Source::from_url(url).unwrap(); let s = Source::from_url(url).unwrap();
// parse it into a channel // parse it into a channel
let chan = rss::Channel::read_from(BufReader::new(buff)).unwrap(); let chan = rss::Channel::read_from(BufReader::new(buff)).unwrap();
Feed::from_channel_source(chan, s.id())
FeedBuilder::default()
.channel(chan)
.source_id(s.id())
.build()
.unwrap()
}) })
.collect(); .collect();
@ -115,7 +120,11 @@ fn bench_index_large_feed(c: &mut Criterion) {
let s = Source::from_url(url).unwrap(); let s = Source::from_url(url).unwrap();
// parse it into a channel // parse it into a channel
let chan = rss::Channel::read_from(BufReader::new(CODE)).unwrap(); let chan = rss::Channel::read_from(BufReader::new(CODE)).unwrap();
let feed = Feed::from_channel_source(chan, s.id()); let feed = FeedBuilder::default()
.channel(chan)
.source_id(s.id())
.build()
.unwrap();
let _foo = core.run(feed.index()).unwrap(); let _foo = core.run(feed.index()).unwrap();
}) })
}); });
@ -132,7 +141,11 @@ fn bench_index_small_feed(c: &mut Criterion) {
let s = Source::from_url(url).unwrap(); let s = Source::from_url(url).unwrap();
// parse it into a channel // parse it into a channel
let chan = rss::Channel::read_from(BufReader::new(STARS)).unwrap(); let chan = rss::Channel::read_from(BufReader::new(STARS)).unwrap();
let feed = Feed::from_channel_source(chan, s.id()); let feed = FeedBuilder::default()
.channel(chan)
.source_id(s.id())
.build()
.unwrap();
let _foo = core.run(feed.index()).unwrap(); let _foo = core.run(feed.index()).unwrap();
}) })
}); });

View File

@ -12,20 +12,19 @@ use pipeline::*;
type InsertUpdate = (Vec<NewEpisode>, Vec<Option<(NewEpisode, i32)>>); type InsertUpdate = (Vec<NewEpisode>, Vec<Option<(NewEpisode, i32)>>);
#[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.
#[derive(Debug, Clone, Builder)]
#[builder(derive(Debug))]
#[builder(setter(into))]
pub struct Feed { pub struct Feed {
/// The `rss::Channel` parsed from the `Source` uri.
channel: rss::Channel, channel: rss::Channel,
/// The `Source` id where the xml `rss::Channel` came from.
source_id: i32, source_id: i32,
} }
impl Feed { impl Feed {
/// Constructor that consumes a `rss::Channel`, returns a `Feed` struct.
pub fn from_channel_source(channel: rss::Channel, source_id: i32) -> Feed {
Feed { channel, source_id }
}
/// Index the contents of the RSS `Feed` into the database. /// Index the contents of the RSS `Feed` into the database.
pub fn index(self) -> Box<Future<Item = (), Error = Error>> { pub fn index(self) -> Box<Future<Item = (), Error = Error>> {
let fut = self.parse_podcast_async() let fut = self.parse_podcast_async()
@ -102,10 +101,11 @@ mod tests {
use tokio_core::reactor::Core; use tokio_core::reactor::Core;
use Source; use Source;
use database::truncate_db;
use dbqueries; use dbqueries;
use pipeline; use pipeline;
use database::truncate_db;
use std::fs; use std::fs;
use std::io::BufReader; use std::io::BufReader;
@ -167,7 +167,11 @@ mod tests {
let feed = fs::File::open(path).unwrap(); let feed = fs::File::open(path).unwrap();
// parse it into a channel // parse it into a channel
let chan = rss::Channel::read_from(BufReader::new(feed)).unwrap(); let chan = rss::Channel::read_from(BufReader::new(feed)).unwrap();
Feed::from_channel_source(chan, s.id()) FeedBuilder::default()
.channel(chan)
.source_id(s.id())
.build()
.unwrap()
}) })
.collect(); .collect();

View File

@ -64,7 +64,7 @@ mod feed;
mod parser; mod parser;
mod schema; mod schema;
pub use feed::Feed; pub use feed::{Feed, FeedBuilder};
pub use models::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source}; pub use models::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source};
/// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths. /// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths.

View File

@ -12,7 +12,7 @@ use futures::prelude::*;
use database::connection; use database::connection;
use errors::*; use errors::*;
use feed::Feed; use feed::{Feed, FeedBuilder};
use models::NewSource; use models::NewSource;
use schema::source; use schema::source;
@ -125,7 +125,13 @@ impl Source {
Ok(res) Ok(res)
}) })
.and_then(response_to_channel) .and_then(response_to_channel)
.map(move |chan| Feed::from_channel_source(chan, id)); .map(move |chan| {
FeedBuilder::default()
.channel(chan)
.source_id(id)
.build()
.unwrap()
});
Box::new(feed) Box::new(feed)
} }