Source: Move around code and change some methods signature.

request_constructor is now a Source method.
update_etags now takes &mut self instead of mut self.
update_etags now uses source setters methods instead of raw fields.
changed the naming of into_feed to to_feed according to rust convention.
This commit is contained in:
Jordan Petridis 2018-01-19 07:51:55 +02:00
parent 9dc555cad7
commit 5c5faafc72
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
5 changed files with 48 additions and 48 deletions

View File

@ -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<String>) {
// 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::<ETag>();
@ -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<Source> {
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<HttpsConnector<HttpConnector>>,
ignore_etags: bool,
) -> Box<Future<Item = Feed, Error = Error>> {
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,33 +132,25 @@ 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<Source> {
NewSource::new(uri).into_source()
}
}
// TODO: make ignore_etags an Enum for better ergonomics.
// #bools_are_just_2variant_enmus
fn request_constructor(
s: &Source,
// TODO: make ignore_etags an Enum for better ergonomics.
// #bools_are_just_2variant_enmus
fn request_constructor(
&self,
client: &Client<HttpsConnector<HttpConnector>>,
ignore_etags: bool,
) -> Box<Future<Item = Response, Error = hyper::Error>> {
) -> Box<Future<Item = Response, Error = hyper::Error>> {
// FIXME: remove unwrap somehow
let uri = Uri::from_str(&s.uri()).unwrap();
let uri = Uri::from_str(self.uri()).unwrap();
let mut req = Request::new(Method::Get, uri);
if !ignore_etags {
if let Some(foo) = s.http_etag() {
if let Some(foo) = self.http_etag() {
req.headers_mut().set(IfNoneMatch::Items(vec![
EntityTag::new(true, foo.to_owned()),
]));
}
if let Some(foo) = s.last_modified() {
if let Some(foo) = self.last_modified() {
if let Ok(x) = foo.parse::<HttpDate>() {
req.headers_mut().set(IfModifiedSince(x));
}
@ -160,6 +159,7 @@ fn request_constructor(
let work = client.request(req).map_err(From::from);
Box::new(work)
}
}
fn response_to_channel(res: Response) -> Box<Future<Item = Channel, Error = Error>> {
@ -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());
}

View File

@ -35,9 +35,8 @@ pub fn pipeline<S: IntoIterator<Item = Source>>(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))?;

View File

@ -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();

View File

@ -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();

View File

@ -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();