Skip to content

Commit 25bdb02

Browse files
committed
Add some some basic functions to Mime
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.
1 parent 68f151c commit 25bdb02

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-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: 31 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,20 @@ pub struct Mime {
1212
}
1313

1414
impl Mime {
15+
pub fn new(type_: &str, subtype: &str) -> Self {
16+
Self {
17+
type_: type_.into(),
18+
subtype: subtype.into(),
19+
parameters: vec![],
20+
}
21+
}
22+
23+
/// Return true if this [`Mime`] matches a given type and subtype, regardless
24+
/// of what parameters it has.
25+
pub fn matches(&self, type_: &str, subtype: &str) -> bool {
26+
self.type_ == type_ && self.subtype == subtype
27+
}
28+
1529
pub fn get_parameter<P>(&self, name: &P) -> Option<&str>
1630
where
1731
P: ?Sized + PartialEq<str>,
@@ -205,3 +219,19 @@ static IS_HTTP_TOKEN: [bool; 256] = byte_map![
205219
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
206220
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
207221
];
222+
223+
#[test]
224+
fn test_basic_mime() {
225+
let mime = Mime::new("text", "plain");
226+
assert!(mime.matches("text", "plain"));
227+
228+
let cloned = mime.clone();
229+
assert!(cloned.matches("text", "plain"));
230+
231+
let mime = Mime {
232+
type_: "text".into(),
233+
subtype: "html".into(),
234+
parameters: vec![("one".into(), "two".into())],
235+
};
236+
assert!(mime.matches("text", "html"));
237+
}

0 commit comments

Comments
 (0)