Skip to content

Commit 9f6e92e

Browse files
authored
Add some some basic functions to Mime (#1047)
This adds `Clone`, `fn new()`, and `fn matches()` to `Mime`. Servo uses `data_url::Mime` in places as it supports charsets and these helpers make it a lot nicer to use. Signed-off-by: Martin Robinson <[email protected]>
1 parent 68f151c commit 9f6e92e

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

data-url/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ serde = { version = "1.0", default-features = false, features = ["alloc", "deriv
2323
serde_json = "1.0"
2424

2525
[lib]
26-
test = false
26+
test = true
2727

2828
[[test]]
2929
name = "wpt"

data-url/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use data_url::{DataUrl, mime};
1313
let url = DataUrl::process("data:,Hello%20World!").unwrap();
1414
let (body, fragment) = url.decode_to_vec().unwrap();
1515

16-
assert_eq!(url.mime_type().type_, "text");
17-
assert_eq!(url.mime_type().subtype, "plain");
16+
assert!(url.mime_type().is("text", "plain"));
1817
assert_eq!(url.mime_type().get_parameter("charset"), Some("US-ASCII"));
1918
assert_eq!(body, b"Hello World!");
2019
assert!(fragment.is_none());

data-url/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
//! let url = DataUrl::process("data:,Hello%20World!").unwrap();
99
//! let (body, fragment) = url.decode_to_vec().unwrap();
1010
//!
11-
//! assert_eq!(url.mime_type().type_, "text");
12-
//! assert_eq!(url.mime_type().subtype, "plain");
11+
//! assert!(url.mime_type().matches("text", "plain"));
1312
//! assert_eq!(url.mime_type().get_parameter("charset"), Some("US-ASCII"));
1413
//! assert_eq!(body, b"Hello World!");
1514
//! assert!(fragment.is_none());

data-url/src/mime.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::fmt::{self, Write};
33
use core::str::FromStr;
44

55
/// <https://mimesniff.spec.whatwg.org/#mime-type-representation>
6-
#[derive(Debug, PartialEq, Eq)]
6+
#[derive(Clone, Debug, PartialEq, Eq)]
77
pub struct Mime {
88
pub type_: String,
99
pub subtype: String,
@@ -12,6 +12,22 @@ pub struct Mime {
1212
}
1313

1414
impl Mime {
15+
/// Construct a new [`Mime`] with the given `type_` and `subtype` and an
16+
/// empty parameter list.
17+
pub fn new(type_: &str, subtype: &str) -> Self {
18+
Self {
19+
type_: type_.into(),
20+
subtype: subtype.into(),
21+
parameters: vec![],
22+
}
23+
}
24+
25+
/// Return true if this [`Mime`] matches a given type and subtype, regardless
26+
/// of what parameters it has.
27+
pub fn matches(&self, type_: &str, subtype: &str) -> bool {
28+
self.type_ == type_ && self.subtype == subtype
29+
}
30+
1531
pub fn get_parameter<P>(&self, name: &P) -> Option<&str>
1632
where
1733
P: ?Sized + PartialEq<str>,
@@ -205,3 +221,19 @@ static IS_HTTP_TOKEN: [bool; 256] = byte_map![
205221
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206222
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
207223
];
224+
225+
#[test]
226+
fn test_basic_mime() {
227+
let mime = Mime::new("text", "plain");
228+
assert!(mime.matches("text", "plain"));
229+
230+
let cloned = mime.clone();
231+
assert!(cloned.matches("text", "plain"));
232+
233+
let mime = Mime {
234+
type_: "text".into(),
235+
subtype: "html".into(),
236+
parameters: vec![("one".into(), "two".into())],
237+
};
238+
assert!(mime.matches("text", "html"));
239+
}

0 commit comments

Comments
 (0)