1
+ //! Processing of `data:` URLs according to the Fetch Standard:
2
+ //! <https://fetch.spec.whatwg.org/#data-urls>
3
+ //! but starting from a string rather than a parsed URL to avoid extra copies.
4
+ //!
5
+ //! ```rust
6
+ //! use data_url::{DataUrl, mime};
7
+ //!
8
+ //! let url = DataUrl::process("data:,Hello%20World!").unwrap();
9
+ //! let (body, fragment) = url.decode_to_vec().unwrap();
10
+ //!
11
+ //! assert_eq!(url.mime_type().type_(), mime::TEXT);
12
+ //! assert_eq!(url.mime_type().subtype(), mime::PLAIN);
13
+ //! assert_eq!(url.mime_type().get_param(mime::CHARSET).unwrap(), "US-ASCII");
14
+ //! assert_eq!(body, b"Hello World!");
15
+ //! assert!(fragment.is_none());
16
+ //! ```
17
+
1
18
#[ macro_use] extern crate matches;
2
19
pub extern crate mime;
3
20
@@ -7,16 +24,19 @@ pub struct DataUrl<'a> {
7
24
encoded_body_plus_fragment : & ' a str ,
8
25
}
9
26
27
+ #[ derive( Debug ) ]
10
28
pub enum DataUrlError {
11
29
NotADataUrl ,
12
30
NoComma ,
13
31
}
14
32
33
+ #[ derive( Debug ) ]
15
34
pub enum DecodeError < E > {
16
35
InvalidBase64 ( InvalidBase64 ) ,
17
36
WriteError ( E ) ,
18
37
}
19
38
39
+ #[ derive( Debug ) ]
20
40
pub struct InvalidBase64 ( ( ) ) ;
21
41
22
42
impl < E > From < InvalidBase64 > for DecodeError < E > {
@@ -25,7 +45,7 @@ impl<E> From<InvalidBase64> for DecodeError<E> {
25
45
26
46
impl < ' a > DataUrl < ' a > {
27
47
/// <https://fetch.spec.whatwg.org/#data-url-processor>
28
- /// but starting from a string rather than a Url, to avoid extra string copies.
48
+ /// but starting from a string rather than a parsed ` Url` , to avoid extra string copies.
29
49
pub fn process ( input : & ' a str ) -> Result < Self , DataUrlError > {
30
50
use DataUrlError :: * ;
31
51
@@ -44,7 +64,7 @@ impl<'a> DataUrl<'a> {
44
64
}
45
65
46
66
/// Streaming-decode the data URL’s body to `write_body_bytes`,
47
- /// and return the URL’s fragment identifier is returned if it has one.
67
+ /// and return the URL’s fragment identifier if it has one.
48
68
pub fn decode < F , E > ( & self , write_body_bytes : F )
49
69
-> Result < Option < FragmentIdentifier < ' a > > , DecodeError < E > >
50
70
where F : FnMut ( & [ u8 ] ) -> Result < ( ) , E >
@@ -57,7 +77,7 @@ impl<'a> DataUrl<'a> {
57
77
}
58
78
}
59
79
60
- /// Return the decoded body and the URL’s fragment identifier
80
+ /// Return the decoded body, and the URL’s fragment identifier if it has one.
61
81
pub fn decode_to_vec ( & self )
62
82
-> Result < ( Vec < u8 > , Option < FragmentIdentifier < ' a > > ) , InvalidBase64 >
63
83
{
0 commit comments