Remove custom builders in favor of derive_builder crate.
This commit is contained in:
parent
35f7464540
commit
8e6cc4d2f2
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -549,6 +549,7 @@ name = "hammond-data"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"derive_builder 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"diesel 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"diesel 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"diesel_migrations 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"diesel_migrations 0.99.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"dotenv 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"dotenv 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|||||||
@ -18,6 +18,7 @@ rfc822_sanitizer = "0.3.3"
|
|||||||
rss = "1.2.1"
|
rss = "1.2.1"
|
||||||
url = "1.6.0"
|
url = "1.6.0"
|
||||||
xdg = "2.1.0"
|
xdg = "2.1.0"
|
||||||
|
derive_builder = "0.5.0"
|
||||||
|
|
||||||
[dependencies.diesel]
|
[dependencies.diesel]
|
||||||
features = ["sqlite"]
|
features = ["sqlite"]
|
||||||
|
|||||||
@ -34,6 +34,9 @@ extern crate diesel;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel_migrations;
|
extern crate diesel_migrations;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate derive_builder;
|
||||||
|
|
||||||
extern crate chrono;
|
extern crate chrono;
|
||||||
extern crate r2d2;
|
extern crate r2d2;
|
||||||
extern crate r2d2_diesel;
|
extern crate r2d2_diesel;
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
#![allow(unused_mut)]
|
||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
|
||||||
use schema::{episode, podcast, source};
|
use schema::{episode, podcast, source};
|
||||||
@ -20,7 +22,10 @@ trait Update {
|
|||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name = "source"]
|
#[table_name = "source"]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Default, Builder)]
|
||||||
|
#[builder(default)]
|
||||||
|
#[builder(derive(Debug))]
|
||||||
|
#[builder(setter(into))]
|
||||||
pub(crate) struct NewSource {
|
pub(crate) struct NewSource {
|
||||||
uri: String,
|
uri: String,
|
||||||
last_modified: Option<String>,
|
last_modified: Option<String>,
|
||||||
@ -63,15 +68,15 @@ impl NewSource {
|
|||||||
|
|
||||||
#[derive(Insertable, AsChangeset)]
|
#[derive(Insertable, AsChangeset)]
|
||||||
#[table_name = "podcast"]
|
#[table_name = "podcast"]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Default, Builder)]
|
||||||
|
#[builder(default)]
|
||||||
|
#[builder(derive(Debug))]
|
||||||
|
#[builder(setter(into))]
|
||||||
pub(crate) struct NewPodcast {
|
pub(crate) struct NewPodcast {
|
||||||
title: String,
|
title: String,
|
||||||
link: String,
|
link: String,
|
||||||
description: String,
|
description: String,
|
||||||
image_uri: Option<String>,
|
image_uri: Option<String>,
|
||||||
favorite: bool,
|
|
||||||
archive: bool,
|
|
||||||
always_dl: bool,
|
|
||||||
source_id: i32,
|
source_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,78 +129,6 @@ impl NewPodcast {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub(crate) struct NewPodcastBuilder {
|
|
||||||
title: String,
|
|
||||||
link: String,
|
|
||||||
description: String,
|
|
||||||
image_uri: Option<String>,
|
|
||||||
favorite: bool,
|
|
||||||
archive: bool,
|
|
||||||
always_dl: bool,
|
|
||||||
source_id: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
impl NewPodcastBuilder {
|
|
||||||
pub(crate) fn new() -> NewPodcastBuilder {
|
|
||||||
NewPodcastBuilder::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn title(mut self, s: String) -> NewPodcastBuilder {
|
|
||||||
self.title = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn link(mut self, s: String) -> NewPodcastBuilder {
|
|
||||||
self.link = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn description(mut self, s: String) -> NewPodcastBuilder {
|
|
||||||
self.description = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn image_uri(mut self, s: Option<String>) -> NewPodcastBuilder {
|
|
||||||
self.image_uri = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn source_id(mut self, s: i32) -> NewPodcastBuilder {
|
|
||||||
self.source_id = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn favorite(mut self, s: bool) -> NewPodcastBuilder {
|
|
||||||
self.favorite = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn archive(mut self, s: bool) -> NewPodcastBuilder {
|
|
||||||
self.archive = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn always_dl(mut self, s: bool) -> NewPodcastBuilder {
|
|
||||||
self.always_dl = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn build(self) -> NewPodcast {
|
|
||||||
NewPodcast {
|
|
||||||
title: self.title,
|
|
||||||
link: self.link,
|
|
||||||
description: self.description,
|
|
||||||
image_uri: self.image_uri,
|
|
||||||
favorite: self.favorite,
|
|
||||||
archive: self.archive,
|
|
||||||
always_dl: self.always_dl,
|
|
||||||
source_id: self.source_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
// Ignore the following geters. They are used in unit tests mainly.
|
// Ignore the following geters. They are used in unit tests mainly.
|
||||||
impl NewPodcast {
|
impl NewPodcast {
|
||||||
@ -222,19 +155,18 @@ impl NewPodcast {
|
|||||||
|
|
||||||
#[derive(Insertable, AsChangeset)]
|
#[derive(Insertable, AsChangeset)]
|
||||||
#[table_name = "episode"]
|
#[table_name = "episode"]
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default, Builder)]
|
||||||
|
#[builder(default)]
|
||||||
|
#[builder(derive(Debug))]
|
||||||
|
#[builder(setter(into))]
|
||||||
pub(crate) struct NewEpisode {
|
pub(crate) struct NewEpisode {
|
||||||
title: Option<String>,
|
title: Option<String>,
|
||||||
uri: String,
|
uri: String,
|
||||||
local_uri: Option<String>,
|
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
published_date: Option<String>,
|
published_date: Option<String>,
|
||||||
length: Option<i32>,
|
length: Option<i32>,
|
||||||
guid: Option<String>,
|
guid: Option<String>,
|
||||||
epoch: i32,
|
epoch: i32,
|
||||||
played: Option<i32>,
|
|
||||||
favorite: bool,
|
|
||||||
archive: bool,
|
|
||||||
podcast_id: i32,
|
podcast_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,8 +188,6 @@ impl Update for NewEpisode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl NewEpisode {
|
impl NewEpisode {
|
||||||
// TODO: Currently using diesel from master git.
|
|
||||||
// Watch out for v0.99.0 beta and change the toml.
|
|
||||||
// TODO: Refactor into batch indexes instead.
|
// TODO: Refactor into batch indexes instead.
|
||||||
pub(crate) fn into_episode(self, con: &SqliteConnection) -> Result<Episode> {
|
pub(crate) fn into_episode(self, con: &SqliteConnection) -> Result<Episode> {
|
||||||
self.index(con)?;
|
self.index(con)?;
|
||||||
@ -287,108 +217,8 @@ impl NewEpisode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub(crate) struct NewEpisodeBuilder {
|
|
||||||
title: Option<String>,
|
|
||||||
uri: String,
|
|
||||||
local_uri: Option<String>,
|
|
||||||
description: Option<String>,
|
|
||||||
published_date: Option<String>,
|
|
||||||
length: Option<i32>,
|
|
||||||
guid: Option<String>,
|
|
||||||
epoch: i32,
|
|
||||||
played: Option<i32>,
|
|
||||||
favorite: bool,
|
|
||||||
archive: bool,
|
|
||||||
podcast_id: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
impl NewEpisodeBuilder {
|
// Ignore the following getters. They are used in unit tests mainly.
|
||||||
pub(crate) fn new() -> NewEpisodeBuilder {
|
|
||||||
NewEpisodeBuilder::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn title(mut self, s: Option<String>) -> NewEpisodeBuilder {
|
|
||||||
self.title = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn uri(mut self, s: String) -> NewEpisodeBuilder {
|
|
||||||
self.uri = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn local_uri(mut self, s: Option<String>) -> NewEpisodeBuilder {
|
|
||||||
self.local_uri = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn description(mut self, s: Option<String>) -> NewEpisodeBuilder {
|
|
||||||
self.description = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn published_date(mut self, s: Option<String>) -> NewEpisodeBuilder {
|
|
||||||
self.published_date = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn length(mut self, s: Option<i32>) -> NewEpisodeBuilder {
|
|
||||||
self.length = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn played(mut self, s: Option<i32>) -> NewEpisodeBuilder {
|
|
||||||
self.played = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn guid(mut self, s: Option<String>) -> NewEpisodeBuilder {
|
|
||||||
self.guid = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn epoch(mut self, s: i32) -> NewEpisodeBuilder {
|
|
||||||
self.epoch = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn podcast_id(mut self, s: i32) -> NewEpisodeBuilder {
|
|
||||||
self.podcast_id = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn favorite(mut self, s: bool) -> NewEpisodeBuilder {
|
|
||||||
self.favorite = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn archive(mut self, s: bool) -> NewEpisodeBuilder {
|
|
||||||
self.archive = s;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn build(self) -> NewEpisode {
|
|
||||||
NewEpisode {
|
|
||||||
title: self.title,
|
|
||||||
uri: self.uri,
|
|
||||||
local_uri: self.local_uri,
|
|
||||||
description: self.description,
|
|
||||||
published_date: self.published_date,
|
|
||||||
length: self.length,
|
|
||||||
guid: self.guid,
|
|
||||||
epoch: self.epoch,
|
|
||||||
played: self.played,
|
|
||||||
favorite: self.favorite,
|
|
||||||
archive: self.archive,
|
|
||||||
podcast_id: self.podcast_id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
// Ignore the following geters. They are used in unit tests mainly.
|
|
||||||
impl NewEpisode {
|
impl NewEpisode {
|
||||||
pub(crate) fn title(&self) -> Option<&str> {
|
pub(crate) fn title(&self) -> Option<&str> {
|
||||||
self.title.as_ref().map(|s| s.as_str())
|
self.title.as_ref().map(|s| s.as_str())
|
||||||
|
|||||||
@ -20,13 +20,14 @@ pub(crate) fn new_podcast(chan: &Channel, source_id: i32) -> NewPodcast {
|
|||||||
chan.image().map(|foo| url_cleaner(foo.url()))
|
chan.image().map(|foo| url_cleaner(foo.url()))
|
||||||
};
|
};
|
||||||
|
|
||||||
NewPodcastBuilder::new()
|
NewPodcastBuilder::default()
|
||||||
.title(title)
|
.title(title)
|
||||||
.description(description)
|
.description(description)
|
||||||
.link(link)
|
.link(link)
|
||||||
.image_uri(image_uri)
|
.image_uri(image_uri)
|
||||||
.source_id(source_id)
|
.source_id(source_id)
|
||||||
.build()
|
.build()
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses an `rss::Item` into a `NewEpisode` Struct.
|
/// Parses an `rss::Item` into a `NewEpisode` Struct.
|
||||||
@ -61,7 +62,7 @@ pub(crate) fn new_episode(item: &Item, parent_id: i32) -> Result<NewEpisode> {
|
|||||||
let length = item.enclosure().map(|x| x.length().parse().unwrap_or(0));
|
let length = item.enclosure().map(|x| x.length().parse().unwrap_or(0));
|
||||||
|
|
||||||
Ok(
|
Ok(
|
||||||
NewEpisodeBuilder::new()
|
NewEpisodeBuilder::default()
|
||||||
.title(title)
|
.title(title)
|
||||||
.uri(uri)
|
.uri(uri)
|
||||||
.description(description)
|
.description(description)
|
||||||
@ -70,7 +71,8 @@ pub(crate) fn new_episode(item: &Item, parent_id: i32) -> Result<NewEpisode> {
|
|||||||
.epoch(epoch)
|
.epoch(epoch)
|
||||||
.guid(guid)
|
.guid(guid)
|
||||||
.podcast_id(parent_id)
|
.podcast_id(parent_id)
|
||||||
.build(),
|
.build()
|
||||||
|
.unwrap()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -129,20 +129,29 @@ mod tests {
|
|||||||
// Setup episodes
|
// Setup episodes
|
||||||
let db = connection();
|
let db = connection();
|
||||||
let con = db.get().unwrap();
|
let con = db.get().unwrap();
|
||||||
NewEpisodeBuilder::new()
|
NewEpisodeBuilder::default()
|
||||||
.uri("foo_bar".to_string())
|
.uri("foo_bar".to_string())
|
||||||
.local_uri(Some(valid_path.to_str().unwrap().to_owned()))
|
|
||||||
.build()
|
.build()
|
||||||
|
.unwrap()
|
||||||
.into_episode(&con)
|
.into_episode(&con)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
NewEpisodeBuilder::new()
|
NewEpisodeBuilder::default()
|
||||||
.uri("bar_baz".to_string())
|
.uri("bar_baz".to_string())
|
||||||
.local_uri(Some(bad_path.to_str().unwrap().to_owned()))
|
|
||||||
.build()
|
.build()
|
||||||
|
.unwrap()
|
||||||
.into_episode(&con)
|
.into_episode(&con)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
let mut ep1 = dbqueries::get_episode_from_uri(&con, "foo_bar").unwrap();
|
||||||
|
let mut ep2 = dbqueries::get_episode_from_uri(&con, "bar_baz").unwrap();
|
||||||
|
ep1.set_local_uri(Some(valid_path.to_str().unwrap()));
|
||||||
|
ep2.set_local_uri(Some(bad_path.to_str().unwrap()));
|
||||||
|
|
||||||
|
drop(con);
|
||||||
|
ep1.save().unwrap();
|
||||||
|
ep2.save().unwrap();
|
||||||
|
|
||||||
tmp_dir
|
tmp_dir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user