From cc353c120ecccc596e87b2a6c92a9370885fec60 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 22 Sep 2017 13:06:33 +0300 Subject: [PATCH] Refactored Setter methods for the diesel models. --- src/index_feed.rs | 20 +++++++++---------- src/models.rs | 51 +++++++++++++++++++++++------------------------ 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/index_feed.rs b/src/index_feed.rs index 98f2689..612f86a 100644 --- a/src/index_feed.rs +++ b/src/index_feed.rs @@ -37,8 +37,8 @@ fn insert_source(con: &SqliteConnection, url: &str) -> Result { Ok(mut bar) => { // TODO: Cmp first before replacing // FIXME: NewSource has None values for etag, and last_mod atm - // bar.set_http_etag(foo.http_etag.map(|x| x.to_string())); - // bar.set_last_modified(foo.last_modified.map(|x| x.to_string())); + // bar.set_http_etag(foo.http_etag); + // bar.set_last_modified(foo.last_modified); // bar.save_changes::(con)?; } Err(_) => { @@ -61,11 +61,11 @@ fn index_podcast( match dbqueries::load_podcast(con, &pd.title) { Ok(mut foo) => { // TODO: Cmp first before replacing - foo.set_link(pd.link); - foo.set_description(pd.description); - foo.set_image_uri(pd.image_uri.map(|x| x.to_string())); + foo.set_link(&pd.link); + foo.set_description(&pd.description); + foo.set_image_uri(pd.image_uri.as_ref().map(|s| s.as_str())); foo.save_changes::(con)?; - } + } Err(_) => { diesel::insert(&pd).into(schema::podcast::table).execute( con, @@ -82,10 +82,10 @@ fn index_episode(con: &SqliteConnection, item: &rss::Item, parent: &Podcast) -> match dbqueries::load_episode(con, &ep.uri.unwrap()) { Ok(mut foo) => { // TODO: Cmp first before replacing - foo.set_title(ep.title.map(|x| x.to_string())); - foo.set_description(ep.description.map(|x| x.to_string())); - foo.set_published_date(ep.published_date.map(|x| x.to_string())); - foo.set_guid(ep.guid.map(|x| x.to_string())); + foo.set_title(ep.title); + foo.set_description(ep.description); + foo.set_published_date(ep.published_date); + foo.set_guid(ep.guid); foo.set_length(ep.length); foo.set_epoch(ep.length); foo.save_changes::(con)?; diff --git a/src/models.rs b/src/models.rs index 3f9a494..58dabdc 100644 --- a/src/models.rs +++ b/src/models.rs @@ -34,48 +34,48 @@ impl Episode { self.title.as_ref().map(|s| s.as_str()) } - pub fn set_title(&mut self, value: Option) { - self.title = value; + pub fn set_title(&mut self, value: Option<&str>) { + self.title = value.map(|x| x.to_string()); } pub fn uri(&self) -> Option<&str> { self.uri.as_ref().map(|s| s.as_str()) } - pub fn set_uri(&mut self, value: Option) { - self.uri = value; + pub fn set_uri(&mut self, value: Option<&str>) { + self.uri = value.map(|x| x.to_string()); } pub fn local_uri(&self) -> Option<&str> { self.local_uri.as_ref().map(|s| s.as_str()) } - pub fn set_local_uri(&mut self, value: Option) { - self.local_uri = value; + pub fn set_local_uri(&mut self, value: Option<&str>) { + self.local_uri = value.map(|x| x.to_string()); } pub fn description(&self) -> Option<&str> { self.description.as_ref().map(|s| s.as_str()) } - pub fn set_description(&mut self, value: Option) { - self.description = value; + pub fn set_description(&mut self, value: Option<&str>) { + self.description = value.map(|x| x.to_string()); } pub fn published_date(&self) -> Option<&str> { self.published_date.as_ref().map(|s| s.as_str()) } - pub fn set_published_date(&mut self, value: Option) { - self.published_date = value; + pub fn set_published_date(&mut self, value: Option<&str>) { + self.published_date = value.map(|x| x.to_string()); } pub fn guid(&self) -> Option<&str> { self.guid.as_ref().map(|s| s.as_str()) } - pub fn set_guid(&mut self, value: Option) { - self.guid = value; + pub fn set_guid(&mut self, value: Option<&str>) { + self.guid = value.map(|x| x.to_string()); } pub fn epoch(&self) -> Option { @@ -122,24 +122,24 @@ impl Podcast { &self.link } - pub fn set_link(&mut self, value: String) { - self.link = value; + pub fn set_link(&mut self, value: &str) { + self.link = value.to_string(); } pub fn description(&self) -> &str { &self.description } - pub fn set_description(&mut self, value: String) { - self.description = value; + pub fn set_description(&mut self, value: &str) { + self.description = value.to_string(); } pub fn image_uri(&self) -> Option<&str> { self.image_uri.as_ref().map(|s| s.as_str()) } - pub fn set_image_uri(&mut self, value: Option) { - self.image_uri = value; + pub fn set_image_uri(&mut self, value: Option<&str>) { + self.image_uri = value.map(|x| x.to_string()); } } @@ -166,16 +166,16 @@ impl<'a> Source { self.last_modified.as_ref().map(|s| s.as_str()) } - pub fn set_last_modified(&mut self, value: Option) { - self.last_modified = value; + pub fn set_last_modified(&mut self, value: Option<&str>) { + self.last_modified = value.map(|x| x.to_string()); } pub fn http_etag(&self) -> Option<&str> { self.http_etag.as_ref().map(|s| s.as_str()) } - pub fn set_http_etag(&mut self, value: Option) { - self.http_etag = value; + pub fn set_http_etag(&mut self, value: Option<&str>) { + self.http_etag = value.map(|x| x.to_string()); } /// Fetch the xml feed from the source url, update the etag headers, @@ -195,6 +195,7 @@ impl<'a> Source { Ok(chan) } + /// Extract Etag and LastModifier from req, and update self and the corresponding db row. fn update_etag(&mut self, con: &SqliteConnection, req: &reqwest::Response) -> Result<()> { let headers = req.headers(); @@ -260,10 +261,8 @@ pub struct NewPodcast { pub source_id: i32, } -impl<'a> NewPodcast { - // pub fn new(parent: &Source) {} - - pub fn from_url(uri: &'a str, parent: &Source) -> Result { +impl NewPodcast { + pub fn from_url(uri: &str, parent: &Source) -> Result { let chan = Channel::from_url(uri)?; let foo = ::feedparser::parse_podcast(&chan, parent.id())?; Ok(foo)