From 641940ef3805589e92b4520d4944ed66afc875fb Mon Sep 17 00:00:00 2001 From: Zachary Kohnen <14093962+DusterTheFirst@users.noreply.github.com> Date: Tue, 31 Mar 2020 12:50:24 -0400 Subject: fix: make filesizeformat work with all types that implement humansize::FileSize (#307) * fix: make filesizeformat work with all types that implement humanzie::FileSize Enabled dynamic dispatch on the filesizeformat filter as to allow for numbers other than usize to work * fix: remove dynamic dispatch dynamic dyspatch would not work since file_size takes a generic. Instead we use a generic function with type B being any type that implements FileSize. * fix: add AsRef to allow for references to be passed as well as owned values * fix: move AsRef into a generic as to allow for owned values * fix: move to borrow from asref as it is implemented by primitives * format: fix import ordering * fix: remove borrow for it was useless * fix: add borrow to input value * refactor: remove unused imports --- askama_shared/src/filters/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 8306ba3..64e1259 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -125,7 +125,7 @@ where #[cfg(feature = "humansize")] /// Returns adequate string representation (in KB, ..) of number of bytes -pub fn filesizeformat(b: usize) -> Result { +pub fn filesizeformat(b: &B) -> Result { b.file_size(file_size_opts::DECIMAL) .map_err(|_| Fmt(fmt::Error)) } @@ -336,11 +336,11 @@ mod tests { #[cfg(feature = "humansize")] #[test] fn test_filesizeformat() { - assert_eq!(filesizeformat(0).unwrap(), "0 B"); - assert_eq!(filesizeformat(999).unwrap(), "999 B"); - assert_eq!(filesizeformat(1000).unwrap(), "1 KB"); - assert_eq!(filesizeformat(1023).unwrap(), "1.02 KB"); - assert_eq!(filesizeformat(1024).unwrap(), "1.02 KB"); + assert_eq!(filesizeformat(&0).unwrap(), "0 B"); + assert_eq!(filesizeformat(&999u64).unwrap(), "999 B"); + assert_eq!(filesizeformat(&1000i32).unwrap(), "1 KB"); + assert_eq!(filesizeformat(&1023).unwrap(), "1.02 KB"); + assert_eq!(filesizeformat(&1024usize).unwrap(), "1.02 KB"); } #[cfg(feature = "percent-encoding")] -- cgit