diff options
author | Hubert Figuière <hub@figuiere.net> | 2024-01-29 23:07:14 -0500 |
---|---|---|
committer | Hubert Figuière <hub@figuiere.net> | 2024-01-30 08:40:41 -0500 |
commit | b4bde3744d6a188b9eeaa759631735c48837ef16 (patch) | |
tree | e9085c2d2bc1741e56beea8e0b4c08b7b93826c4 | |
parent | 2bd66c616edf7b98ff6e5a6c04acc953261d8162 (diff) |
api: Added type_for_mime_type()
Signed-off-by: Hubert Figuière <hub@figuiere.net>
-rw-r--r-- | src/identify.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/identify.rs b/src/identify.rs index 1c64db2..dd391c7 100644 --- a/src/identify.rs +++ b/src/identify.rs @@ -23,6 +23,7 @@ use std::collections::HashMap; use std::io::{Read, Seek, SeekFrom}; +use std::iter::FromIterator; use once_cell::sync::Lazy; @@ -80,6 +81,10 @@ lazy_static::lazy_static! { pub(crate) static ref TYPE_TO_MIME: HashMap<Type, &'static str> = HashMap::from( TYPE_MIME ); + + pub(crate) static ref MIME_TO_TYPE: HashMap<&'static str, Type> = HashMap::from_iter( + TYPE_MIME.iter().map(|(t, m)| (*m, *t)) + ); } static MIME_TYPES: Lazy<Vec<String>> = Lazy::new(|| { @@ -107,6 +112,12 @@ pub(crate) fn type_for_extension(ext: &str) -> Option<Type> { EXT_TO_TYPE.get(ext).cloned() } +/// Get the type associated to the mimetype. +pub(crate) fn type_for_mime_type(mime: &str) -> Option<Type> { + MIME_TO_TYPE.get(mime).cloned() +} + + /// Return the `Type` based on the content of the file. pub(crate) fn type_for_content(content: &mut View) -> Result<Option<Type>> { use crate::Type::*; @@ -193,6 +204,16 @@ mod test { } #[test] + fn test_type_for_mime_type() { + use super::type_for_mime_type; + use crate::Type; + + assert_eq!(type_for_mime_type("image/x-fuji-raf"), Some(Type::Raf)); + assert_eq!(type_for_mime_type("image/x-canon-cr3"), Some(Type::Cr3)); + assert_eq!(type_for_mime_type("application/octet-stream"), None); + } + + #[test] fn test_type_for_content() { use super::type_for_content; use crate::{io, Error, Type}; |