From 1d91113524c632d35b3b141eb6d92607b8d94843 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 17 Sep 2021 12:59:02 +0530 Subject: [PATCH] make asset lookup generic over impl AsRef<&str> --- CHANGELOG.md | 3 +++ src/filemap.rs | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 090f23a..fc8db4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,9 @@ is unset(i.e `None`), no mime based filtering is done and all files inside source directory is considered for processing. +- `Files::get_full_path()` and `Files::get()` now accept `impl + AsRef` for path argument. + ### Fixed: - `Files::get()` now behaves as it is described in the documentation diff --git a/src/filemap.rs b/src/filemap.rs index 30cbc9d..e9741d1 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -14,7 +14,6 @@ //! //! let files = Files::new(CACHE_BUSTER_DATA_FILE); //! ``` - use std::collections::HashMap; use serde::{Deserialize, Serialize}; @@ -40,8 +39,8 @@ impl Files { /// /// If the modified filename path is `./prod/test.randomhash.svg`, it will /// output `/test.randomhash.svg`. For full path, see [get_full_path][Self::get_full_path]. - pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a str> { - if let Some(path) = self.map.get(path) { + pub fn get<'a, 'b>(&'b self, path: impl AsRef) -> Option<&'b str> { + if let Some(path) = self.map.get(path.as_ref()) { Some(&path[self.base_dir.len()..]) // Some(&path) } else { @@ -53,8 +52,8 @@ impl Files { /// /// If the modified filename path is `./prod/test.randomhash.svg`, it will /// output `/prod/test.randomhash.svg`. For relative path, see [get][Self::get]. - pub fn get_full_path<'a>(&'a self, path: &'a str) -> Option<&'a String> { - self.map.get(path) + pub fn get_full_path<'a, 'b>(&'b self, path: impl AsRef) -> Option<&'b String> { + self.map.get(path.as_ref()) } }