15
15
//! assert!(fragment.is_none());
16
16
//! ```
17
17
18
- #[ macro_use] extern crate matches;
18
+ #[ macro_use]
19
+ extern crate matches;
19
20
20
21
macro_rules! require {
21
22
( $condition: expr) => {
22
23
if !$condition {
23
- return None
24
+ return None ;
24
25
}
25
- }
26
+ } ;
26
27
}
27
28
28
29
pub mod forgiving_base64;
@@ -53,7 +54,11 @@ impl<'a> DataUrl<'a> {
53
54
54
55
let ( mime_type, base64) = parse_header ( from_colon_to_comma) ;
55
56
56
- Ok ( DataUrl { mime_type, base64, encoded_body_plus_fragment } )
57
+ Ok ( DataUrl {
58
+ mime_type,
59
+ base64,
60
+ encoded_body_plus_fragment,
61
+ } )
57
62
}
58
63
59
64
pub fn mime_type ( & self ) -> & mime:: Mime {
@@ -62,9 +67,12 @@ impl<'a> DataUrl<'a> {
62
67
63
68
/// Streaming-decode the data URL’s body to `write_body_bytes`,
64
69
/// and return the URL’s fragment identifier if it has one.
65
- pub fn decode < F , E > ( & self , write_body_bytes : F )
66
- -> Result < Option < FragmentIdentifier < ' a > > , forgiving_base64:: DecodeError < E > >
67
- where F : FnMut ( & [ u8 ] ) -> Result < ( ) , E >
70
+ pub fn decode < F , E > (
71
+ & self ,
72
+ write_body_bytes : F ,
73
+ ) -> Result < Option < FragmentIdentifier < ' a > > , forgiving_base64:: DecodeError < E > >
74
+ where
75
+ F : FnMut ( & [ u8 ] ) -> Result < ( ) , E > ,
68
76
{
69
77
if self . base64 {
70
78
decode_with_base64 ( self . encoded_body_plus_fragment , write_body_bytes)
@@ -75,9 +83,9 @@ impl<'a> DataUrl<'a> {
75
83
}
76
84
77
85
/// Return the decoded body, and the URL’s fragment identifier if it has one.
78
- pub fn decode_to_vec ( & self )
79
- -> Result < ( Vec < u8 > , Option < FragmentIdentifier < ' a > > ) , forgiving_base64 :: InvalidBase64 >
80
- {
86
+ pub fn decode_to_vec (
87
+ & self ,
88
+ ) -> Result < ( Vec < u8 > , Option < FragmentIdentifier < ' a > > ) , forgiving_base64 :: InvalidBase64 > {
81
89
let mut body = Vec :: new ( ) ;
82
90
let fragment = self . decode ( |bytes| Ok ( body. extend_from_slice ( bytes) ) ) ?;
83
91
Ok ( ( body, fragment) )
@@ -100,7 +108,7 @@ impl<'a> FragmentIdentifier<'a> {
100
108
percent_encode ( byte, & mut string)
101
109
}
102
110
// Printable ASCII
103
- _ => string. push ( byte as char )
111
+ _ => string. push ( byte as char ) ,
104
112
}
105
113
}
106
114
string
@@ -125,7 +133,9 @@ fn pretend_parse_data_url(input: &str) -> Option<&str> {
125
133
let mut bytes = left_trimmed. bytes ( ) ;
126
134
{
127
135
// Ignore ASCII tabs or newlines like the URL parser would
128
- let mut iter = bytes. by_ref ( ) . filter ( |& byte| !matches ! ( byte, b'\t' | b'\n' | b'\r' ) ) ;
136
+ let mut iter = bytes
137
+ . by_ref ( )
138
+ . filter ( |& byte| !matches ! ( byte, b'\t' | b'\n' | b'\r' ) ) ;
129
139
require ! ( iter. next( ) ?. to_ascii_lowercase( ) == b'd' ) ;
130
140
require ! ( iter. next( ) ?. to_ascii_lowercase( ) == b'a' ) ;
131
141
require ! ( iter. next( ) ?. to_ascii_lowercase( ) == b't' ) ;
@@ -142,10 +152,10 @@ fn pretend_parse_data_url(input: &str) -> Option<&str> {
142
152
fn find_comma_before_fragment ( after_colon : & str ) -> Option < ( & str , & str ) > {
143
153
for ( i, byte) in after_colon. bytes ( ) . enumerate ( ) {
144
154
if byte == b',' {
145
- return Some ( ( & after_colon[ ..i] , & after_colon[ i + 1 ..] ) )
155
+ return Some ( ( & after_colon[ ..i] , & after_colon[ i + 1 ..] ) ) ;
146
156
}
147
157
if byte == b'#' {
148
- break
158
+ break ;
149
159
}
150
160
}
151
161
None
@@ -187,18 +197,16 @@ fn parse_header(from_colon_to_comma: &str) -> (mime::Mime, bool) {
187
197
}
188
198
189
199
// Printable ASCII
190
- _ => string. push ( byte as char )
200
+ _ => string. push ( byte as char ) ,
191
201
}
192
202
}
193
203
194
204
// FIXME: does Mime::from_str match the MIME Sniffing Standard’s parsing algorithm?
195
205
// <https://mimesniff.spec.whatwg.org/#parse-a-mime-type>
196
- let mime_type = string. parse ( ) . unwrap_or_else ( |_| {
197
- mime:: Mime {
198
- type_ : String :: from ( "text" ) ,
199
- subtype : String :: from ( "plain" ) ,
200
- parameters : vec ! [ ( String :: from( "charset" ) , String :: from( "US-ASCII" ) ) ] ,
201
- }
206
+ let mime_type = string. parse ( ) . unwrap_or_else ( |_| mime:: Mime {
207
+ type_ : String :: from ( "text" ) ,
208
+ subtype : String :: from ( "plain" ) ,
209
+ parameters : vec ! [ ( String :: from( "charset" ) , String :: from( "US-ASCII" ) ) ] ,
202
210
} ) ;
203
211
204
212
( mime_type, base64)
@@ -209,7 +217,9 @@ fn remove_base64_suffix(s: &str) -> Option<&str> {
209
217
let mut bytes = s. bytes ( ) ;
210
218
{
211
219
// Ignore ASCII tabs or newlines like the URL parser would
212
- let iter = bytes. by_ref ( ) . filter ( |& byte| !matches ! ( byte, b'\t' | b'\n' | b'\r' ) ) ;
220
+ let iter = bytes
221
+ . by_ref ( )
222
+ . filter ( |& byte| !matches ! ( byte, b'\t' | b'\n' | b'\r' ) ) ;
213
223
214
224
// Search from the end
215
225
let mut iter = iter. rev ( ) ;
@@ -240,9 +250,12 @@ fn percent_encode(byte: u8, string: &mut String) {
240
250
/// Anything that would have been UTF-8 percent-encoded by the URL parser
241
251
/// would be percent-decoded here.
242
252
/// We skip that round-trip and pass it through unchanged.
243
- fn decode_without_base64 < F , E > ( encoded_body_plus_fragment : & str , mut write_bytes : F )
244
- -> Result < Option < FragmentIdentifier > , E >
245
- where F : FnMut ( & [ u8 ] ) -> Result < ( ) , E >
253
+ fn decode_without_base64 < F , E > (
254
+ encoded_body_plus_fragment : & str ,
255
+ mut write_bytes : F ,
256
+ ) -> Result < Option < FragmentIdentifier > , E >
257
+ where
258
+ F : FnMut ( & [ u8 ] ) -> Result < ( ) , E > ,
246
259
{
247
260
let bytes = encoded_body_plus_fragment. as_bytes ( ) ;
248
261
let mut slice_start = 0 ;
@@ -275,11 +288,11 @@ fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes
275
288
b'#' => {
276
289
let fragment_start = i + 1 ;
277
290
let fragment = & encoded_body_plus_fragment[ fragment_start..] ;
278
- return Ok ( Some ( FragmentIdentifier ( fragment) ) )
291
+ return Ok ( Some ( FragmentIdentifier ( fragment) ) ) ;
279
292
}
280
293
281
294
// Ignore over '\t' | '\n' | '\r'
282
- _ => slice_start = i + 1
295
+ _ => slice_start = i + 1 ,
283
296
}
284
297
}
285
298
}
@@ -290,9 +303,12 @@ fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes
290
303
/// `decode_without_base64()` composed with
291
304
/// <https://infra.spec.whatwg.org/#isomorphic-decode> composed with
292
305
/// <https://infra.spec.whatwg.org/#forgiving-base64-decode>.
293
- fn decode_with_base64 < F , E > ( encoded_body_plus_fragment : & str , write_bytes : F )
294
- -> Result < Option < FragmentIdentifier > , forgiving_base64:: DecodeError < E > >
295
- where F : FnMut ( & [ u8 ] ) -> Result < ( ) , E >
306
+ fn decode_with_base64 < F , E > (
307
+ encoded_body_plus_fragment : & str ,
308
+ write_bytes : F ,
309
+ ) -> Result < Option < FragmentIdentifier > , forgiving_base64:: DecodeError < E > >
310
+ where
311
+ F : FnMut ( & [ u8 ] ) -> Result < ( ) , E > ,
296
312
{
297
313
let mut decoder = forgiving_base64:: Decoder :: new ( write_bytes) ;
298
314
let fragment = decode_without_base64 ( encoded_body_plus_fragment, |bytes| decoder. feed ( bytes) ) ?;
0 commit comments