@@ -6,14 +6,15 @@ use std::ops::Index;
66use std:: pin:: Pin ;
77use std:: task:: { Context , Poll } ;
88
9+ #[ cfg( feature = "serde" ) ]
910use crate :: convert:: { DeserializeOwned , Serialize } ;
1011use crate :: headers:: {
1112 self , HeaderName , HeaderValue , HeaderValues , Headers , Names , ToHeaderValues , Values ,
1213 CONTENT_TYPE ,
1314} ;
1415use crate :: mime:: Mime ;
1516use crate :: trailers:: { self , Trailers } ;
16- use crate :: { Body , Extensions , Method , StatusCode , Url , Version } ;
17+ use crate :: { Body , Extensions , Method , Url , Version } ;
1718
1819pin_project_lite:: pin_project! {
1920 /// An HTTP request.
@@ -349,6 +350,7 @@ impl Request {
349350 /// use http_types::{Body, Request};
350351 ///
351352 /// #[derive(Debug, Serialize, Deserialize)]
353+ /// # #[serde(crate = "serde_crate")]
352354 /// struct Cat {
353355 /// name: String,
354356 /// }
@@ -363,6 +365,7 @@ impl Request {
363365 /// assert_eq!(&cat.name, "chashu");
364366 /// # Ok(()) }) }
365367 /// ```
368+ #[ cfg( feature = "serde" ) ]
366369 pub async fn body_json < T : DeserializeOwned > ( & mut self ) -> crate :: Result < T > {
367370 let body = self . take_body ( ) ;
368371 body. into_json ( ) . await
@@ -383,6 +386,7 @@ impl Request {
383386 /// use http_types::{Body, Request};
384387 ///
385388 /// #[derive(Debug, Serialize, Deserialize)]
389+ /// # #[serde(crate = "serde_crate")]
386390 /// struct Cat {
387391 /// name: String,
388392 /// }
@@ -397,6 +401,7 @@ impl Request {
397401 /// assert_eq!(&cat.name, "chashu");
398402 /// # Ok(()) }) }
399403 /// ```
404+ #[ cfg( feature = "serde" ) ]
400405 pub async fn body_form < T : DeserializeOwned > ( & mut self ) -> crate :: Result < T > {
401406 let body = self . take_body ( ) ;
402407 body. into_form ( ) . await
@@ -616,6 +621,7 @@ impl Request {
616621 /// use std::collections::HashMap;
617622 ///
618623 /// #[derive(Deserialize)]
624+ /// # #[serde(crate = "serde_crate")]
619625 /// struct Index {
620626 /// page: u32,
621627 /// selections: HashMap<String, String>,
@@ -627,15 +633,16 @@ impl Request {
627633 /// assert_eq!(selections["width"], "narrow");
628634 /// assert_eq!(selections["height"], "tall");
629635 /// ```
630- pub fn query < T : serde:: de:: DeserializeOwned > ( & self ) -> crate :: Result < T > {
636+ #[ cfg( feature = "serde" ) ]
637+ pub fn query < T : serde_crate:: de:: DeserializeOwned > ( & self ) -> crate :: Result < T > {
631638 // Default to an empty query string if no query parameter has been specified.
632639 // This allows successful deserialisation of structs where all fields are optional
633640 // when none of those fields has actually been passed by the caller.
634641 let query = self . url ( ) . query ( ) . unwrap_or ( "" ) ;
635642 serde_qs:: from_str ( query) . map_err ( |e| {
636643 // Return the displayable version of the deserialisation error to the caller
637644 // for easier debugging.
638- crate :: Error :: from_str ( StatusCode :: BadRequest , format ! ( "{}" , e) )
645+ crate :: Error :: from_str ( crate :: StatusCode :: BadRequest , format ! ( "{}" , e) )
639646 } )
640647 }
641648
@@ -649,6 +656,7 @@ impl Request {
649656 /// use std::collections::HashMap;
650657 ///
651658 /// #[derive(Serialize)]
659+ /// # #[serde(crate = "serde_crate")]
652660 /// struct Index {
653661 /// page: u32,
654662 /// topics: Vec<&'static str>,
@@ -659,9 +667,10 @@ impl Request {
659667 /// req.set_query(&query).unwrap();
660668 /// assert_eq!(req.url().query(), Some("page=2&topics[0]=rust&topics[1]=crabs&topics[2]=crustaceans"));
661669 /// ```
670+ #[ cfg( feature = "serde" ) ]
662671 pub fn set_query ( & mut self , query : & impl Serialize ) -> crate :: Result < ( ) > {
663672 let query = serde_qs:: to_string ( query)
664- . map_err ( |e| crate :: Error :: from_str ( StatusCode :: BadRequest , format ! ( "{}" , e) ) ) ?;
673+ . map_err ( |e| crate :: Error :: from_str ( crate :: StatusCode :: BadRequest , format ! ( "{}" , e) ) ) ?;
665674 self . url . set_query ( Some ( & query) ) ;
666675 Ok ( ( ) )
667676 }
0 commit comments