mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
uucore: add InvalidSuffix to ParseSizeError
This commit is contained in:
parent
e337826a11
commit
27dd59635a
10 changed files with 89 additions and 35 deletions
|
@ -388,7 +388,7 @@ fn parse_bytes_no_x(s: &str) -> Result<u64, ParseError> {
|
||||||
let (num, multiplier) = match (s.find('c'), s.rfind('w'), s.rfind('b')) {
|
let (num, multiplier) = match (s.find('c'), s.rfind('w'), s.rfind('b')) {
|
||||||
(None, None, None) => match uucore::parse_size::parse_size(s) {
|
(None, None, None) => match uucore::parse_size::parse_size(s) {
|
||||||
Ok(n) => (n, 1),
|
Ok(n) => (n, 1),
|
||||||
Err(ParseSizeError::ParseFailure(s)) => {
|
Err(ParseSizeError::InvalidSuffix(s)) | Err(ParseSizeError::ParseFailure(s)) => {
|
||||||
return Err(ParseError::MultiplierStringParseFailure(s))
|
return Err(ParseError::MultiplierStringParseFailure(s))
|
||||||
}
|
}
|
||||||
Err(ParseSizeError::SizeTooBig(s)) => {
|
Err(ParseSizeError::SizeTooBig(s)) => {
|
||||||
|
|
|
@ -121,6 +121,7 @@ impl Default for Options {
|
||||||
enum OptionsError {
|
enum OptionsError {
|
||||||
BlockSizeTooLarge(String),
|
BlockSizeTooLarge(String),
|
||||||
InvalidBlockSize(String),
|
InvalidBlockSize(String),
|
||||||
|
InvalidSuffix(String),
|
||||||
|
|
||||||
/// An error getting the columns to display in the output table.
|
/// An error getting the columns to display in the output table.
|
||||||
ColumnError(ColumnError),
|
ColumnError(ColumnError),
|
||||||
|
@ -139,6 +140,9 @@ impl fmt::Display for OptionsError {
|
||||||
// TODO This needs to vary based on whether `--block-size`
|
// TODO This needs to vary based on whether `--block-size`
|
||||||
// or `-B` were provided.
|
// or `-B` were provided.
|
||||||
Self::InvalidBlockSize(s) => write!(f, "invalid --block-size argument {}", s),
|
Self::InvalidBlockSize(s) => write!(f, "invalid --block-size argument {}", s),
|
||||||
|
// TODO This needs to vary based on whether `--block-size`
|
||||||
|
// or `-B` were provided.
|
||||||
|
Self::InvalidSuffix(s) => write!(f, "invalid suffix in --block-size argument {}", s),
|
||||||
Self::ColumnError(ColumnError::MultipleColumns(s)) => write!(
|
Self::ColumnError(ColumnError::MultipleColumns(s)) => write!(
|
||||||
f,
|
f,
|
||||||
"option --output: field {} used more than once",
|
"option --output: field {} used more than once",
|
||||||
|
@ -174,6 +178,7 @@ impl Options {
|
||||||
show_local_fs: matches.is_present(OPT_LOCAL),
|
show_local_fs: matches.is_present(OPT_LOCAL),
|
||||||
show_all_fs: matches.is_present(OPT_ALL),
|
show_all_fs: matches.is_present(OPT_ALL),
|
||||||
block_size: block_size_from_matches(matches).map_err(|e| match e {
|
block_size: block_size_from_matches(matches).map_err(|e| match e {
|
||||||
|
ParseSizeError::InvalidSuffix(s) => OptionsError::InvalidSuffix(s),
|
||||||
ParseSizeError::SizeTooBig(_) => OptionsError::BlockSizeTooLarge(
|
ParseSizeError::SizeTooBig(_) => OptionsError::BlockSizeTooLarge(
|
||||||
matches.value_of(OPT_BLOCKSIZE).unwrap().to_string(),
|
matches.value_of(OPT_BLOCKSIZE).unwrap().to_string(),
|
||||||
),
|
),
|
||||||
|
|
|
@ -953,8 +953,10 @@ impl Threshold {
|
||||||
fn format_error_message(error: &ParseSizeError, s: &str, option: &str) -> String {
|
fn format_error_message(error: &ParseSizeError, s: &str, option: &str) -> String {
|
||||||
// NOTE:
|
// NOTE:
|
||||||
// GNU's du echos affected flag, -B or --block-size (-t or --threshold), depending user's selection
|
// GNU's du echos affected flag, -B or --block-size (-t or --threshold), depending user's selection
|
||||||
// GNU's du does distinguish between "invalid (suffix in) argument"
|
|
||||||
match error {
|
match error {
|
||||||
|
ParseSizeError::InvalidSuffix(_) => {
|
||||||
|
format!("invalid suffix in --{} argument {}", option, s.quote())
|
||||||
|
}
|
||||||
ParseSizeError::ParseFailure(_) => format!("invalid --{} argument {}", option, s.quote()),
|
ParseSizeError::ParseFailure(_) => format!("invalid --{} argument {}", option, s.quote()),
|
||||||
ParseSizeError::SizeTooBig(_) => format!("--{} argument {} too large", option, s.quote()),
|
ParseSizeError::SizeTooBig(_) => format!("--{} argument {} too large", option, s.quote()),
|
||||||
}
|
}
|
||||||
|
|
|
@ -679,8 +679,10 @@ fn open_input_peek_reader(
|
||||||
fn format_error_message(error: &ParseSizeError, s: &str, option: &str) -> String {
|
fn format_error_message(error: &ParseSizeError, s: &str, option: &str) -> String {
|
||||||
// NOTE:
|
// NOTE:
|
||||||
// GNU's od echos affected flag, -N or --read-bytes (-j or --skip-bytes, etc.), depending user's selection
|
// GNU's od echos affected flag, -N or --read-bytes (-j or --skip-bytes, etc.), depending user's selection
|
||||||
// GNU's od does distinguish between "invalid (suffix in) argument"
|
|
||||||
match error {
|
match error {
|
||||||
|
ParseSizeError::InvalidSuffix(_) => {
|
||||||
|
format!("invalid suffix in --{} argument {}", option, s.quote())
|
||||||
|
}
|
||||||
ParseSizeError::ParseFailure(_) => format!("invalid --{} argument {}", option, s.quote()),
|
ParseSizeError::ParseFailure(_) => format!("invalid --{} argument {}", option, s.quote()),
|
||||||
ParseSizeError::SizeTooBig(_) => format!("--{} argument {} too large", option, s.quote()),
|
ParseSizeError::SizeTooBig(_) => format!("--{} argument {} too large", option, s.quote()),
|
||||||
}
|
}
|
||||||
|
|
|
@ -362,8 +362,10 @@ impl GlobalSettings {
|
||||||
size
|
size
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
|
} else if size_string.starts_with(|c: char| c.is_ascii_digit()) {
|
||||||
|
Err(ParseSizeError::InvalidSuffix("invalid suffix".to_string()))
|
||||||
} else {
|
} else {
|
||||||
Err(ParseSizeError::ParseFailure("invalid suffix".to_string()))
|
Err(ParseSizeError::ParseFailure("parse failure".to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1833,8 +1835,10 @@ fn open(path: impl AsRef<OsStr>) -> UResult<Box<dyn Read + Send>> {
|
||||||
fn format_error_message(error: &ParseSizeError, s: &str, option: &str) -> String {
|
fn format_error_message(error: &ParseSizeError, s: &str, option: &str) -> String {
|
||||||
// NOTE:
|
// NOTE:
|
||||||
// GNU's sort echos affected flag, -S or --buffer-size, depending user's selection
|
// GNU's sort echos affected flag, -S or --buffer-size, depending user's selection
|
||||||
// GNU's sort does distinguish between "invalid (suffix in) argument"
|
|
||||||
match error {
|
match error {
|
||||||
|
ParseSizeError::InvalidSuffix(_) => {
|
||||||
|
format!("invalid suffix in --{} argument {}", option, s.quote())
|
||||||
|
}
|
||||||
ParseSizeError::ParseFailure(_) => format!("invalid --{} argument {}", option, s.quote()),
|
ParseSizeError::ParseFailure(_) => format!("invalid --{} argument {}", option, s.quote()),
|
||||||
ParseSizeError::SizeTooBig(_) => format!("--{} argument {} too large", option, s.quote()),
|
ParseSizeError::SizeTooBig(_) => format!("--{} argument {} too large", option, s.quote()),
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,7 +72,8 @@ pub fn parse_size(size: &str) -> Result<u64, ParseSizeError> {
|
||||||
"EB" | "eB" => (1000, 6),
|
"EB" | "eB" => (1000, 6),
|
||||||
"ZB" | "zB" => (1000, 7),
|
"ZB" | "zB" => (1000, 7),
|
||||||
"YB" | "yB" => (1000, 8),
|
"YB" | "yB" => (1000, 8),
|
||||||
_ => return Err(ParseSizeError::parse_failure(size)),
|
_ if numeric_string.is_empty() => return Err(ParseSizeError::parse_failure(size)),
|
||||||
|
_ => return Err(ParseSizeError::invalid_suffix(size)),
|
||||||
};
|
};
|
||||||
let factor = match u64::try_from(base.pow(exponent)) {
|
let factor = match u64::try_from(base.pow(exponent)) {
|
||||||
Ok(n) => n,
|
Ok(n) => n,
|
||||||
|
@ -85,13 +86,15 @@ pub fn parse_size(size: &str) -> Result<u64, ParseSizeError> {
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum ParseSizeError {
|
pub enum ParseSizeError {
|
||||||
ParseFailure(String), // Syntax
|
InvalidSuffix(String), // Suffix
|
||||||
SizeTooBig(String), // Overflow
|
ParseFailure(String), // Syntax
|
||||||
|
SizeTooBig(String), // Overflow
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for ParseSizeError {
|
impl Error for ParseSizeError {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
match *self {
|
match *self {
|
||||||
|
ParseSizeError::InvalidSuffix(ref s) => &*s,
|
||||||
ParseSizeError::ParseFailure(ref s) => &*s,
|
ParseSizeError::ParseFailure(ref s) => &*s,
|
||||||
ParseSizeError::SizeTooBig(ref s) => &*s,
|
ParseSizeError::SizeTooBig(ref s) => &*s,
|
||||||
}
|
}
|
||||||
|
@ -101,7 +104,9 @@ impl Error for ParseSizeError {
|
||||||
impl fmt::Display for ParseSizeError {
|
impl fmt::Display for ParseSizeError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
let s = match self {
|
let s = match self {
|
||||||
ParseSizeError::ParseFailure(s) | ParseSizeError::SizeTooBig(s) => s,
|
ParseSizeError::InvalidSuffix(s)
|
||||||
|
| ParseSizeError::ParseFailure(s)
|
||||||
|
| ParseSizeError::SizeTooBig(s) => s,
|
||||||
};
|
};
|
||||||
write!(f, "{}", s)
|
write!(f, "{}", s)
|
||||||
}
|
}
|
||||||
|
@ -111,6 +116,10 @@ impl fmt::Display for ParseSizeError {
|
||||||
// but there's a lot of downstream code that constructs these errors manually
|
// but there's a lot of downstream code that constructs these errors manually
|
||||||
// that would be affected
|
// that would be affected
|
||||||
impl ParseSizeError {
|
impl ParseSizeError {
|
||||||
|
fn invalid_suffix(s: &str) -> Self {
|
||||||
|
Self::InvalidSuffix(format!("{}", s.quote()))
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_failure(s: &str) -> Self {
|
fn parse_failure(s: &str) -> Self {
|
||||||
// stderr on linux (GNU coreutils 8.32) (LC_ALL=C)
|
// stderr on linux (GNU coreutils 8.32) (LC_ALL=C)
|
||||||
// has to be handled in the respective uutils because strings differ, e.g.:
|
// has to be handled in the respective uutils because strings differ, e.g.:
|
||||||
|
@ -237,20 +246,20 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_suffix() {
|
||||||
|
let test_strings = ["328hdsf3290", "5mib", "1e2", "1H", "1.2"];
|
||||||
|
for &test_string in &test_strings {
|
||||||
|
assert_eq!(
|
||||||
|
parse_size(test_string).unwrap_err(),
|
||||||
|
ParseSizeError::InvalidSuffix(format!("{}", test_string.quote()))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn invalid_syntax() {
|
fn invalid_syntax() {
|
||||||
let test_strings = [
|
let test_strings = ["biB", "-", "+", "", "-1", "∞"];
|
||||||
"328hdsf3290",
|
|
||||||
"5MiB nonsense",
|
|
||||||
"5mib",
|
|
||||||
"biB",
|
|
||||||
"-",
|
|
||||||
"+",
|
|
||||||
"",
|
|
||||||
"-1",
|
|
||||||
"1e2",
|
|
||||||
"∞",
|
|
||||||
];
|
|
||||||
for &test_string in &test_strings {
|
for &test_string in &test_strings {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_size(test_string).unwrap_err(),
|
parse_size(test_string).unwrap_err(),
|
||||||
|
|
|
@ -566,6 +566,19 @@ fn test_invalid_block_size() {
|
||||||
.stderr_contains("invalid --block-size argument '0K'");
|
.stderr_contains("invalid --block-size argument '0K'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_invalid_block_size_suffix() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("--block-size=1H")
|
||||||
|
.fails()
|
||||||
|
.stderr_contains("invalid suffix in --block-size argument '1H'");
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("--block-size=1.2")
|
||||||
|
.fails()
|
||||||
|
.stderr_contains("invalid suffix in --block-size argument '1.2'");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_output_selects_columns() {
|
fn test_output_selects_columns() {
|
||||||
let output = new_ucmd!()
|
let output = new_ucmd!()
|
||||||
|
|
|
@ -94,7 +94,13 @@ fn test_du_invalid_size() {
|
||||||
.arg("/tmp")
|
.arg("/tmp")
|
||||||
.fails()
|
.fails()
|
||||||
.code_is(1)
|
.code_is(1)
|
||||||
.stderr_only(format!("du: invalid --{} argument '1fb4t'", s));
|
.stderr_only(format!("du: invalid suffix in --{} argument '1fb4t'", s));
|
||||||
|
ts.ucmd()
|
||||||
|
.arg(format!("--{}=x", s))
|
||||||
|
.arg("/tmp")
|
||||||
|
.fails()
|
||||||
|
.code_is(1)
|
||||||
|
.stderr_only(format!("du: invalid --{} argument 'x'", s));
|
||||||
#[cfg(not(target_pointer_width = "128"))]
|
#[cfg(not(target_pointer_width = "128"))]
|
||||||
ts.ucmd()
|
ts.ucmd()
|
||||||
.arg(format!("--{}=1Y", s))
|
.arg(format!("--{}=1Y", s))
|
||||||
|
|
|
@ -827,7 +827,8 @@ fn test_traditional_only_label() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_od_invalid_bytes() {
|
fn test_od_invalid_bytes() {
|
||||||
const INVALID_SIZE: &str = "1fb4t";
|
const INVALID_SIZE: &str = "x";
|
||||||
|
const INVALID_SUFFIX: &str = "1fb4t";
|
||||||
const BIG_SIZE: &str = "1Y";
|
const BIG_SIZE: &str = "1Y";
|
||||||
|
|
||||||
// NOTE:
|
// NOTE:
|
||||||
|
@ -852,6 +853,16 @@ fn test_od_invalid_bytes() {
|
||||||
option, INVALID_SIZE
|
option, INVALID_SIZE
|
||||||
));
|
));
|
||||||
|
|
||||||
|
new_ucmd!()
|
||||||
|
.arg(format!("{}={}", option, INVALID_SUFFIX))
|
||||||
|
.arg("file")
|
||||||
|
.fails()
|
||||||
|
.code_is(1)
|
||||||
|
.stderr_only(format!(
|
||||||
|
"od: invalid suffix in {} argument '{}'",
|
||||||
|
option, INVALID_SUFFIX
|
||||||
|
));
|
||||||
|
|
||||||
#[cfg(not(target_pointer_width = "128"))]
|
#[cfg(not(target_pointer_width = "128"))]
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.arg(format!("{}={}", option, BIG_SIZE))
|
.arg(format!("{}={}", option, BIG_SIZE))
|
||||||
|
|
|
@ -56,18 +56,20 @@ fn test_buffer_sizes() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_buffer_size() {
|
fn test_invalid_buffer_size() {
|
||||||
let buffer_sizes = ["asd", "100f"];
|
new_ucmd!()
|
||||||
for invalid_buffer_size in &buffer_sizes {
|
.arg("-S")
|
||||||
new_ucmd!()
|
.arg("asd")
|
||||||
.arg("-S")
|
.fails()
|
||||||
.arg(invalid_buffer_size)
|
.code_is(2)
|
||||||
.fails()
|
.stderr_only("sort: invalid --buffer-size argument 'asd'");
|
||||||
.code_is(2)
|
|
||||||
.stderr_only(format!(
|
new_ucmd!()
|
||||||
"sort: invalid --buffer-size argument '{}'",
|
.arg("-S")
|
||||||
invalid_buffer_size
|
.arg("100f")
|
||||||
));
|
.fails()
|
||||||
}
|
.code_is(2)
|
||||||
|
.stderr_only("sort: invalid suffix in --buffer-size argument '100f'");
|
||||||
|
|
||||||
#[cfg(not(target_pointer_width = "128"))]
|
#[cfg(not(target_pointer_width = "128"))]
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
.arg("-n")
|
.arg("-n")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue