1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 13:37:48 +00:00

fold: use UResult

This commit is contained in:
Thomas Queiroz 2021-11-16 17:54:25 -03:00
parent abc93d6f17
commit a7d18f43b4
No known key found for this signature in database
GPG key ID: 229D2DDF7ECA5F8F

View file

@ -7,13 +7,12 @@
// spell-checker:ignore (ToDOs) ncount routput // spell-checker:ignore (ToDOs) ncount routput
#[macro_use]
extern crate uucore;
use clap::{crate_version, App, Arg}; use clap::{crate_version, App, Arg};
use std::fs::File; use std::fs::File;
use std::io::{stdin, BufRead, BufReader, Read}; use std::io::{stdin, BufRead, BufReader, Read};
use std::path::Path; use std::path::Path;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
const TAB_WIDTH: usize = 8; const TAB_WIDTH: usize = 8;
@ -30,7 +29,8 @@ mod options {
pub const FILE: &str = "file"; pub const FILE: &str = "file";
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::ConvertLossy) .collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(); .accept_any();
@ -46,10 +46,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}; };
let width = match poss_width { let width = match poss_width {
Some(inp_width) => match inp_width.parse::<usize>() { Some(inp_width) => inp_width.parse::<usize>().map_err(|e| {
Ok(width) => width, USimpleError::new(
Err(e) => crash!(1, "illegal width value (\"{}\"): {}", inp_width, e), 1,
}, format!("illegal width value ({}): {}", inp_width.quote(), e),
)
})?,
None => 80, None => 80,
}; };
@ -58,9 +60,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
None => vec!["-".to_owned()], None => vec!["-".to_owned()],
}; };
fold(files, bytes, spaces, width); fold(files, bytes, spaces, width)
0
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {
@ -110,7 +110,7 @@ fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) {
(args.to_vec(), None) (args.to_vec(), None)
} }
fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) { fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) -> UResult<()> {
for filename in &filenames { for filename in &filenames {
let filename: &str = filename; let filename: &str = filename;
let mut stdin_buf; let mut stdin_buf;
@ -119,16 +119,17 @@ fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) {
stdin_buf = stdin(); stdin_buf = stdin();
&mut stdin_buf as &mut dyn Read &mut stdin_buf as &mut dyn Read
} else { } else {
file_buf = crash_if_err!(1, File::open(Path::new(filename))); file_buf = File::open(Path::new(filename)).map_err_context(|| filename.to_string())?;
&mut file_buf as &mut dyn Read &mut file_buf as &mut dyn Read
}); });
if bytes { if bytes {
fold_file_bytewise(buffer, spaces, width); fold_file_bytewise(buffer, spaces, width)?;
} else { } else {
fold_file(buffer, spaces, width); fold_file(buffer, spaces, width)?;
} }
} }
Ok(())
} }
/// Fold `file` to fit `width` (number of columns), counting all characters as /// Fold `file` to fit `width` (number of columns), counting all characters as
@ -139,11 +140,15 @@ fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) {
/// to all other characters in the stream. /// to all other characters in the stream.
/// ///
/// If `spaces` is `true`, attempt to break lines at whitespace boundaries. /// If `spaces` is `true`, attempt to break lines at whitespace boundaries.
fn fold_file_bytewise<T: Read>(mut file: BufReader<T>, spaces: bool, width: usize) { fn fold_file_bytewise<T: Read>(mut file: BufReader<T>, spaces: bool, width: usize) -> UResult<()> {
let mut line = String::new(); let mut line = String::new();
loop { loop {
if let Ok(0) = file.read_line(&mut line) { if file
.read_line(&mut line)
.map_err_context(|| "failed to read line".to_string())?
== 0
{
break; break;
} }
@ -190,6 +195,8 @@ fn fold_file_bytewise<T: Read>(mut file: BufReader<T>, spaces: bool, width: usiz
line.truncate(0); line.truncate(0);
} }
Ok(())
} }
/// Fold `file` to fit `width` (number of columns). /// Fold `file` to fit `width` (number of columns).
@ -200,7 +207,7 @@ fn fold_file_bytewise<T: Read>(mut file: BufReader<T>, spaces: bool, width: usiz
/// ///
/// If `spaces` is `true`, attempt to break lines at whitespace boundaries. /// If `spaces` is `true`, attempt to break lines at whitespace boundaries.
#[allow(unused_assignments)] #[allow(unused_assignments)]
fn fold_file<T: Read>(mut file: BufReader<T>, spaces: bool, width: usize) { fn fold_file<T: Read>(mut file: BufReader<T>, spaces: bool, width: usize) -> UResult<()> {
let mut line = String::new(); let mut line = String::new();
let mut output = String::new(); let mut output = String::new();
let mut col_count = 0; let mut col_count = 0;
@ -230,7 +237,11 @@ fn fold_file<T: Read>(mut file: BufReader<T>, spaces: bool, width: usize) {
} }
loop { loop {
if let Ok(0) = file.read_line(&mut line) { if file
.read_line(&mut line)
.map_err_context(|| "failed to read line".to_string())?
== 0
{
break; break;
} }
@ -281,4 +292,6 @@ fn fold_file<T: Read>(mut file: BufReader<T>, spaces: bool, width: usize) {
line.truncate(0); line.truncate(0);
} }
Ok(())
} }