10
10
//!
11
11
//! let qs = QueryString::new()
12
12
//! .with_value("q", "🍎 apple")
13
+ //! .with_value("tasty", true)
13
14
//! .with_opt_value("color", None::<String>)
14
15
//! .with_opt_value("category", Some("fruits and vegetables?"));
15
16
//!
16
17
//! assert_eq!(
17
18
//! 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?"
19
20
//! );
20
21
//! ```
21
22
@@ -80,17 +81,18 @@ impl QueryString {
80
81
///
81
82
/// let qs = QueryString::new()
82
83
/// .with_value("q", "🍎 apple")
83
- /// .with_value("category", "fruits and vegetables");
84
+ /// .with_value("category", "fruits and vegetables")
85
+ /// .with_value("answer", 42);
84
86
///
85
87
/// assert_eq!(
86
88
/// 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 "
88
90
/// );
89
91
/// ```
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 {
91
93
self . pairs . push ( Kvp {
92
- key : key. into ( ) ,
93
- value : value. into ( ) ,
94
+ key : key. to_string ( ) ,
95
+ value : value. to_string ( ) ,
94
96
} ) ;
95
97
self
96
98
}
@@ -105,18 +107,15 @@ impl QueryString {
105
107
/// let qs = QueryString::new()
106
108
/// .with_opt_value("q", Some("🍎 apple"))
107
109
/// .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));
109
112
///
110
113
/// assert_eq!(
111
114
/// 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 "
113
116
/// );
114
117
/// ```
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 {
120
119
if let Some ( value) = value {
121
120
self . with_value ( key, value)
122
121
} else {
@@ -140,10 +139,10 @@ impl QueryString {
140
139
/// "https://example.com/?q=apple&category=fruits%20and%20vegetables"
141
140
/// );
142
141
/// ```
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 {
144
143
self . pairs . push ( Kvp {
145
- key : key. into ( ) ,
146
- value : value. into ( ) ,
144
+ key : key. to_string ( ) ,
145
+ value : value. to_string ( ) ,
147
146
} ) ;
148
147
self
149
148
}
@@ -164,11 +163,7 @@ impl QueryString {
164
163
/// "https://example.com/?q=%F0%9F%8D%8E%20apple"
165
164
/// );
166
165
/// ```
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 {
172
167
if let Some ( value) = value {
173
168
self . push ( key, value)
174
169
} else {
@@ -274,12 +269,14 @@ mod tests {
274
269
fn test_simple ( ) {
275
270
let qs = QueryString :: new ( )
276
271
. 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 ) ;
278
275
assert_eq ! (
279
276
qs. to_string( ) ,
280
- "?q=apple???&category=fruits%20and%20vegetables"
277
+ "?q=apple???&category=fruits%20and%20vegetables&tasty=true&weight=99.9 "
281
278
) ;
282
- assert_eq ! ( qs. len( ) , 2 ) ;
279
+ assert_eq ! ( qs. len( ) , 4 ) ;
283
280
assert ! ( !qs. is_empty( ) ) ;
284
281
}
285
282
@@ -307,12 +304,14 @@ mod tests {
307
304
let qs = QueryString :: new ( )
308
305
. with_value ( "q" , "celery" )
309
306
. 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 ) ) ;
311
310
assert_eq ! (
312
311
qs. to_string( ) ,
313
- "?q=celery&category=fruits%20and%20vegetables"
312
+ "?q=celery&category=fruits%20and%20vegetables&tasty=true&weight=99.9 "
314
313
) ;
315
- assert_eq ! ( qs. len( ) , 2 ) ; // not three !
314
+ assert_eq ! ( qs. len( ) , 4 ) ; // not five !
316
315
}
317
316
318
317
#[ test]
0 commit comments