@@ -81,13 +81,19 @@ impl<'a> DataUrl<'a> {
81
81
pub fn decode_to_vec ( & self )
82
82
-> Result < ( Vec < u8 > , Option < FragmentIdentifier < ' a > > ) , InvalidBase64 >
83
83
{
84
- enum Impossible { }
85
84
let mut body = Vec :: new ( ) ;
86
- let result = self . decode :: < _ , Impossible > ( |bytes| Ok ( body. extend_from_slice ( bytes) ) ) ;
87
- match result {
88
- Ok ( url_fragment) => Ok ( ( body, url_fragment) ) ,
89
- Err ( DecodeError :: InvalidBase64 ( e) ) => Err ( e) ,
90
- Err ( DecodeError :: WriteError ( e) ) => match e { }
85
+ let fragment = self . decode ( |bytes| Ok ( body. extend_from_slice ( bytes) ) ) ?;
86
+ Ok ( ( body, fragment) )
87
+ }
88
+ }
89
+
90
+ enum Impossible { }
91
+
92
+ impl From < DecodeError < Impossible > > for InvalidBase64 {
93
+ fn from ( e : DecodeError < Impossible > ) -> Self {
94
+ match e {
95
+ DecodeError :: InvalidBase64 ( e) => e,
96
+ DecodeError :: WriteError ( e) => match e { }
91
97
}
92
98
}
93
99
}
@@ -312,6 +318,19 @@ fn decode_with_base64<F, E>(encoded_body_plus_fragment: &str, write_bytes: F)
312
318
Ok ( fragment)
313
319
}
314
320
321
+ /// <https://infra.spec.whatwg.org/#forgiving-base64-decode>
322
+ ///
323
+ /// `input` is assumed to be in an ASCII-compatible encoding
324
+ pub fn forgiving_base64_decode_to_vec ( input : & [ u8 ] ) -> Result < Vec < u8 > , InvalidBase64 > {
325
+ let mut v = Vec :: new ( ) ;
326
+ {
327
+ let mut decoder = ForgivingBase64Decoder :: new ( |bytes| Ok ( v. extend_from_slice ( bytes) ) ) ;
328
+ decoder. feed ( input) ?;
329
+ decoder. finish ( ) ?;
330
+ }
331
+ Ok ( v)
332
+ }
333
+
315
334
/// <https://infra.spec.whatwg.org/#forgiving-base64-decode>
316
335
pub struct ForgivingBase64Decoder < F , E > where F : FnMut ( & [ u8 ] ) -> Result < ( ) , E > {
317
336
write_bytes : F ,
0 commit comments