1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

fmt:uumain: split/doc copy into smaller functions

This commit is contained in:
Sylvestre Ledru 2023-04-14 23:36:51 +02:00
parent 2c1aa229a0
commit 25fbcd89a5

View file

@ -11,7 +11,7 @@ use clap::{crate_version, Arg, ArgAction, Command};
use std::cmp; use std::cmp;
use std::fs::File; use std::fs::File;
use std::io::{stdin, stdout, Write}; use std::io::{stdin, stdout, Write};
use std::io::{BufReader, BufWriter, Read}; use std::io::{BufReader, BufWriter, Read, Stdout};
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError}; use uucore::error::{FromIo, UResult, USimpleError};
use uucore::{format_usage, help_about, help_usage, show_warning}; use uucore::{format_usage, help_about, help_usage, show_warning};
@ -60,10 +60,16 @@ pub struct FmtOptions {
goal: usize, goal: usize,
tabwidth: usize, tabwidth: usize,
} }
/// Parse the command line arguments and return the list of files and formatting options.
#[uucore::main] ///
#[allow(clippy::cognitive_complexity)] /// # Arguments
pub fn uumain(args: impl uucore::Args) -> UResult<()> { ///
/// * `args` - Command line arguments.
///
/// # Returns
///
/// A tuple containing a vector of file names and a `FmtOptions` struct.
fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec<String>, FmtOptions)> {
let matches = uu_app().try_get_matches_from(args)?; let matches = uu_app().try_get_matches_from(args)?;
let mut files: Vec<String> = matches let mut files: Vec<String> = matches
@ -177,20 +183,37 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
files.push("-".to_owned()); files.push("-".to_owned());
} }
let mut ostream = BufWriter::new(stdout()); Ok((files, fmt_opts))
}
for i in files.iter().map(|x| &x[..]) { /// Process the content of a file and format it according to the provided options.
let mut fp = match i { ///
/// # Arguments
///
/// * `file_name` - The name of the file to process. A value of "-" represents the standard input.
/// * `fmt_opts` - A reference to a `FmtOptions` struct containing the formatting options.
/// * `ostream` - A mutable reference to a `BufWriter` wrapping the standard output.
///
/// # Returns
///
/// A `UResult<()>` indicating success or failure.
fn process_file(
file_name: &str,
fmt_opts: &FmtOptions,
ostream: &mut BufWriter<Stdout>,
) -> UResult<()> {
let mut fp = match file_name {
"-" => BufReader::new(Box::new(stdin()) as Box<dyn Read + 'static>), "-" => BufReader::new(Box::new(stdin()) as Box<dyn Read + 'static>),
_ => match File::open(i) { _ => match File::open(file_name) {
Ok(f) => BufReader::new(Box::new(f) as Box<dyn Read + 'static>), Ok(f) => BufReader::new(Box::new(f) as Box<dyn Read + 'static>),
Err(e) => { Err(e) => {
show_warning!("{}: {}", i.maybe_quote(), e); show_warning!("{}: {}", file_name.maybe_quote(), e);
continue; return Ok(());
} }
}, },
}; };
let p_stream = ParagraphStream::new(&fmt_opts, &mut fp);
let p_stream = ParagraphStream::new(fmt_opts, &mut fp);
for para_result in p_stream { for para_result in p_stream {
match para_result { match para_result {
Err(s) => { Err(s) => {
@ -201,7 +224,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.write_all(b"\n") .write_all(b"\n")
.map_err_context(|| "failed to write output".to_string())?; .map_err_context(|| "failed to write output".to_string())?;
} }
Ok(para) => break_lines(&para, &fmt_opts, &mut ostream) Ok(para) => break_lines(&para, fmt_opts, ostream)
.map_err_context(|| "failed to write output".to_string())?, .map_err_context(|| "failed to write output".to_string())?,
} }
} }
@ -210,6 +233,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
ostream ostream
.flush() .flush()
.map_err_context(|| "failed to write output".to_string())?; .map_err_context(|| "failed to write output".to_string())?;
Ok(())
}
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let (files, fmt_opts) = parse_arguments(args)?;
let mut ostream = BufWriter::new(stdout());
for file_name in &files {
process_file(file_name, &fmt_opts, &mut ostream)?;
} }
Ok(()) Ok(())