Merge pull request #413 from dezgeg/uucore_parse_size
mcookie: Use parse_size from uucore
This commit is contained in:
@@ -11,7 +11,7 @@ name = "mcookie"
|
|||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
uucore = { workspace = true }
|
uucore = { workspace = true, features = ["parser"] }
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
md-5 = { workspace = true }
|
md-5 = { workspace = true }
|
||||||
rand = { workspace = true }
|
rand = { workspace = true }
|
||||||
|
|||||||
@@ -14,9 +14,8 @@ use rand::RngCore;
|
|||||||
use uucore::{
|
use uucore::{
|
||||||
error::{UResult, USimpleError},
|
error::{UResult, USimpleError},
|
||||||
format_usage, help_about, help_usage,
|
format_usage, help_about, help_usage,
|
||||||
|
parser::parse_size,
|
||||||
};
|
};
|
||||||
mod size;
|
|
||||||
use size::Size;
|
|
||||||
|
|
||||||
mod options {
|
mod options {
|
||||||
pub const FILE: &str = "file";
|
pub const FILE: &str = "file";
|
||||||
@@ -43,14 +42,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let max_size = if let Some(size_str) = matches.get_one::<String>(options::MAX_SIZE) {
|
let max_size = if let Some(size_str) = matches.get_one::<String>(options::MAX_SIZE) {
|
||||||
match Size::parse(size_str) {
|
match parse_size::parse_size_u64(size_str) {
|
||||||
Ok(size) => {
|
Ok(0) => MAX_DEFAULT,
|
||||||
let mut s = size.size_bytes();
|
Ok(size) => size,
|
||||||
if s == 0 {
|
|
||||||
s = MAX_DEFAULT;
|
|
||||||
}
|
|
||||||
s
|
|
||||||
}
|
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
return Err(USimpleError::new(1, "Failed to parse max-size value"));
|
return Err(USimpleError::new(1, "Failed to parse max-size value"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<Self, ParseSizeError> {
|
|
||||||
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::<u64>()
|
|
||||||
.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::<u64>()
|
|
||||||
.map(|n| Self(n * 1024_u64.pow(exponent)))
|
|
||||||
.map_err(|_| ParseSizeError(s.to_string()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no suffix, treat as bytes
|
|
||||||
s.parse::<u64>()
|
|
||||||
.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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user