13
13
//! Converts between a string (such as an URL’s query string)
14
14
//! and a sequence of (name, value) pairs.
15
15
16
- use encoding:: { decode_utf8_lossy, EncodingOverride } ;
17
16
use percent_encoding:: { percent_decode, percent_encode_byte} ;
17
+ use query_encoding:: { self , decode_utf8_lossy, EncodingOverride } ;
18
18
use std:: borrow:: { Borrow , Cow } ;
19
- use std:: fmt;
20
19
use std:: str;
21
20
22
21
/// Convert a byte string in the `application/x-www-form-urlencoded` syntax
@@ -31,7 +30,7 @@ pub fn parse(input: &[u8]) -> Parse {
31
30
Parse { input : input }
32
31
}
33
32
/// The return type of `parse()`.
34
- #[ derive( Copy , Clone , Debug ) ]
33
+ #[ derive( Copy , Clone ) ]
35
34
pub struct Parse < ' a > {
36
35
input : & ' a [ u8 ] ,
37
36
}
@@ -91,7 +90,6 @@ impl<'a> Parse<'a> {
91
90
}
92
91
93
92
/// Like `Parse`, but yields pairs of `String` instead of pairs of `Cow<str>`.
94
- #[ derive( Debug ) ]
95
93
pub struct ParseIntoOwned < ' a > {
96
94
inner : Parse < ' a > ,
97
95
}
@@ -161,20 +159,10 @@ impl<'a> Iterator for ByteSerialize<'a> {
161
159
162
160
/// The [`application/x-www-form-urlencoded` serializer](
163
161
/// https://url.spec.whatwg.org/#concept-urlencoded-serializer).
164
- #[ derive( Debug ) ]
165
- pub struct Serializer < T : Target > {
162
+ pub struct Serializer < ' a , T : Target > {
166
163
target : Option < T > ,
167
164
start_position : usize ,
168
- encoding : EncodingOverride ,
169
- custom_encoding : Option < SilentDebug < Box < dyn FnMut ( & str ) -> Cow < [ u8 ] > > > > ,
170
- }
171
-
172
- struct SilentDebug < T > ( T ) ;
173
-
174
- impl < T > fmt:: Debug for SilentDebug < T > {
175
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
176
- f. write_str ( "…" )
177
- }
165
+ encoding : EncodingOverride < ' a > ,
178
166
}
179
167
180
168
pub trait Target {
@@ -227,7 +215,7 @@ impl<'a> Target for ::UrlQuery<'a> {
227
215
type Finished = & ' a mut :: Url ;
228
216
}
229
217
230
- impl < T : Target > Serializer < T > {
218
+ impl < ' a , T : Target > Serializer < ' a , T > {
231
219
/// Create a new `application/x-www-form-urlencoded` serializer for the given target.
232
220
///
233
221
/// If the target is non-empty,
@@ -246,8 +234,7 @@ impl<T: Target> Serializer<T> {
246
234
Serializer {
247
235
target : Some ( target) ,
248
236
start_position : start_position,
249
- encoding : EncodingOverride :: utf8 ( ) ,
250
- custom_encoding : None ,
237
+ encoding : None ,
251
238
}
252
239
}
253
240
@@ -260,18 +247,8 @@ impl<T: Target> Serializer<T> {
260
247
}
261
248
262
249
/// Set the character encoding to be used for names and values before percent-encoding.
263
- #[ cfg( feature = "query_encoding" ) ]
264
- pub fn encoding_override ( & mut self , new : Option < :: encoding:: EncodingRef > ) -> & mut Self {
265
- self . encoding = EncodingOverride :: from_opt_encoding ( new) . to_output_encoding ( ) ;
266
- self
267
- }
268
-
269
- /// Set the character encoding to be used for names and values before percent-encoding.
270
- pub fn custom_encoding_override < F > ( & mut self , encode : F ) -> & mut Self
271
- where
272
- F : FnMut ( & str ) -> Cow < [ u8 ] > + ' static ,
273
- {
274
- self . custom_encoding = Some ( SilentDebug ( Box :: new ( encode) ) ) ;
250
+ pub fn encoding_override ( & mut self , new : EncodingOverride < ' a > ) -> & mut Self {
251
+ self . encoding = new;
275
252
self
276
253
}
277
254
@@ -283,7 +260,6 @@ impl<T: Target> Serializer<T> {
283
260
string ( & mut self . target ) ,
284
261
self . start_position ,
285
262
self . encoding ,
286
- & mut self . custom_encoding ,
287
263
name,
288
264
value,
289
265
) ;
@@ -312,7 +288,6 @@ impl<T: Target> Serializer<T> {
312
288
string,
313
289
self . start_position ,
314
290
self . encoding ,
315
- & mut self . custom_encoding ,
316
291
k. as_ref ( ) ,
317
292
v. as_ref ( ) ,
318
293
) ;
@@ -321,26 +296,6 @@ impl<T: Target> Serializer<T> {
321
296
self
322
297
}
323
298
324
- /// Add a name/value pair whose name is `_charset_`
325
- /// and whose value is the character encoding’s name.
326
- /// (See the `encoding_override()` method.)
327
- ///
328
- /// Panics if called after `.finish()`.
329
- #[ cfg( feature = "query_encoding" ) ]
330
- pub fn append_charset ( & mut self ) -> & mut Self {
331
- assert ! (
332
- self . custom_encoding. is_none( ) ,
333
- "Cannot use both custom_encoding_override() and append_charset()"
334
- ) ;
335
- {
336
- let string = string ( & mut self . target ) ;
337
- append_separator_if_needed ( string, self . start_position ) ;
338
- string. push_str ( "_charset_=" ) ;
339
- string. push_str ( self . encoding . name ( ) ) ;
340
- }
341
- self
342
- }
343
-
344
299
/// If this serializer was constructed with a string, take and return that string.
345
300
///
346
301
/// ```rust
@@ -378,26 +333,15 @@ fn append_pair(
378
333
string : & mut String ,
379
334
start_position : usize ,
380
335
encoding : EncodingOverride ,
381
- custom_encoding : & mut Option < SilentDebug < Box < dyn FnMut ( & str ) -> Cow < [ u8 ] > > > > ,
382
336
name : & str ,
383
337
value : & str ,
384
338
) {
385
339
append_separator_if_needed ( string, start_position) ;
386
- append_encoded ( name, string, encoding, custom_encoding ) ;
340
+ append_encoded ( name, string, encoding) ;
387
341
string. push ( '=' ) ;
388
- append_encoded ( value, string, encoding, custom_encoding ) ;
342
+ append_encoded ( value, string, encoding) ;
389
343
}
390
344
391
- fn append_encoded (
392
- s : & str ,
393
- string : & mut String ,
394
- encoding : EncodingOverride ,
395
- custom_encoding : & mut Option < SilentDebug < Box < dyn FnMut ( & str ) -> Cow < [ u8 ] > > > > ,
396
- ) {
397
- let bytes = if let Some ( SilentDebug ( ref mut custom) ) = * custom_encoding {
398
- custom ( s)
399
- } else {
400
- encoding. encode ( s. into ( ) )
401
- } ;
402
- string. extend ( byte_serialize ( & bytes) ) ;
345
+ fn append_encoded ( s : & str , string : & mut String , encoding : EncodingOverride ) {
346
+ string. extend ( byte_serialize ( & query_encoding:: encode ( encoding, s. into ( ) ) ) )
403
347
}
0 commit comments