Skip to content

Commit 768fc9a

Browse files
committed
expose Mime::from_extension
1 parent 54b8bc5 commit 768fc9a

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/body.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,7 @@ async fn peek_mime(file: &mut async_std::fs::File) -> io::Result<Option<Mime>> {
491491
#[cfg(all(feature = "async_std", not(target_os = "unknown")))]
492492
fn guess_ext(path: &std::path::Path) -> Option<Mime> {
493493
let ext = path.extension().map(|p| p.to_str()).flatten();
494-
match ext {
495-
Some("html") => Some(mime::HTML),
496-
Some("js") | Some("mjs") | Some("jsonp") => Some(mime::JAVASCRIPT),
497-
Some("json") => Some(mime::JSON),
498-
Some("css") => Some(mime::CSS),
499-
Some("svg") => Some(mime::SVG),
500-
Some("xml") => Some(mime::XML),
501-
None | Some(_) => None,
502-
}
494+
ext.and_then(Mime::from_extension)
503495
}
504496

505497
#[cfg(test)]

src/mime/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ impl Mime {
5050
Mime::from_str(&mime)
5151
}
5252

53+
/// Guess the mime type from a file extension
54+
pub fn from_extension(extension: impl AsRef<str>) -> Option<Self> {
55+
match extension.as_ref() {
56+
"html" => Some(HTML),
57+
"js" | "mjs" | "jsonp" => Some(JAVASCRIPT),
58+
"json" => Some(JSON),
59+
"css" => Some(CSS),
60+
"svg" => Some(SVG),
61+
"xml" => Some(XML),
62+
_ => None,
63+
}
64+
}
65+
5366
/// Access the Mime's `type` value.
5467
///
5568
/// According to the spec this method should be named `type`, but that's a reserved keyword in

0 commit comments

Comments
 (0)