From 476b1fb36a40f6a2339c6b170d96e0131f2091d2 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 2 Apr 2018 21:43:50 -0700 Subject: [PATCH] simplify DefaultHeaders middleware --- guide/src/qs_10.md | 5 +- src/middleware/defaultheaders.rs | 89 ++++++++++++++------------------ src/middleware/mod.rs | 2 +- 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/guide/src/qs_10.md b/guide/src/qs_10.md index 9a4addba1..aaff39ae1 100644 --- a/guide/src/qs_10.md +++ b/guide/src/qs_10.md @@ -140,9 +140,8 @@ use actix_web::{http, middleware, App, HttpResponse}; fn main() { let app = App::new() .middleware( - middleware::DefaultHeaders::build() - .header("X-Version", "0.2") - .finish()) + middleware::DefaultHeaders::new() + .header("X-Version", "0.2")) .resource("/test", |r| { r.method(http::Method::GET).f(|req| HttpResponse::Ok()); r.method(http::Method::HEAD).f(|req| HttpResponse::MethodNotAllowed()); diff --git a/src/middleware/defaultheaders.rs b/src/middleware/defaultheaders.rs index 068002cad..5399b29d4 100644 --- a/src/middleware/defaultheaders.rs +++ b/src/middleware/defaultheaders.rs @@ -18,9 +18,8 @@ use middleware::{Response, Middleware}; /// fn main() { /// let app = App::new() /// .middleware( -/// middleware::DefaultHeaders::build() -/// .header("X-Version", "0.2") -/// .finish()) +/// middleware::DefaultHeaders::new() +/// .header("X-Version", "0.2")) /// .resource("/test", |r| { /// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); /// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed()); @@ -33,9 +32,41 @@ pub struct DefaultHeaders{ headers: HeaderMap, } +impl Default for DefaultHeaders { + fn default() -> Self { + DefaultHeaders{ct: false, headers: HeaderMap::new()} + } +} + impl DefaultHeaders { - pub fn build() -> DefaultHeadersBuilder { - DefaultHeadersBuilder{ct: false, headers: Some(HeaderMap::new())} + /// Construct `DefaultHeaders` middleware. + pub fn new() -> DefaultHeaders { + DefaultHeaders::default() + } + + /// Set a header. + #[inline] + #[cfg_attr(feature = "cargo-clippy", allow(match_wild_err_arm))] + pub fn header(mut self, key: K, value: V) -> Self + where HeaderName: HttpTryFrom, + HeaderValue: HttpTryFrom + { + match HeaderName::try_from(key) { + Ok(key) => { + match HeaderValue::try_from(value) { + Ok(value) => { self.headers.append(key, value); } + Err(_) => panic!("Can not create header value"), + } + }, + Err(_) => panic!("Can not create header name"), + } + self + } + + /// Set *CONTENT-TYPE* header if response does not contain this header. + pub fn content_type(mut self) -> Self { + self.ct = true; + self } } @@ -56,49 +87,6 @@ impl Middleware for DefaultHeaders { } } -/// Structure that follows the builder pattern for building `DefaultHeaders` middleware. -#[derive(Debug)] -pub struct DefaultHeadersBuilder { - ct: bool, - headers: Option, -} - -impl DefaultHeadersBuilder { - - /// Set a header. - #[inline] - #[cfg_attr(feature = "cargo-clippy", allow(match_wild_err_arm))] - pub fn header(&mut self, key: K, value: V) -> &mut Self - where HeaderName: HttpTryFrom, - HeaderValue: HttpTryFrom - { - if let Some(ref mut headers) = self.headers { - match HeaderName::try_from(key) { - Ok(key) => { - match HeaderValue::try_from(value) { - Ok(value) => { headers.append(key, value); } - Err(_) => panic!("Can not create header value"), - } - }, - Err(_) => panic!("Can not create header name"), - }; - } - self - } - - /// Set *CONTENT-TYPE* header if response does not contain this header. - pub fn content_type(&mut self) -> &mut Self { - self.ct = true; - self - } - - /// Finishes building and returns the built `DefaultHeaders` middleware. - pub fn finish(&mut self) -> DefaultHeaders { - let headers = self.headers.take().expect("cannot reuse middleware builder"); - DefaultHeaders{ ct: self.ct, headers } - } -} - #[cfg(test)] mod tests { use super::*; @@ -106,9 +94,8 @@ mod tests { #[test] fn test_default_headers() { - let mw = DefaultHeaders::build() - .header(CONTENT_TYPE, "0001") - .finish(); + let mw = DefaultHeaders::new() + .header(CONTENT_TYPE, "0001"); let mut req = HttpRequest::default(); diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index 97f3fa2b0..8b0503925 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -15,7 +15,7 @@ pub mod cors; pub mod csrf; pub use self::logger::Logger; pub use self::errhandlers::ErrorHandlers; -pub use self::defaultheaders::{DefaultHeaders, DefaultHeadersBuilder}; +pub use self::defaultheaders::DefaultHeaders; #[cfg(feature = "session")] pub use self::session::{RequestSession, Session, SessionImpl, SessionBackend, SessionStorage,