From b1b1760b64b277949840d0a610c1e9d1406aee36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Sat, 14 Nov 2020 15:13:12 +0100 Subject: [PATCH] Make ImageFormat::from_extension take an &OsStr --- src/image.rs | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/image.rs b/src/image.rs index 2dc928d338..180bbf5563 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,5 +1,6 @@ #![allow(clippy::too_many_arguments)] use std::convert::TryFrom; +use std::ffi::OsStr; use std::io; use std::io::Read; use std::ops::{Deref, DerefMut}; @@ -66,24 +67,29 @@ pub enum ImageFormat { impl ImageFormat { /// Return the image format specified by a path's file extension. - pub fn from_extension(ext: &str) -> Option { - let ext = ext.to_ascii_lowercase(); - - Some(match ext.as_str() { - "jpg" | "jpeg" => ImageFormat::Jpeg, - "png" => ImageFormat::Png, - "gif" => ImageFormat::Gif, - "webp" => ImageFormat::WebP, - "tif" | "tiff" => ImageFormat::Tiff, - "tga" => ImageFormat::Tga, - "dds" => ImageFormat::Dds, - "bmp" => ImageFormat::Bmp, - "ico" => ImageFormat::Ico, - "hdr" => ImageFormat::Hdr, - "pbm" | "pam" | "ppm" | "pgm" => ImageFormat::Pnm, - "ff" | "farbfeld" => ImageFormat::Farbfeld, - _ => return None, - }) + #[inline] + pub fn from_extension(ext: S) -> Option where S: AsRef { + fn inner(ext: &OsStr) -> Option { + let ext = ext.to_str()?.to_ascii_lowercase(); + + Some(match ext.as_str() { + "jpg" | "jpeg" => ImageFormat::Jpeg, + "png" => ImageFormat::Png, + "gif" => ImageFormat::Gif, + "webp" => ImageFormat::WebP, + "tif" | "tiff" => ImageFormat::Tiff, + "tga" => ImageFormat::Tga, + "dds" => ImageFormat::Dds, + "bmp" => ImageFormat::Bmp, + "ico" => ImageFormat::Ico, + "hdr" => ImageFormat::Hdr, + "pbm" | "pam" | "ppm" | "pgm" => ImageFormat::Pnm, + "ff" | "farbfeld" => ImageFormat::Farbfeld, + _ => return None, + }) + } + + inner(ext.as_ref()) } /// Return the image format specified by the path's file extension. @@ -93,7 +99,6 @@ impl ImageFormat { fn inner(path: &Path) -> ImageResult { let exact_ext = path.extension(); exact_ext - .and_then(|s| s.to_str()) .and_then(|ext| ImageFormat::from_extension(ext)) .ok_or_else(|| { let format_hint = match exact_ext {