1515//! assert!(fragment.is_none());
1616//! ```
1717
18- #[ macro_use] extern crate matches;
18+ #[ macro_use]
19+ extern crate matches;
1920
2021macro_rules! require {
2122 ( $condition: expr) => {
2223 if !$condition {
23- return None
24+ return None ;
2425 }
25- }
26+ } ;
2627}
2728
2829pub mod forgiving_base64;
@@ -53,7 +54,11 @@ impl<'a> DataUrl<'a> {
5354
5455 let ( mime_type, base64) = parse_header ( from_colon_to_comma) ;
5556
56- Ok ( DataUrl { mime_type, base64, encoded_body_plus_fragment } )
57+ Ok ( DataUrl {
58+ mime_type,
59+ base64,
60+ encoded_body_plus_fragment,
61+ } )
5762 }
5863
5964 pub fn mime_type ( & self ) -> & mime:: Mime {
@@ -62,9 +67,12 @@ impl<'a> DataUrl<'a> {
6267
6368 /// Streaming-decode the data URL’s body to `write_body_bytes`,
6469 /// 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 > ,
6876 {
6977 if self . base64 {
7078 decode_with_base64 ( self . encoded_body_plus_fragment , write_body_bytes)
@@ -75,9 +83,9 @@ impl<'a> DataUrl<'a> {
7583 }
7684
7785 /// 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 > {
8189 let mut body = Vec :: new ( ) ;
8290 let fragment = self . decode ( |bytes| Ok ( body. extend_from_slice ( bytes) ) ) ?;
8391 Ok ( ( body, fragment) )
@@ -100,7 +108,7 @@ impl<'a> FragmentIdentifier<'a> {
100108 percent_encode ( byte, & mut string)
101109 }
102110 // Printable ASCII
103- _ => string. push ( byte as char )
111+ _ => string. push ( byte as char ) ,
104112 }
105113 }
106114 string
@@ -125,7 +133,9 @@ fn pretend_parse_data_url(input: &str) -> Option<&str> {
125133 let mut bytes = left_trimmed. bytes ( ) ;
126134 {
127135 // 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' ) ) ;
129139 require ! ( iter. next( ) ?. to_ascii_lowercase( ) == b'd' ) ;
130140 require ! ( iter. next( ) ?. to_ascii_lowercase( ) == b'a' ) ;
131141 require ! ( iter. next( ) ?. to_ascii_lowercase( ) == b't' ) ;
@@ -142,10 +152,10 @@ fn pretend_parse_data_url(input: &str) -> Option<&str> {
142152fn find_comma_before_fragment ( after_colon : & str ) -> Option < ( & str , & str ) > {
143153 for ( i, byte) in after_colon. bytes ( ) . enumerate ( ) {
144154 if byte == b',' {
145- return Some ( ( & after_colon[ ..i] , & after_colon[ i + 1 ..] ) )
155+ return Some ( ( & after_colon[ ..i] , & after_colon[ i + 1 ..] ) ) ;
146156 }
147157 if byte == b'#' {
148- break
158+ break ;
149159 }
150160 }
151161 None
@@ -187,18 +197,16 @@ fn parse_header(from_colon_to_comma: &str) -> (mime::Mime, bool) {
187197 }
188198
189199 // Printable ASCII
190- _ => string. push ( byte as char )
200+ _ => string. push ( byte as char ) ,
191201 }
192202 }
193203
194204 // FIXME: does Mime::from_str match the MIME Sniffing Standard’s parsing algorithm?
195205 // <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" ) ) ] ,
202210 } ) ;
203211
204212 ( mime_type, base64)
@@ -209,7 +217,9 @@ fn remove_base64_suffix(s: &str) -> Option<&str> {
209217 let mut bytes = s. bytes ( ) ;
210218 {
211219 // 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' ) ) ;
213223
214224 // Search from the end
215225 let mut iter = iter. rev ( ) ;
@@ -240,9 +250,12 @@ fn percent_encode(byte: u8, string: &mut String) {
240250/// Anything that would have been UTF-8 percent-encoded by the URL parser
241251/// would be percent-decoded here.
242252/// 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 > ,
246259{
247260 let bytes = encoded_body_plus_fragment. as_bytes ( ) ;
248261 let mut slice_start = 0 ;
@@ -275,11 +288,11 @@ fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes
275288 b'#' => {
276289 let fragment_start = i + 1 ;
277290 let fragment = & encoded_body_plus_fragment[ fragment_start..] ;
278- return Ok ( Some ( FragmentIdentifier ( fragment) ) )
291+ return Ok ( Some ( FragmentIdentifier ( fragment) ) ) ;
279292 }
280293
281294 // Ignore over '\t' | '\n' | '\r'
282- _ => slice_start = i + 1
295+ _ => slice_start = i + 1 ,
283296 }
284297 }
285298 }
@@ -290,9 +303,12 @@ fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes
290303/// `decode_without_base64()` composed with
291304/// <https://infra.spec.whatwg.org/#isomorphic-decode> composed with
292305/// <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 > ,
296312{
297313 let mut decoder = forgiving_base64:: Decoder :: new ( write_bytes) ;
298314 let fragment = decode_without_base64 ( encoded_body_plus_fragment, |bytes| decoder. feed ( bytes) ) ?;
0 commit comments