diff options
Diffstat (limited to '')
-rw-r--r-- | src/cli.rs | 45 |
1 files changed, 6 insertions, 39 deletions
diff --git a/src/cli.rs b/src/cli.rs index 6f5abd2..c567828 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,6 +14,7 @@ use anyhow::{bail, Error}; use chrono::NaiveDate; use clap::{ArgAction, Args, Parser, Subcommand}; use url::Url; +use bytes::Bytes; use crate::{ constants, select::selection_file::duration::Duration, @@ -104,45 +105,11 @@ pub enum Command { }, } -fn byte_parser(s: &str) -> Result<u64, Error> { - const B: u64 = 1; - - const KIB: u64 = 1024 * B; - const MIB: u64 = 1024 * KIB; - const GIB: u64 = 1024 * MIB; - - const KB: u64 = 1000 * B; - const MB: u64 = 1000 * KB; - const GB: u64 = 1000 * MB; - - let s = s - .chars() - .filter(|elem| !elem.is_whitespace()) - .collect::<String>(); - - let number: u64 = s - .chars() - .take_while(|x| x.is_numeric()) - .collect::<String>() - .parse()?; - let extension = s.chars().skip_while(|x| x.is_numeric()).collect::<String>(); - - let output = match extension.to_lowercase().as_str() { - "" => number, - "b" => number * B, - "kib" => number * KIB, - "mib" => number * MIB, - "gib" => number * GIB, - "kb" => number * KB, - "mb" => number * MB, - "gb" => number * GB, - other => bail!( - "Your extension '{}' is not yet supported. Only KB,MB,GB or KiB,MiB,GiB are supported", - other - ), - }; - - Ok(output) +fn byte_parser(input: &str) -> Result<u64, anyhow::Error> { + Ok(input + .parse::<Bytes>() + .with_context(|| format!("Failed to parse '{}' as bytes!", input))? + .as_u64()) } impl Default for Command { |