Skip to content

Add some some basic functions to Mime #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data-url/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ serde = { version = "1.0", default-features = false, features = ["alloc", "deriv
serde_json = "1.0"

[lib]
test = false
test = true

[[test]]
name = "wpt"
Expand Down
3 changes: 1 addition & 2 deletions data-url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use data_url::{DataUrl, mime};
let url = DataUrl::process("data:,Hello%20World!").unwrap();
let (body, fragment) = url.decode_to_vec().unwrap();

assert_eq!(url.mime_type().type_, "text");
assert_eq!(url.mime_type().subtype, "plain");
assert!(url.mime_type().is("text", "plain"));
assert_eq!(url.mime_type().get_parameter("charset"), Some("US-ASCII"));
assert_eq!(body, b"Hello World!");
assert!(fragment.is_none());
Expand Down
3 changes: 1 addition & 2 deletions data-url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
//! let url = DataUrl::process("data:,Hello%20World!").unwrap();
//! let (body, fragment) = url.decode_to_vec().unwrap();
//!
//! assert_eq!(url.mime_type().type_, "text");
//! assert_eq!(url.mime_type().subtype, "plain");
//! assert!(url.mime_type().matches("text", "plain"));
//! assert_eq!(url.mime_type().get_parameter("charset"), Some("US-ASCII"));
//! assert_eq!(body, b"Hello World!");
//! assert!(fragment.is_none());
Expand Down
32 changes: 31 additions & 1 deletion data-url/src/mime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::fmt::{self, Write};
use core::str::FromStr;

/// <https://mimesniff.spec.whatwg.org/#mime-type-representation>
#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mime {
pub type_: String,
pub subtype: String,
Expand All @@ -12,6 +12,20 @@ pub struct Mime {
}

impl Mime {
pub fn new(type_: &str, subtype: &str) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A line of docs here? Just mention that this constructs the MIME <type>/<subtype> with no parameters

Self {
type_: type_.into(),
subtype: subtype.into(),
parameters: vec![],
}
}

/// Return true if this [`Mime`] matches a given type and subtype, regardless
/// of what parameters it has.
pub fn matches(&self, type_: &str, subtype: &str) -> bool {
self.type_ == type_ && self.subtype == subtype
}

pub fn get_parameter<P>(&self, name: &P) -> Option<&str>
where
P: ?Sized + PartialEq<str>,
Expand Down Expand Up @@ -205,3 +219,19 @@ static IS_HTTP_TOKEN: [bool; 256] = byte_map![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

#[test]
fn test_basic_mime() {
let mime = Mime::new("text", "plain");
assert!(mime.matches("text", "plain"));

let cloned = mime.clone();
assert!(cloned.matches("text", "plain"));

let mime = Mime {
type_: "text".into(),
subtype: "html".into(),
parameters: vec![("one".into(), "two".into())],
};
assert!(mime.matches("text", "html"));
}
Loading