Skip to content

Commit

Permalink
numfmt: simplify error handling in value handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
sbentmar committed Jan 3, 2023
1 parent b17fda5 commit f9c83b3
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions src/uu/numfmt/src/numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ const USAGE: &str = help_usage!("numfmt.md");

fn handle_args<'a>(args: impl Iterator<Item = &'a str>, options: &NumfmtOptions) -> UResult<()> {
for l in args {
let format_result = format_and_handle_validation(l, options);

if format_result.is_err() && options.invalid == InvalidModes::Abort {
return format_result;
}
format_and_handle_validation(l, options)?;
}
Ok(())
}
Expand All @@ -45,29 +41,16 @@ fn handle_buffer<R>(input: R, options: &NumfmtOptions) -> UResult<()>
where
R: BufRead,
{
let mut lines = input.lines();

for line in lines.by_ref().take(options.header) {
match line {
Ok(l) => println!("{}", l),
Err(e) => return Err(Box::new(NumfmtError::IoError(e.to_string()))),
};
}

for line_result in lines.by_ref() {
if let Err(err) = line_result {
return Err(Box::new(NumfmtError::IoError(err.to_string())));
};

let format_result = format_and_handle_validation(line_result.unwrap().as_ref(), options);

if format_result.is_ok() {
continue;
}

return format_result;
for (idx, line_result) in input.lines().by_ref().enumerate() {
match line_result {
Ok(line) if idx < options.header => {
println!("{}", line);
Ok(())
}
Ok(line) => format_and_handle_validation(line.as_ref(), options),
Err(err) => return Err(Box::new(NumfmtError::IoError(err.to_string()))),
}?;
}

Ok(())
}

Expand Down

0 comments on commit f9c83b3

Please sign in to comment.