From 85aa23456cb5b4baa100ba27c4ab131386e06a3d Mon Sep 17 00:00:00 2001 From: Laurentiu Nicola Date: Mon, 1 Oct 2018 20:59:14 +0300 Subject: [PATCH] Add guess_mime_type_str --- src/lib.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9975141..5c34a04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,9 +69,24 @@ pub fn guess_mime_type>(path: P) -> Mime { /// /// Take care when processing files with assumptions based on the return value of this function. pub fn guess_mime_type_opt>(path: P) -> Option { + guess_mime_type_str(path) + .map(|mime| mime.parse::().unwrap()) +} + +/// Guess the MIME type string of `path` by its extension (as defined by `Path::extension()`). +/// +/// If `path` has no extension, or its extension has no known MIME type mapping, +/// then `None` is returned. +/// +/// ## Note +/// **Guess** is the operative word here, as there are no guarantees that the contents of the file +/// that `path` points to match the MIME type associated with the path's extension. +/// +/// Take care when processing files with assumptions based on the return value of this function. +pub fn guess_mime_type_str>(path: P) -> Option<&'static str> { let ext = path.as_ref().extension().and_then(OsStr::to_str).unwrap_or(""); - get_mime_type_opt(ext) + get_mime_type_str(ext) } /// Get the MIME type associated with a file extension.