From 2d7ba7b246f0f8c3d783a54e687a441e5f1159ee Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 3 Apr 2018 12:22:25 +0300 Subject: [PATCH] h-data/source.rs: Reduce boilerplate. --- hammond-data/src/models/source.rs | 78 +++++++------------------------ 1 file changed, 16 insertions(+), 62 deletions(-) diff --git a/hammond-data/src/models/source.rs b/hammond-data/src/models/source.rs index 1921e28..fe6e87c 100644 --- a/hammond-data/src/models/source.rs +++ b/hammond-data/src/models/source.rs @@ -102,6 +102,14 @@ impl Source { Ok(()) } + fn make_err(self, context: &str, code: StatusCode) -> DataError { + DataError::HttpStatusError { + url: self.uri, + status_code: code, + context: context.into(), + } + } + // TODO match on more stuff // 301: Moved Permanently // 304: Up to date Feed, checked with the Etag @@ -115,75 +123,21 @@ impl Source { fn match_status(mut self, res: Response) -> Result<(Self, Response), DataError> { self.update_etag(&res)?; let code = res.status(); - match code { - StatusCode::NotModified => { - let err = DataError::HttpStatusError { - url: self.uri, - status_code: code, - context: "304: skipping..".into(), - }; - return Err(err); - } + match code { + StatusCode::NotModified => return Err(self.make_err("304: skipping..", code)), StatusCode::MovedPermanently => { error!("Feed was moved permanently."); self.handle_301(&res)?; - - let err = DataError::HttpStatusError { - url: self.uri, - status_code: code, - context: "301: Feed was moved permanently.".into(), - }; - - return Err(err); + return Err(self.make_err("301: Feed was moved permanently.", code)); } StatusCode::TemporaryRedirect => debug!("307: Temporary Redirect."), StatusCode::PermanentRedirect => warn!("308: Permanent Redirect."), - StatusCode::Unauthorized => { - let err = DataError::HttpStatusError { - url: self.uri, - status_code: code, - context: "401: Unauthorized.".into(), - }; - - return Err(err); - } - StatusCode::Forbidden => { - let err = DataError::HttpStatusError { - url: self.uri, - status_code: code, - context: "403: Forbidden.".into(), - }; - - return Err(err); - } - StatusCode::NotFound => { - let err = DataError::HttpStatusError { - url: self.uri, - status_code: code, - context: "404: Not found.".into(), - }; - - return Err(err); - } - StatusCode::RequestTimeout => { - let err = DataError::HttpStatusError { - url: self.uri, - status_code: code, - context: "408: Request Timeout.".into(), - }; - - return Err(err); - } - StatusCode::Gone => { - let err = DataError::HttpStatusError { - url: self.uri, - status_code: code, - context: "410: Feed was deleted..".into(), - }; - - return Err(err); - } + StatusCode::Unauthorized => return Err(self.make_err("401: Unauthorized.", code)), + StatusCode::Forbidden => return Err(self.make_err("403: Forbidden.", code)), + StatusCode::NotFound => return Err(self.make_err("404: Not found.", code)), + StatusCode::RequestTimeout => return Err(self.make_err("408: Request Timeout.", code)), + StatusCode::Gone => return Err(self.make_err("410: Feed was deleted..", code)), _ => info!("HTTP StatusCode: {}", code), }; Ok((self, res))