diff --git a/hammond-data/src/models/source.rs b/hammond-data/src/models/source.rs index 4843aaa..2d58193 100644 --- a/hammond-data/src/models/source.rs +++ b/hammond-data/src/models/source.rs @@ -51,8 +51,9 @@ impl Source { } /// Set `last_modified` value. - pub fn set_last_modified(&mut self, value: Option<&str>) { - self.last_modified = value.map(|x| x.to_string()); + pub fn set_last_modified(&mut self, value: Option) { + // self.last_modified = value.map(|x| x.to_string()); + self.last_modified = value; } /// Represents the Http Etag Header field. @@ -77,8 +78,7 @@ impl Source { /// Extract Etag and LastModifier from res, and update self and the /// corresponding db row. - // FIXME: With &mut self the closure is of type FnMut instead of FnOnce. - fn update_etag(mut self, res: &Response) -> Result<()> { + fn update_etag(&mut self, res: &Response) -> Result<()> { let headers = res.headers(); let etag = headers.get::(); @@ -87,14 +87,21 @@ impl Source { if self.http_etag() != etag.map(|x| x.tag()) || self.last_modified != lmod.map(|x| { format!("{}", x) }) { - self.http_etag = etag.map(|x| x.tag().to_string().to_owned()); - self.last_modified = lmod.map(|x| format!("{}", x)); + self.set_http_etag(etag.map(|x| x.tag())); + self.set_last_modified(lmod.map(|x| format!("{}", x))); self.save()?; } Ok(()) } + /// Construct a new `Source` with the given `uri` and index it. + /// + /// This only indexes the `Source` struct, not the Podcast Feed. + pub fn from_url(uri: &str) -> Result { + NewSource::new(uri).into_source() + } + /// `Feed` constructor. /// /// Fetches the latest xml Feed. @@ -103,13 +110,13 @@ impl Source { /// /// Consumes `self` and Returns the corresponding `Feed` Object. // TODO: Refactor into TryInto once it lands on stable. - pub fn into_feed( - self, + pub fn to_feed( + mut self, client: &Client>, ignore_etags: bool, ) -> Box> { let id = self.id(); - let feed = request_constructor(&self, client, ignore_etags) + let feed = self.request_constructor(client, ignore_etags) .map_err(From::from) .and_then(move |res| { self.update_etag(&res)?; @@ -125,41 +132,34 @@ impl Source { Box::new(feed) } - /// Construct a new `Source` with the given `uri` and index it. - /// - /// This only indexes the `Source` struct, not the Podcast Feed. - pub fn from_url(uri: &str) -> Result { - NewSource::new(uri).into_source() - } -} + // TODO: make ignore_etags an Enum for better ergonomics. + // #bools_are_just_2variant_enmus + fn request_constructor( + &self, + client: &Client>, + ignore_etags: bool, + ) -> Box> { + // FIXME: remove unwrap somehow + let uri = Uri::from_str(self.uri()).unwrap(); + let mut req = Request::new(Method::Get, uri); -// TODO: make ignore_etags an Enum for better ergonomics. -// #bools_are_just_2variant_enmus -fn request_constructor( - s: &Source, - client: &Client>, - ignore_etags: bool, -) -> Box> { - // FIXME: remove unwrap somehow - let uri = Uri::from_str(&s.uri()).unwrap(); - let mut req = Request::new(Method::Get, uri); + if !ignore_etags { + if let Some(foo) = self.http_etag() { + req.headers_mut().set(IfNoneMatch::Items(vec![ + EntityTag::new(true, foo.to_owned()), + ])); + } - if !ignore_etags { - if let Some(foo) = s.http_etag() { - req.headers_mut().set(IfNoneMatch::Items(vec![ - EntityTag::new(true, foo.to_owned()), - ])); - } - - if let Some(foo) = s.last_modified() { - if let Ok(x) = foo.parse::() { - req.headers_mut().set(IfModifiedSince(x)); + if let Some(foo) = self.last_modified() { + if let Ok(x) = foo.parse::() { + req.headers_mut().set(IfModifiedSince(x)); + } } } - } - let work = client.request(req).map_err(From::from); - Box::new(work) + let work = client.request(req).map_err(From::from); + Box::new(work) + } } fn response_to_channel(res: Response) -> Box> { @@ -191,14 +191,15 @@ fn match_status(code: StatusCode) -> Result<()> { StatusCode::TemporaryRedirect => debug!("307: Temporary Redirect."), // TODO: Change the source uri to the new one StatusCode::MovedPermanently | StatusCode::PermanentRedirect => { - warn!("Feed was moved permanently.") + warn!("Feed was moved permanently."); + bail!("Feed was moved permanently.") } StatusCode::Unauthorized => bail!("401: Unauthorized."), StatusCode::Forbidden => bail!("403: Forbidden."), StatusCode::NotFound => bail!("404: Not found."), StatusCode::RequestTimeout => bail!("408: Request Timeout."), StatusCode::Gone => bail!("410: Feed was deleted."), - _ => (), + _ => info!("HTTP StatusCode: {}", code), }; Ok(()) } @@ -222,7 +223,7 @@ mod tests { let url = "http://www.newrustacean.com/feed.xml"; let source = Source::from_url(url).unwrap(); - let feed = source.into_feed(&client, true); + let feed = source.to_feed(&client, true); assert!(core.run(feed).is_ok()); } diff --git a/hammond-data/src/pipeline.rs b/hammond-data/src/pipeline.rs index c2064e3..a2d1685 100644 --- a/hammond-data/src/pipeline.rs +++ b/hammond-data/src/pipeline.rs @@ -35,9 +35,8 @@ pub fn pipeline>(sources: S, ignore_etags: bool) let list = sources .into_iter() - // FIXME: Make proper indexing futures instead of wrapping up existing - // blocking functions - .map(|s| s.into_feed(&client, ignore_etags).and_then(|feed| feed.index_async())) + .map(|s| s.to_feed(&client, ignore_etags)) + .map(|fut| fut.and_then(|feed| feed.index_async())) .collect(); let f = core.run(collect_futures(list))?; diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 821fbce..95586d4 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -239,7 +239,7 @@ mod tests { // Copy it's id let sid = source.id(); // Convert Source it into a future Feed and index it - let future = source.into_feed(&client, true); + let future = source.to_feed(&client, true); let feed = core.run(future).unwrap(); feed.index().unwrap(); diff --git a/hammond-gtk/src/manager.rs b/hammond-gtk/src/manager.rs index 476edd7..1916383 100644 --- a/hammond-gtk/src/manager.rs +++ b/hammond-gtk/src/manager.rs @@ -147,7 +147,7 @@ mod tests { // Copy it's id let sid = source.id(); // Convert Source it into a future Feed and index it - let future = source.into_feed(&client, true); + let future = source.to_feed(&client, true); let feed = core.run(future).unwrap(); feed.index().unwrap(); diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 5f11ce7..7ff58ad 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -104,7 +104,7 @@ mod tests { // Copy it's id let sid = source.id(); // Convert Source it into a future Feed and index it - let future = source.into_feed(&client, true); + let future = source.to_feed(&client, true); let feed = core.run(future).unwrap(); feed.index().unwrap();