From 1019ea1ed341e3a7769c046aa0be5764789360b6 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 2 Jun 2024 14:38:13 +0200 Subject: Migrate to Rust and Hyper With sanic, downloading a file locally is around ten times slower than with Rust and hyper. Maybe `pypy` could have helped, but I didn’t succeed to set it up quickly with the dependencies. --- src/util.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/util.rs (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..9bc7cb9 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,125 @@ +use chrono::Duration; + +pub fn sanitize_filename(s: &str) -> String { + s.split('.') + .map(sanitize_filename_part) + .collect::>() + .join(".") +} + +pub fn sanitize_filename_part(s: &str) -> String { + s.chars() + .map(|c| { + if c.is_ascii_alphanumeric() { + c.to_lowercase().collect::() + } else { + " ".to_string() + } + }) + .collect::() + .split_whitespace() + .collect::>() + .join("-") +} + +pub fn pretty_print_duration(d: Duration) -> String { + if d.num_days() > 0 { + let plural = if d.num_days() > 1 { "s" } else { "" }; + format!("{} day{}", d.num_days(), plural) + } else if d.num_hours() > 0 { + format!("{} h", d.num_hours()) + } else if d.num_minutes() > 0 { + format!("{} min", d.num_minutes()) + } else { + format!("{} s", d.num_seconds()) + } +} + +pub fn pretty_print_bytes(bytes: usize) -> String { + let ko = bytes / 1024; + let mo = ko / 1024; + let go = mo / 1024; + if go > 0 { + format!("{} GB", go) + } else if mo > 0 { + format!("{} MB", mo) + } else if ko > 0 { + format!("{} KB", ko) + } else { + format!("{} B", bytes) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sanitize_filename() { + assert_eq!(sanitize_filename(""), ""); + assert_eq!(sanitize_filename("foo bar 123"), "foo-bar-123"); + assert_eq!(sanitize_filename("foo bar.123"), "foo-bar.123"); + assert_eq!(sanitize_filename("foo ( test+2 ).xml"), "foo-test-2.xml"); + } + + #[test] + fn test_sanitize_filename_part() { + assert_eq!(sanitize_filename_part(""), ""); + assert_eq!(sanitize_filename_part("foo123BAZ"), "foo123baz"); + assert_eq!(sanitize_filename_part("foo-123-BAZ"), "foo-123-baz"); + assert_eq!(sanitize_filename_part("[()] */+-!;?<'> ?:"), ""); + assert_eq!(sanitize_filename_part("foo [bar] -- BAZ3"), "foo-bar-baz3"); + } + + #[test] + fn test_pretty_print_duration() { + assert_eq!( + pretty_print_duration(Duration::days(2)), + "2 days".to_string() + ); + assert_eq!( + pretty_print_duration(Duration::hours(30)), + "1 day".to_string() + ); + assert_eq!( + pretty_print_duration(Duration::days(1)), + "1 day".to_string() + ); + assert_eq!( + pretty_print_duration(Duration::hours(15)), + "15 h".to_string() + ); + assert_eq!( + pretty_print_duration(Duration::minutes(70)), + "1 h".to_string() + ); + assert_eq!( + pretty_print_duration(Duration::minutes(44)), + "44 min".to_string() + ); + assert_eq!( + pretty_print_duration(Duration::seconds(100)), + "1 min".to_string() + ); + assert_eq!( + pretty_print_duration(Duration::seconds(7)), + "7 s".to_string() + ); + assert_eq!(pretty_print_duration(Duration::zero()), "0 s".to_string()); + } + + #[test] + fn test_pretty_print_bytes() { + assert_eq!(pretty_print_bytes(0), "0 B"); + assert_eq!(pretty_print_bytes(10), "10 B"); + assert_eq!(pretty_print_bytes(1024), "1 KB"); + assert_eq!(pretty_print_bytes(1100), "1 KB"); + assert_eq!(pretty_print_bytes(54 * 1024), "54 KB"); + assert_eq!(pretty_print_bytes(1024 * 1024), "1 MB"); + assert_eq!(pretty_print_bytes(1300 * 1024), "1 MB"); + assert_eq!(pretty_print_bytes(79 * 1024 * 1024), "79 MB"); + assert_eq!(pretty_print_bytes(1024 * 1024 * 1024), "1 GB"); + assert_eq!(pretty_print_bytes(1300 * 1024 * 1024), "1 GB"); + assert_eq!(pretty_print_bytes(245 * 1024 * 1024 * 1024), "245 GB"); + } +} -- cgit v1.2.3