From 2106ddd0889a4d0191eaa40978787e9a96a62bff Mon Sep 17 00:00:00 2001 From: Felipe Lema <1232306+FelipeLema@users.noreply.github.com> Date: Tue, 27 Jul 2021 00:07:32 -0400 Subject: [PATCH 1/2] migrate `cat` code to UResult #2464 --- src/uu/cat/src/cat.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 8ad563c5d..cf2ca8bdd 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -23,6 +23,7 @@ use clap::{crate_version, App, Arg}; use std::fs::{metadata, File}; use std::io::{self, Read, Write}; use thiserror::Error; +use uucore::error::UResult; /// Linux splice support #[cfg(any(target_os = "linux", target_os = "android"))] @@ -167,7 +168,8 @@ mod options { 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 .collect_str(InvalidEncodingHandling::Ignore) .accept_any(); @@ -220,13 +222,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { show_tabs, squeeze_blank, }; - let success = cat_files(files, &options).is_ok(); - - if success { - 0 - } else { - 1 - } + cat_files(files, &options) } 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, options: &OutputOptions) -> Result<(), u32> { - let mut error_count = 0; +fn cat_files(files: Vec, options: &OutputOptions) -> UResult<()> { let mut state = OutputState { line_number: 1, at_line_start: true, }; + let mut error_messages : Vec = Vec::new(); for path in &files { if let Err(err) = cat_path(path, options, &mut state) { - show_error!("{}: {}", path, err); - error_count += 1; + error_messages.push( + format!("{}: {}", path, err)); } } - if error_count == 0 { + if error_messages.len() == 0 { Ok(()) } 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())) } } From 5646c8133283af95a5a1fa6fbf7f896b443c3440 Mon Sep 17 00:00:00 2001 From: Felipe Lema <1232306+FelipeLema@users.noreply.github.com> Date: Sun, 1 Aug 2021 23:55:30 -0400 Subject: [PATCH 2/2] correct clippy issues, RustFmt --- src/uu/cat/src/cat.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index cf2ca8bdd..c1ca5ec7a 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -343,24 +343,23 @@ fn cat_files(files: Vec, options: &OutputOptions) -> UResult<()> { line_number: 1, at_line_start: true, }; - let mut error_messages : Vec = Vec::new(); + let mut error_messages: Vec = Vec::new(); for path in &files { if let Err(err) = cat_path(path, options, &mut state) { - error_messages.push( - format!("{}: {}", path, err)); + error_messages.push(format!("{}: {}", path, err)); } } - if error_messages.len() == 0 { + if error_messages.is_empty() { Ok(()) } else { // each next line is expected to display "cat: …" - let line_joiner = format!("\n{}: ", - executable!()).to_owned(); + let line_joiner = format!("\n{}: ", executable!()); Err(uucore::error::USimpleError::new( - error_messages.len() as i32, - error_messages.join(&line_joiner).into())) + error_messages.len() as i32, + error_messages.join(&line_joiner), + )) } }