1010//!
1111//! let qs = QueryString::new()
1212//! .with_value("q", "🍎 apple")
13+ //! .with_value("tasty", true)
1314//! .with_opt_value("color", None::<String>)
1415//! .with_opt_value("category", Some("fruits and vegetables?"));
1516//!
1617//! assert_eq!(
1718//! format!("example.com/{qs}"),
18- //! "example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables?"
19+ //! "example.com/?q=%F0%9F%8D%8E%20apple&tasty=true& category=fruits%20and%20vegetables?"
1920//! );
2021//! ```
2122
@@ -80,17 +81,18 @@ impl QueryString {
8081 ///
8182 /// let qs = QueryString::new()
8283 /// .with_value("q", "🍎 apple")
83- /// .with_value("category", "fruits and vegetables");
84+ /// .with_value("category", "fruits and vegetables")
85+ /// .with_value("answer", 42);
8486 ///
8587 /// assert_eq!(
8688 /// format!("https://example.com/{qs}"),
87- /// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables"
89+ /// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables&answer=42 "
8890 /// );
8991 /// ```
90- pub fn with_value < K : Into < String > , V : Into < String > > ( mut self , key : K , value : V ) -> Self {
92+ pub fn with_value < K : ToString , V : ToString > ( mut self , key : K , value : V ) -> Self {
9193 self . pairs . push ( Kvp {
92- key : key. into ( ) ,
93- value : value. into ( ) ,
94+ key : key. to_string ( ) ,
95+ value : value. to_string ( ) ,
9496 } ) ;
9597 self
9698 }
@@ -105,18 +107,15 @@ impl QueryString {
105107 /// let qs = QueryString::new()
106108 /// .with_opt_value("q", Some("🍎 apple"))
107109 /// .with_opt_value("f", None::<String>)
108- /// .with_opt_value("category", Some("fruits and vegetables"));
110+ /// .with_opt_value("category", Some("fruits and vegetables"))
111+ /// .with_opt_value("works", Some(true));
109112 ///
110113 /// assert_eq!(
111114 /// format!("https://example.com/{qs}"),
112- /// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables"
115+ /// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables&works=true "
113116 /// );
114117 /// ```
115- pub fn with_opt_value < K : Into < String > , V : Into < String > > (
116- self ,
117- key : K ,
118- value : Option < V > ,
119- ) -> Self {
118+ pub fn with_opt_value < K : ToString , V : ToString > ( self , key : K , value : Option < V > ) -> Self {
120119 if let Some ( value) = value {
121120 self . with_value ( key, value)
122121 } else {
@@ -140,10 +139,10 @@ impl QueryString {
140139 /// "https://example.com/?q=apple&category=fruits%20and%20vegetables"
141140 /// );
142141 /// ```
143- pub fn push < K : Into < String > , V : Into < String > > ( & mut self , key : K , value : V ) -> & Self {
142+ pub fn push < K : ToString , V : ToString > ( & mut self , key : K , value : V ) -> & Self {
144143 self . pairs . push ( Kvp {
145- key : key. into ( ) ,
146- value : value. into ( ) ,
144+ key : key. to_string ( ) ,
145+ value : value. to_string ( ) ,
147146 } ) ;
148147 self
149148 }
@@ -164,11 +163,7 @@ impl QueryString {
164163 /// "https://example.com/?q=%F0%9F%8D%8E%20apple"
165164 /// );
166165 /// ```
167- pub fn push_opt < K : Into < String > , V : Into < String > > (
168- & mut self ,
169- key : K ,
170- value : Option < V > ,
171- ) -> & Self {
166+ pub fn push_opt < K : ToString , V : ToString > ( & mut self , key : K , value : Option < V > ) -> & Self {
172167 if let Some ( value) = value {
173168 self . push ( key, value)
174169 } else {
@@ -274,12 +269,14 @@ mod tests {
274269 fn test_simple ( ) {
275270 let qs = QueryString :: new ( )
276271 . with_value ( "q" , "apple???" )
277- . with_value ( "category" , "fruits and vegetables" ) ;
272+ . with_value ( "category" , "fruits and vegetables" )
273+ . with_value ( "tasty" , true )
274+ . with_value ( "weight" , 99.9 ) ;
278275 assert_eq ! (
279276 qs. to_string( ) ,
280- "?q=apple???&category=fruits%20and%20vegetables"
277+ "?q=apple???&category=fruits%20and%20vegetables&tasty=true&weight=99.9 "
281278 ) ;
282- assert_eq ! ( qs. len( ) , 2 ) ;
279+ assert_eq ! ( qs. len( ) , 4 ) ;
283280 assert ! ( !qs. is_empty( ) ) ;
284281 }
285282
@@ -307,12 +304,14 @@ mod tests {
307304 let qs = QueryString :: new ( )
308305 . with_value ( "q" , "celery" )
309306 . with_opt_value ( "taste" , None :: < String > )
310- . with_opt_value ( "category" , Some ( "fruits and vegetables" ) ) ;
307+ . with_opt_value ( "category" , Some ( "fruits and vegetables" ) )
308+ . with_opt_value ( "tasty" , Some ( true ) )
309+ . with_opt_value ( "weight" , Some ( 99.9 ) ) ;
311310 assert_eq ! (
312311 qs. to_string( ) ,
313- "?q=celery&category=fruits%20and%20vegetables"
312+ "?q=celery&category=fruits%20and%20vegetables&tasty=true&weight=99.9 "
314313 ) ;
315- assert_eq ! ( qs. len( ) , 2 ) ; // not three !
314+ assert_eq ! ( qs. len( ) , 4 ) ; // not five !
316315 }
317316
318317 #[ test]
0 commit comments