Skip to content

Commit 5876b29

Browse files
committed
Add a doctest
1 parent 6953b97 commit 5876b29

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/lib.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
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+
118
#[macro_use] extern crate matches;
219
pub extern crate mime;
320

@@ -7,16 +24,19 @@ pub struct DataUrl<'a> {
724
encoded_body_plus_fragment: &'a str,
825
}
926

27+
#[derive(Debug)]
1028
pub enum DataUrlError {
1129
NotADataUrl,
1230
NoComma,
1331
}
1432

33+
#[derive(Debug)]
1534
pub enum DecodeError<E> {
1635
InvalidBase64(InvalidBase64),
1736
WriteError(E),
1837
}
1938

39+
#[derive(Debug)]
2040
pub struct InvalidBase64(());
2141

2242
impl<E> From<InvalidBase64> for DecodeError<E> {
@@ -25,7 +45,7 @@ impl<E> From<InvalidBase64> for DecodeError<E> {
2545

2646
impl<'a> DataUrl<'a> {
2747
/// <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.
2949
pub fn process(input: &'a str) -> Result<Self, DataUrlError> {
3050
use DataUrlError::*;
3151

@@ -44,7 +64,7 @@ impl<'a> DataUrl<'a> {
4464
}
4565

4666
/// 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.
4868
pub fn decode<F, E>(&self, write_body_bytes: F)
4969
-> Result<Option<FragmentIdentifier<'a>>, DecodeError<E>>
5070
where F: FnMut(&[u8]) -> Result<(), E>
@@ -57,7 +77,7 @@ impl<'a> DataUrl<'a> {
5777
}
5878
}
5979

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.
6181
pub fn decode_to_vec(&self)
6282
-> Result<(Vec<u8>, Option<FragmentIdentifier<'a>>), InvalidBase64>
6383
{

0 commit comments

Comments
 (0)