From 100a7817fd5ae5aa34fc804db8be42b40d533ee1 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Sun, 12 Oct 2025 14:04:17 +0300 Subject: [PATCH] mcookie: Use parse_size from uucore --- src/uu/mcookie/Cargo.toml | 2 +- src/uu/mcookie/src/mcookie.rs | 14 ++---- src/uu/mcookie/src/size.rs | 88 ----------------------------------- 3 files changed, 5 insertions(+), 99 deletions(-) delete mode 100644 src/uu/mcookie/src/size.rs diff --git a/src/uu/mcookie/Cargo.toml b/src/uu/mcookie/Cargo.toml index 6b5908e..f50855c 100644 --- a/src/uu/mcookie/Cargo.toml +++ b/src/uu/mcookie/Cargo.toml @@ -11,7 +11,7 @@ name = "mcookie" path = "src/main.rs" [dependencies] -uucore = { workspace = true } +uucore = { workspace = true, features = ["parser"] } clap = { workspace = true } md-5 = { workspace = true } rand = { workspace = true } diff --git a/src/uu/mcookie/src/mcookie.rs b/src/uu/mcookie/src/mcookie.rs index 30e8ef7..8d791d7 100644 --- a/src/uu/mcookie/src/mcookie.rs +++ b/src/uu/mcookie/src/mcookie.rs @@ -14,9 +14,8 @@ use rand::RngCore; use uucore::{ error::{UResult, USimpleError}, format_usage, help_about, help_usage, + parser::parse_size, }; -mod size; -use size::Size; mod options { pub const FILE: &str = "file"; @@ -43,14 +42,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .collect(); let max_size = if let Some(size_str) = matches.get_one::(options::MAX_SIZE) { - match Size::parse(size_str) { - Ok(size) => { - let mut s = size.size_bytes(); - if s == 0 { - s = MAX_DEFAULT; - } - s - } + match parse_size::parse_size_u64(size_str) { + Ok(0) => MAX_DEFAULT, + Ok(size) => size, Err(_) => { return Err(USimpleError::new(1, "Failed to parse max-size value")); } diff --git a/src/uu/mcookie/src/size.rs b/src/uu/mcookie/src/size.rs deleted file mode 100644 index b870978..0000000 --- a/src/uu/mcookie/src/size.rs +++ /dev/null @@ -1,88 +0,0 @@ -// This file is part of the uutils util-linux package. -// -// For the full copyright and license information, please view the LICENSE -// file that was distributed with this source code. - -use std::error::Error; -use std::fmt; - -#[derive(Debug)] -pub struct ParseSizeError(String); - -impl fmt::Display for ParseSizeError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Invalid size format: {}", self.0) - } -} - -impl Error for ParseSizeError {} - -pub struct Size(u64); - -impl Size { - pub fn parse(s: &str) -> Result { - let s = s.trim(); - - // Handle bytes with "B" suffix - if s.ends_with('B') && !s.ends_with("iB") { - if let Some(nums) = s.strip_suffix('B') { - return nums - .trim() - .parse::() - .map(Self) - .map_err(|_| ParseSizeError(s.to_string())); - } - } - - // Handle binary units (KiB, MiB, GiB, TiB) - for (suffix, exponent) in [("KiB", 1), ("MiB", 2), ("GiB", 3), ("TiB", 4)] { - if let Some(nums) = s.strip_suffix(suffix) { - return nums - .trim() - .parse::() - .map(|n| Self(n * 1024_u64.pow(exponent))) - .map_err(|_| ParseSizeError(s.to_string())); - } - } - - // If no suffix, treat as bytes - s.parse::() - .map(Self) - .map_err(|_| ParseSizeError(s.to_string())) - } - - pub fn size_bytes(&self) -> u64 { - self.0 - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_parse_numeric() { - assert_eq!(Size::parse("1234").unwrap().size_bytes(), 1234); - } - - #[test] - fn test_parse_with_suffix() { - assert_eq!(Size::parse("1024B").unwrap().size_bytes(), 1024); - assert_eq!(Size::parse("1KiB").unwrap().size_bytes(), 1024); - assert_eq!(Size::parse("1MiB").unwrap().size_bytes(), 1024 * 1024); - assert_eq!( - Size::parse("1GiB").unwrap().size_bytes(), - 1024 * 1024 * 1024 - ); - assert_eq!( - Size::parse("1TiB").unwrap().size_bytes(), - 1024 * 1024 * 1024 * 1024 - ); - } - - #[test] - fn test_invalid_input() { - // Invalid format - assert!(Size::parse("invalid").is_err()); - } -}