1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-14 11:07:59 +00:00

migrate cat code to UResult #2464

This commit is contained in:
Felipe Lema 2021-07-27 00:07:32 -04:00
parent 52c6593ef5
commit 2106ddd088

View file

@ -23,6 +23,7 @@ use clap::{crate_version, App, Arg};
use std::fs::{metadata, File}; use std::fs::{metadata, File};
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
use thiserror::Error; use thiserror::Error;
use uucore::error::UResult;
/// Linux splice support /// Linux splice support
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
@ -167,7 +168,8 @@ mod options {
pub static SHOW_NONPRINTING: &str = "show-nonprinting"; pub static SHOW_NONPRINTING: &str = "show-nonprinting";
} }
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::Ignore) .collect_str(InvalidEncodingHandling::Ignore)
.accept_any(); .accept_any();
@ -220,13 +222,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
show_tabs, show_tabs,
squeeze_blank, squeeze_blank,
}; };
let success = cat_files(files, &options).is_ok(); cat_files(files, &options)
if success {
0
} else {
1
}
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {
@ -342,23 +338,29 @@ fn cat_path(path: &str, options: &OutputOptions, state: &mut OutputState) -> Cat
} }
} }
fn cat_files(files: Vec<String>, options: &OutputOptions) -> Result<(), u32> { fn cat_files(files: Vec<String>, options: &OutputOptions) -> UResult<()> {
let mut error_count = 0;
let mut state = OutputState { let mut state = OutputState {
line_number: 1, line_number: 1,
at_line_start: true, at_line_start: true,
}; };
let mut error_messages : Vec<String> = Vec::new();
for path in &files { for path in &files {
if let Err(err) = cat_path(path, options, &mut state) { if let Err(err) = cat_path(path, options, &mut state) {
show_error!("{}: {}", path, err); error_messages.push(
error_count += 1; format!("{}: {}", path, err));
} }
} }
if error_count == 0 { if error_messages.len() == 0 {
Ok(()) Ok(())
} else { } else {
Err(error_count) // each next line is expected to display "cat: …"
let line_joiner = format!("\n{}: ",
executable!()).to_owned();
Err(uucore::error::USimpleError::new(
error_messages.len() as i32,
error_messages.join(&line_joiner).into()))
} }
} }