@@ -23,6 +23,12 @@ impl<E> From<InvalidBase64> for DecodeError<E> {
23
23
fn from ( e : InvalidBase64 ) -> Self { DecodeError :: InvalidBase64 ( e) }
24
24
}
25
25
26
+ /// The URL’s fragment identifier (after `#`) encoded as in the original input.
27
+ ///
28
+ /// It needs to be either percent-encoded to obtain the same string as in a parsed URL,
29
+ /// or percent-decoded to interpret it as text.
30
+ pub struct UrlFragmentIdentifier < ' a > ( pub & ' a str ) ;
31
+
26
32
impl < ' a > DataUrl < ' a > {
27
33
/// <https://fetch.spec.whatwg.org/#data-url-processor>
28
34
/// but starting from a string rather than a Url, to avoid extra string copies.
@@ -45,11 +51,8 @@ impl<'a> DataUrl<'a> {
45
51
46
52
/// Streaming-decode the data URL’s body to `write_body_bytes`,
47
53
/// and return the URL’s fragment identifier is returned if it has one.
48
- ///
49
- /// The fragment is represented as in the origin input.
50
- /// It needs to be either percent-encoded to obtain the same string as in a parsed URL,
51
- /// or percent-decoded to interpret it as text.
52
- pub fn decode < F , E > ( & self , write_body_bytes : F ) -> Result < Option < & ' a str > , DecodeError < E > >
54
+ pub fn decode < F , E > ( & self , write_body_bytes : F )
55
+ -> Result < Option < UrlFragmentIdentifier < ' a > > , DecodeError < E > >
53
56
where F : FnMut ( & [ u8 ] ) -> Result < ( ) , E >
54
57
{
55
58
if self . base64 {
@@ -60,7 +63,10 @@ impl<'a> DataUrl<'a> {
60
63
}
61
64
}
62
65
63
- pub fn decode_to_vec ( & self ) -> Result < ( Vec < u8 > , Option < & str > ) , InvalidBase64 > {
66
+ /// Return the decoded body and the URL’s fragment identifier
67
+ pub fn decode_to_vec ( & self )
68
+ -> Result < ( Vec < u8 > , Option < UrlFragmentIdentifier < ' a > > ) , InvalidBase64 >
69
+ {
64
70
enum Impossible { }
65
71
let mut body = Vec :: new ( ) ;
66
72
let result = self . decode :: < _ , Impossible > ( |bytes| Ok ( body. extend_from_slice ( bytes) ) ) ;
@@ -183,7 +189,7 @@ fn remove_suffix<'a, Eq>(haystack: &'a str, needle: &str, eq: Eq) -> Option<&'a
183
189
/// would be percent-decoded here.
184
190
/// We skip that round-trip and pass it through unchanged.
185
191
fn decode_without_base64 < F , E > ( encoded_body_plus_fragment : & str , mut write_bytes : F )
186
- -> Result < Option < & str > , E >
192
+ -> Result < Option < UrlFragmentIdentifier > , E >
187
193
where F : FnMut ( & [ u8 ] ) -> Result < ( ) , E >
188
194
{
189
195
let bytes = encoded_body_plus_fragment. as_bytes ( ) ;
@@ -216,7 +222,8 @@ fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes
216
222
217
223
b'#' => {
218
224
let fragment_start = i + 1 ;
219
- return Ok ( Some ( & encoded_body_plus_fragment[ fragment_start..] ) )
225
+ let fragment = & encoded_body_plus_fragment[ fragment_start..] ;
226
+ return Ok ( Some ( UrlFragmentIdentifier ( fragment) ) )
220
227
}
221
228
222
229
// Ignore over '\t' | '\n' | '\r'
@@ -232,7 +239,7 @@ fn decode_without_base64<F, E>(encoded_body_plus_fragment: &str, mut write_bytes
232
239
/// <https://infra.spec.whatwg.org/#isomorphic-decode> composed with
233
240
/// <https://infra.spec.whatwg.org/#forgiving-base64-decode>.
234
241
fn decode_with_base64 < F , E > ( encoded_body_plus_fragment : & str , mut write_bytes : F )
235
- -> Result < Option < & str > , DecodeError < E > >
242
+ -> Result < Option < UrlFragmentIdentifier > , DecodeError < E > >
236
243
where F : FnMut ( & [ u8 ] ) -> Result < ( ) , E >
237
244
{
238
245
let mut bit_buffer: u32 = 0 ;
0 commit comments