about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-23 13:04:42 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-23 13:04:42 +0200
commit08e9d686a836032ec54834a8142be4d7302205e0 (patch)
tree9d94f172434f2740eac082a12ab973c1b874dbb5
parentfix(yt_dlp/lib): Standardize the formatting of bytes with the new `bytes` crate (diff)
downloadyt-08e9d686a836032ec54834a8142be4d7302205e0.tar.gz
yt-08e9d686a836032ec54834a8142be4d7302205e0.zip
refactor(cli): Replace the byte parser with the one from the `bytes` crate
-rw-r--r--Cargo.lock12
-rw-r--r--Cargo.toml1
-rw-r--r--src/cli.rs45
3 files changed, 16 insertions, 42 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6a2f287..587c33a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -243,6 +243,10 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
 
 [[package]]
 name = "bytes"
+version = "1.0.0"
+
+[[package]]
+name = "bytes"
 version = "1.7.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
@@ -1550,7 +1554,7 @@ dependencies = [
  "ahash",
  "atoi",
  "byteorder",
- "bytes",
+ "bytes 1.7.1",
  "crc",
  "crossbeam-queue",
  "either",
@@ -1628,7 +1632,7 @@ dependencies = [
  "base64",
  "bitflags 2.6.0",
  "byteorder",
- "bytes",
+ "bytes 1.7.1",
  "crc",
  "digest",
  "dotenvy",
@@ -1859,7 +1863,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5"
 dependencies = [
  "backtrace",
- "bytes",
+ "bytes 1.7.1",
  "libc",
  "mio",
  "pin-project-lite",
@@ -2293,6 +2297,7 @@ version = "1.0.0"
 dependencies = [
  "anyhow",
  "blake3",
+ "bytes 1.0.0",
  "chrono",
  "chrono-humanize",
  "clap",
@@ -2316,6 +2321,7 @@ dependencies = [
 name = "yt_dlp"
 version = "0.1.0"
 dependencies = [
+ "bytes 1.0.0",
  "log",
  "pyo3",
  "serde",
diff --git a/Cargo.toml b/Cargo.toml
index a2797f7..20b5af2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -40,6 +40,7 @@ url = { version = "2.5.2", features = ["serde"] }
 xdg = "2.5.2"
 yt_dlp = { path = "./yt_dlp/" }
 libmpv2 = { path = "./crates/libmpv2" }
+bytes = { path = "./crates/bytes" }
 trinitry = { version = "0.2.2" }
 
 [[bin]]
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 {