From 5010ce5e1aa553b8ce614410d3200565f8bbc0d6 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 20 Aug 2022 15:54:09 +0200 Subject: [PATCH] Allow parsing compression ratios without a colon --- src/formatters.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/formatters.rs b/src/formatters.rs index dfeb7c1b..fed591dc 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -49,14 +49,21 @@ pub fn v2s_compression_ratio(digits: usize) -> Arc String + Send } /// Parse a `x:y` compression ratio back to a floating point number. Used in conjunction with -/// [`v2s_compression_ratio()`]. +/// [`v2s_compression_ratio()`]. Plain numbers are parsed directly for UX's sake. pub fn s2v_compression_ratio() -> Arc Option + Send + Sync> { Arc::new(|string| { - let (numerator, denominator) = string.trim().split_once(':')?; - let numerator: f32 = numerator.trim().parse().ok()?; - let denominator: f32 = denominator.trim().parse().ok()?; + let string = string.trim(); + string + .trim() + .split_once(':') + .and_then(|(numerator, denominator)| { + let numerator: f32 = numerator.trim().parse().ok()?; + let denominator: f32 = denominator.trim().parse().ok()?; - Some(numerator / denominator) + Some(numerator / denominator) + }) + // Just parse the value directly if it doesn't contain a colon + .or_else(|| string.parse().ok()) }) }