aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared
diff options
context:
space:
mode:
authorLibravatar Zachary Kohnen <14093962+DusterTheFirst@users.noreply.github.com>2020-03-31 12:50:24 -0400
committerLibravatar GitHub <noreply@github.com>2020-03-31 18:50:24 +0200
commit641940ef3805589e92b4520d4944ed66afc875fb (patch)
treeb9c929555879c939578fe8db0e0be4c4e0f25a31 /askama_shared
parent2ec671f243e20f58ac52c0ce935d9734c1069c8f (diff)
downloadaskama-641940ef3805589e92b4520d4944ed66afc875fb.tar.gz
askama-641940ef3805589e92b4520d4944ed66afc875fb.tar.bz2
askama-641940ef3805589e92b4520d4944ed66afc875fb.zip
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
Diffstat (limited to 'askama_shared')
-rw-r--r--askama_shared/src/filters/mod.rs12
1 files 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<String> {
+pub fn filesizeformat<B: FileSize>(b: &B) -> Result<String> {
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")]