diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-08-23 13:04:42 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-08-23 13:04:42 +0200 |
commit | 08e9d686a836032ec54834a8142be4d7302205e0 (patch) | |
tree | 9d94f172434f2740eac082a12ab973c1b874dbb5 /src | |
parent | fix(yt_dlp/lib): Standardize the formatting of bytes with the new `bytes` crate (diff) | |
download | yt-08e9d686a836032ec54834a8142be4d7302205e0.tar.gz yt-08e9d686a836032ec54834a8142be4d7302205e0.zip |
refactor(cli): Replace the byte parser with the one from the `bytes` crate
Diffstat (limited to 'src')
-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 { |