Redefine NewModels to hold Option<String> instead of Option<&str>.

This commit is contained in:
Jordan Petridis 2017-11-22 09:53:52 +02:00
parent 31328355b1
commit 3bcb23b39a
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 74 additions and 43 deletions

View File

@ -9,16 +9,16 @@ use dbqueries;
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "source"] #[table_name = "source"]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct NewSource<'a> { pub struct NewSource {
uri: &'a str, uri: String,
last_modified: Option<&'a str>, last_modified: Option<String>,
http_etag: Option<&'a str>, http_etag: Option<String>,
} }
impl<'a> NewSource<'a> { impl NewSource {
pub fn new_with_uri(uri: &'a str) -> NewSource { pub fn new_with_uri(uri: &str) -> NewSource {
NewSource { NewSource {
uri, uri: uri.to_string(),
last_modified: None, last_modified: None,
http_etag: None, http_etag: None,
} }
@ -34,25 +34,25 @@ impl<'a> NewSource<'a> {
pub fn into_source(self) -> QueryResult<Source> { pub fn into_source(self) -> QueryResult<Source> {
self.index(); self.index();
dbqueries::get_source_from_uri(self.uri) dbqueries::get_source_from_uri(&self.uri)
} }
} }
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "episode"] #[table_name = "episode"]
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct NewEpisode<'a> { pub struct NewEpisode {
pub title: Option<&'a str>, pub title: Option<String>,
pub uri: Option<String>, pub uri: Option<String>,
pub description: Option<&'a str>, pub description: Option<String>,
pub published_date: Option<String>, pub published_date: Option<String>,
pub length: Option<i32>, pub length: Option<i32>,
pub guid: Option<&'a str>, pub guid: Option<String>,
pub epoch: i32, pub epoch: i32,
pub podcast_id: i32, pub podcast_id: i32,
} }
impl<'a> NewEpisode<'a> { impl NewEpisode {
// TODO: Currently using diesel from master git. // TODO: Currently using diesel from master git.
// Watch out for v0.99.0 beta and change the toml. // Watch out for v0.99.0 beta and change the toml.
// TODO: Refactor into batch indexes instead. // TODO: Refactor into batch indexes instead.
@ -60,7 +60,7 @@ impl<'a> NewEpisode<'a> {
let ep = dbqueries::get_episode_from_uri(&self.uri.clone().unwrap()); let ep = dbqueries::get_episode_from_uri(&self.uri.clone().unwrap());
match ep { match ep {
Ok(foo) => if foo.title() != self.title Ok(foo) => if foo.title() != self.title.as_ref().map(|x| x.as_str())
|| foo.published_date() != self.published_date.as_ref().map(|x| x.as_str()) || foo.published_date() != self.published_date.as_ref().map(|x| x.as_str())
{ {
dbqueries::replace_episode(self)?; dbqueries::replace_episode(self)?;

View File

@ -29,9 +29,9 @@ pub fn new_podcast(chan: &Channel, source_id: i32) -> NewPodcast {
/// Parses an `rss::Item` into a `NewEpisode` Struct. /// Parses an `rss::Item` into a `NewEpisode` Struct.
pub fn new_episode(item: &Item, parent_id: i32) -> NewEpisode { pub fn new_episode(item: &Item, parent_id: i32) -> NewEpisode {
let title = item.title().map(|s| s.trim()); let title = item.title().map(|s| s.trim().to_owned());
let description = item.description().map(|s| s.trim()); let description = item.description().map(|s| s.trim().to_owned());
let guid = item.guid().map(|s| s.value().trim()); let guid = item.guid().map(|s| s.value().trim().to_owned());
// Its kinda weird this being an Option type. // Its kinda weird this being an Option type.
// Rss 2.0 specified that it's optional. // Rss 2.0 specified that it's optional.
@ -176,14 +176,17 @@ mod tests {
performs."; performs.";
let i = new_episode(&firstitem, 0); let i = new_episode(&firstitem, 0);
assert_eq!(i.title, Some("The Super Bowl of Racism")); assert_eq!(i.title, Some("The Super Bowl of Racism".to_string()));
assert_eq!( assert_eq!(
i.uri, i.uri,
Some("http://traffic.megaphone.fm/PPY6458293736.mp3".to_string()) Some("http://traffic.megaphone.fm/PPY6458293736.mp3".to_string())
); );
assert_eq!(i.description, Some(descr)); assert_eq!(i.description, Some(descr.to_string()));
assert_eq!(i.length, Some(66738886)); assert_eq!(i.length, Some(66738886));
assert_eq!(i.guid, Some("7df4070a-9832-11e7-adac-cb37b05d5e24")); assert_eq!(
i.guid,
Some("7df4070a-9832-11e7-adac-cb37b05d5e24".to_string())
);
assert_eq!( assert_eq!(
i.published_date, i.published_date,
Some("Wed, 13 Sep 2017 10:00:00 +0000".to_string()) Some("Wed, 13 Sep 2017 10:00:00 +0000".to_string())
@ -203,15 +206,18 @@ mod tests {
Venezuela."; Venezuela.";
assert_eq!( assert_eq!(
i2.title, i2.title,
Some("Atlas Golfed — U.S.-Backed Think Tanks Target Latin America",) Some("Atlas Golfed — U.S.-Backed Think Tanks Target Latin America".to_string(),)
); );
assert_eq!( assert_eq!(
i2.uri, i2.uri,
Some("http://traffic.megaphone.fm/FL5331443769.mp3".to_string()) Some("http://traffic.megaphone.fm/FL5331443769.mp3".to_string())
); );
assert_eq!(i2.description, Some(descr2)); assert_eq!(i2.description, Some(descr2.to_string()));
assert_eq!(i2.length, Some(67527575)); assert_eq!(i2.length, Some(67527575));
assert_eq!(i2.guid, Some("7c207a24-e33f-11e6-9438-eb45dcf36a1d")); assert_eq!(
i2.guid,
Some("7c207a24-e33f-11e6-9438-eb45dcf36a1d".to_string())
);
assert_eq!( assert_eq!(
i2.published_date, i2.published_date,
Some("Wed, 9 Aug 2017 10:00:00 +0000".to_string()) Some("Wed, 9 Aug 2017 10:00:00 +0000".to_string())
@ -231,7 +237,10 @@ mod tests {
assert_eq!( assert_eq!(
i.title, i.title,
Some("The Breakthrough: Hopelessness and Exploitation Inside Homes for Mentally Ill",) Some(
"The Breakthrough: Hopelessness and Exploitation Inside Homes for Mentally Ill"
.to_string(),
)
); );
assert_eq!( assert_eq!(
i.uri, i.uri,
@ -240,13 +249,14 @@ mod tests {
.to_string(), .to_string(),
) )
); );
assert_eq!(i.description, Some(descr)); assert_eq!(i.description, Some(descr.to_string()));
assert_eq!(i.length, Some(33396551)); assert_eq!(i.length, Some(33396551));
assert_eq!( assert_eq!(
i.guid, i.guid,
Some( Some(
"https://www.propublica.org/podcast/\ "https://www.propublica.org/podcast/\
the-breakthrough-hopelessness-exploitation-homes-for-mentally-ill#134472", the-breakthrough-hopelessness-exploitation-homes-for-mentally-ill#134472"
.to_string(),
) )
); );
assert_eq!( assert_eq!(
@ -263,9 +273,8 @@ mod tests {
assert_eq!( assert_eq!(
i2.title, i2.title,
Some( Some("The Breakthrough: Behind the Scenes of Hillary Clintons Failed Bid for \
"The Breakthrough: Behind the Scenes of Hillary Clintons Failed Bid for President", President".to_string())
)
); );
assert_eq!( assert_eq!(
i2.uri, i2.uri,
@ -274,13 +283,14 @@ mod tests {
.to_string(), .to_string(),
) )
); );
assert_eq!(i2.description, Some(descr2)); assert_eq!(i2.description, Some(descr2.to_string()));
assert_eq!(i2.length, Some(17964071)); assert_eq!(i2.length, Some(17964071));
assert_eq!( assert_eq!(
i2.guid, i2.guid,
Some( Some(
"https://www.propublica.\ "https://www.propublica.\
org/podcast/the-breakthrough-hillary-clinton-failed-presidential-bid#133721", org/podcast/the-breakthrough-hillary-clinton-failed-presidential-bid#133721"
.to_string(),
) )
); );
assert_eq!( assert_eq!(
@ -302,7 +312,10 @@ mod tests {
blaming open source & the Beard just saved the show. Its a really packed episode!"; blaming open source & the Beard just saved the show. Its a really packed episode!";
let i = new_episode(&firstitem, 0); let i = new_episode(&firstitem, 0);
assert_eq!(i.title, Some("Hacking Devices with Kali Linux | LUP 214")); assert_eq!(
i.title,
Some("Hacking Devices with Kali Linux | LUP 214".to_string())
);
assert_eq!( assert_eq!(
i.uri, i.uri,
Some( Some(
@ -310,9 +323,12 @@ mod tests {
.to_string(), .to_string(),
) )
); );
assert_eq!(i.description, Some(descr)); assert_eq!(i.description, Some(descr.to_string()));
assert_eq!(i.length, Some(46479789)); assert_eq!(i.length, Some(46479789));
assert_eq!(i.guid, Some("78A682B4-73E8-47B8-88C0-1BE62DD4EF9D")); assert_eq!(
i.guid,
Some("78A682B4-73E8-47B8-88C0-1BE62DD4EF9D".to_string())
);
assert_eq!( assert_eq!(
i.published_date, i.published_date,
Some("Tue, 12 Sep 2017 22:24:42 -0700".to_string()) Some("Tue, 12 Sep 2017 22:24:42 -0700".to_string())
@ -328,7 +344,7 @@ mod tests {
future.</p>\n\n<p>Plus we chat with Wimpy about the Ubuntu Rally in NYC, \ future.</p>\n\n<p>Plus we chat with Wimpy about the Ubuntu Rally in NYC, \
Microsofts sneaky move to turn Windows 10 into the ULTIMATE LINUX \ Microsofts sneaky move to turn Windows 10 into the ULTIMATE LINUX \
RUNTIME, community news & more!</p>"; RUNTIME, community news & more!</p>";
assert_eq!(i2.title, Some("Gnome Does it Again | LUP 213")); assert_eq!(i2.title, Some("Gnome Does it Again | LUP 213".to_string()));
assert_eq!( assert_eq!(
i2.uri, i2.uri,
Some( Some(
@ -336,9 +352,12 @@ mod tests {
.to_string(), .to_string(),
) )
); );
assert_eq!(i2.description, Some(descr2)); assert_eq!(i2.description, Some(descr2.to_string()));
assert_eq!(i2.length, Some(36544272)); assert_eq!(i2.length, Some(36544272));
assert_eq!(i2.guid, Some("1CE57548-B36C-4F14-832A-5D5E0A24E35B")); assert_eq!(
i2.guid,
Some("1CE57548-B36C-4F14-832A-5D5E0A24E35B".to_string())
);
assert_eq!( assert_eq!(
i2.published_date, i2.published_date,
Some("Tue, 5 Sep 2017 20:57:27 -0700".to_string()) Some("Tue, 5 Sep 2017 20:57:27 -0700".to_string())
@ -357,7 +376,10 @@ mod tests {
\"Non-lexical lifetimes\""; \"Non-lexical lifetimes\"";
let i = new_episode(&firstitem, 0); let i = new_episode(&firstitem, 0);
assert_eq!(i.title, Some("Episode #9 - A Once in a Lifetime RFC")); assert_eq!(
i.title,
Some("Episode #9 - A Once in a Lifetime RFC".to_string())
);
assert_eq!( assert_eq!(
i.uri, i.uri,
Some( Some(
@ -366,11 +388,14 @@ mod tests {
.to_string(), .to_string(),
) )
); );
assert_eq!(i.description, Some(descr)); assert_eq!(i.description, Some(descr.to_string()));
assert_eq!(i.length, Some(15077388)); assert_eq!(i.length, Some(15077388));
assert_eq!( assert_eq!(
i.guid, i.guid,
Some("https://request-for-explanation.github.io/podcast/ep9-a-once-in-a-lifetime-rfc/",) Some(
"https://request-for-explanation.github.io/podcast/ep9-a-once-in-a-lifetime-rfc/"
.to_string(),
)
); );
assert_eq!( assert_eq!(
i.published_date, i.published_date,
@ -384,7 +409,10 @@ mod tests {
let descr2 = "This week we look at <a \ let descr2 = "This week we look at <a \
href=\"https://github.com/rust-lang/rfcs/pull/2071\">RFC 2071</a> \"Add \ href=\"https://github.com/rust-lang/rfcs/pull/2071\">RFC 2071</a> \"Add \
impl Trait type alias and variable declarations\""; impl Trait type alias and variable declarations\"";
assert_eq!(i2.title, Some("Episode #8 - An Existential Crisis")); assert_eq!(
i2.title,
Some("Episode #8 - An Existential Crisis".to_string())
);
assert_eq!( assert_eq!(
i2.uri, i2.uri,
Some( Some(
@ -393,11 +421,14 @@ mod tests {
.to_string(), .to_string(),
) )
); );
assert_eq!(i2.description, Some(descr2)); assert_eq!(i2.description, Some(descr2.to_string()));
assert_eq!(i2.length, Some(13713219)); assert_eq!(i2.length, Some(13713219));
assert_eq!( assert_eq!(
i2.guid, i2.guid,
Some("https://request-for-explanation.github.io/podcast/ep8-an-existential-crisis/",) Some(
"https://request-for-explanation.github.io/podcast/ep8-an-existential-crisis/"
.to_string(),
)
); );
assert_eq!( assert_eq!(
i2.published_date, i2.published_date,