From dfd27c867b6aecc166fb8d7f4da67d071eaac223 Mon Sep 17 00:00:00 2001 From: Kazuhiko Kikuchi Date: Fri, 7 Dec 2018 15:07:39 +0900 Subject: [PATCH 1/3] implement get_method, get_uri, get_headers --- src/request.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/src/request.rs b/src/request.rs index 382af1e4..a38981ed 100644 --- a/src/request.rs +++ b/src/request.rs @@ -771,6 +771,34 @@ impl Builder { self } + /// Get the HTTP Method for this request. + /// + /// By default this is `GET`. + /// if builder has error, returns None. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let mut req = Request::builder(); + /// assert_eq!(req.get_method(),Some(Method::GET)); + /// req.method("POST"); + /// assert_eq!(req.get_method(),Some(Method::POST)); + /// req.method("DELETE"); + /// assert_eq!(req.get_method(),Some(Method::DELETE)); + /// ``` + pub fn get_method(&self) -> Option + { + if self.err.is_some() { + return None + } + match &self.head { + Some(head) => Some(head.method.clone()), + None => None + } + } + /// Set the URI for this request. /// /// This function will configure the URI of the `Request` that will @@ -800,6 +828,31 @@ impl Builder { self } + /// Get the URI for this request + /// + /// By default this is `/` + /// # Examples + /// + /// ``` + /// # use http::*; + /// + /// let mut req = Request::builder(); + /// assert_eq!(req.get_uri().unwrap().to_string(), "/" ); + /// req.uri("https://www.rust-lang.org/"); + /// assert_eq!(req.get_uri().unwrap().to_string(), "https://www.rust-lang.org/" ); + /// ``` + pub fn get_uri(&self) -> Option + { + if self.err.is_some() { + return None; + } + match &self.head + { + Some(head) => Some(head.uri.clone()), + None => None + } + } + /// Set the HTTP version for this request. /// /// This function will configure the HTTP version of the `Request` that @@ -860,6 +913,33 @@ impl Builder { self } + /// Get header on this request builder. + /// when builder has error returns None + /// + /// # Example + /// + /// ``` + /// # use http::*; + /// # use http::header::HeaderValue; + /// # use http::request::Builder; + /// let mut req = Request::builder(); + /// req.header("Accept", "text/html") + /// .header("X-Custom-Foo", "bar"); + /// let headers = req.get_headers().unwrap(); + /// assert_eq!( headers["Accept"], "text/html" ); + /// assert_eq!( headers["X-Custom-Foo"], "bar" ); + /// ``` + pub fn get_headers(&self ) -> Option> { + if self.err.is_some() { + return None; + } + match &self.head + { + Some(head) => Some(head.headers.clone()), + None => None + } + } + /// Adds an extension to this builder /// /// # Examples From 5d9b7d8eeac92ab98d2bd9bbeb57049b422738d7 Mon Sep 17 00:00:00 2001 From: Kazuhiko Kikuchi Date: Thu, 13 Dec 2018 12:29:58 +0900 Subject: [PATCH 2/3] fix compile errors on rust 1.20 --- src/request.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/request.rs b/src/request.rs index a38981ed..7904fc9d 100644 --- a/src/request.rs +++ b/src/request.rs @@ -794,8 +794,8 @@ impl Builder { return None } match &self.head { - Some(head) => Some(head.method.clone()), - None => None + &Some(ref head) => Some(head.method.clone()), + &None => None } } @@ -848,8 +848,8 @@ impl Builder { } match &self.head { - Some(head) => Some(head.uri.clone()), - None => None + &Some(ref head) => Some(head.uri.clone()), + &None => None } } @@ -935,8 +935,8 @@ impl Builder { } match &self.head { - Some(head) => Some(head.headers.clone()), - None => None + &Some(ref head) => Some(head.headers.clone()), + &None => None } } From 71726ca4607e8c6a028d34981786badda4fff474 Mon Sep 17 00:00:00 2001 From: Kazuhiko Kikuchi Date: Fri, 14 Dec 2018 10:07:19 +0900 Subject: [PATCH 3/3] apply review comments change function name get_method -> method_ref get_uri -> uri_ref get_headers -> header_ref avoid clone, and returns reference --- src/request.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/request.rs b/src/request.rs index 7904fc9d..2fc1163d 100644 --- a/src/request.rs +++ b/src/request.rs @@ -782,20 +782,20 @@ impl Builder { /// # use http::*; /// /// let mut req = Request::builder(); - /// assert_eq!(req.get_method(),Some(Method::GET)); + /// assert_eq!(req.method_ref(),Some(&Method::GET)); /// req.method("POST"); - /// assert_eq!(req.get_method(),Some(Method::POST)); + /// assert_eq!(req.method_ref(),Some(&Method::POST)); /// req.method("DELETE"); - /// assert_eq!(req.get_method(),Some(Method::DELETE)); + /// assert_eq!(req.method_ref(),Some(&Method::DELETE)); /// ``` - pub fn get_method(&self) -> Option + pub fn method_ref(&self) -> Option<&Method> { if self.err.is_some() { return None } - match &self.head { - &Some(ref head) => Some(head.method.clone()), - &None => None + match self.head { + Some(ref head) => Some(&head.method), + None => None } } @@ -837,19 +837,19 @@ impl Builder { /// # use http::*; /// /// let mut req = Request::builder(); - /// assert_eq!(req.get_uri().unwrap().to_string(), "/" ); + /// assert_eq!(req.uri_ref().unwrap().to_string(), "/" ); /// req.uri("https://www.rust-lang.org/"); - /// assert_eq!(req.get_uri().unwrap().to_string(), "https://www.rust-lang.org/" ); + /// assert_eq!(req.uri_ref().unwrap().to_string(), "https://www.rust-lang.org/" ); /// ``` - pub fn get_uri(&self) -> Option + pub fn uri_ref(&self) -> Option<&Uri> { if self.err.is_some() { return None; } - match &self.head + match self.head { - &Some(ref head) => Some(head.uri.clone()), - &None => None + Some(ref head) => Some(&head.uri), + None => None } } @@ -925,18 +925,18 @@ impl Builder { /// let mut req = Request::builder(); /// req.header("Accept", "text/html") /// .header("X-Custom-Foo", "bar"); - /// let headers = req.get_headers().unwrap(); + /// let headers = req.headers_ref().unwrap(); /// assert_eq!( headers["Accept"], "text/html" ); /// assert_eq!( headers["X-Custom-Foo"], "bar" ); /// ``` - pub fn get_headers(&self ) -> Option> { + pub fn headers_ref(&self) -> Option<&HeaderMap> { if self.err.is_some() { return None; } - match &self.head + match self.head { - &Some(ref head) => Some(head.headers.clone()), - &None => None + Some(ref head) => Some(&head.headers), + None => None } }