From 2cbc2aa59bd7d154125ab20b1f168ea6f8617cc8 Mon Sep 17 00:00:00 2001 From: Smicry Date: Wed, 20 Oct 2021 23:49:04 +0800 Subject: [PATCH 1/3] head: use UResult in util --- src/uu/head/src/head.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/uu/head/src/head.rs b/src/uu/head/src/head.rs index 42b7c0fda..93404876d 100644 --- a/src/uu/head/src/head.rs +++ b/src/uu/head/src/head.rs @@ -10,10 +10,9 @@ use std::convert::TryFrom; use std::ffi::OsString; use std::io::{self, ErrorKind, Read, Seek, SeekFrom, Write}; use uucore::display::Quotable; -use uucore::{crash, show_error_custom_description}; +use uucore::error::{UResult, USimpleError}; +use uucore::show_error_custom_description; -const EXIT_FAILURE: i32 = 1; -const EXIT_SUCCESS: i32 = 0; const BUF_SIZE: usize = 65536; const ABOUT: &str = "\ @@ -372,7 +371,7 @@ fn head_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Resul } } -fn uu_head(options: &HeadOptions) -> Result<(), u32> { +fn uu_head(options: &HeadOptions) -> UResult<()> { let mut error_count = 0; let mut first = true; for file in &options.files { @@ -445,23 +444,21 @@ fn uu_head(options: &HeadOptions) -> Result<(), u32> { first = false; } if error_count > 0 { - Err(error_count) + Err(USimpleError::new(1, format!(""))) } else { Ok(()) } } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = match HeadOptions::get_from(args) { Ok(o) => o, Err(s) => { - crash!(EXIT_FAILURE, "{}", s); + return Err(USimpleError::new(1, format!("{}", s))); } }; - match uu_head(&args) { - Ok(_) => EXIT_SUCCESS, - Err(_) => EXIT_FAILURE, - } + uu_head(&args) } #[cfg(test)] From 396fa7a9b4037166dae1e63ca6007f8184694ecb Mon Sep 17 00:00:00 2001 From: Smicry Date: Thu, 21 Oct 2021 00:13:28 +0800 Subject: [PATCH 2/3] fix lint error --- src/uu/head/src/head.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/head/src/head.rs b/src/uu/head/src/head.rs index 93404876d..67f73a397 100644 --- a/src/uu/head/src/head.rs +++ b/src/uu/head/src/head.rs @@ -444,7 +444,7 @@ fn uu_head(options: &HeadOptions) -> UResult<()> { first = false; } if error_count > 0 { - Err(USimpleError::new(1, format!(""))) + Err(USimpleError::new(1, "")) } else { Ok(()) } @@ -455,7 +455,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = match HeadOptions::get_from(args) { Ok(o) => o, Err(s) => { - return Err(USimpleError::new(1, format!("{}", s))); + return Err(USimpleError::new(1, s.to_string())); } }; uu_head(&args) From b89b11f5daf1549a3a025b0e9a0c88f23ae51d1f Mon Sep 17 00:00:00 2001 From: Smicry Date: Thu, 21 Oct 2021 00:22:36 +0800 Subject: [PATCH 3/3] fix lint error --- src/uu/head/src/head.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/head/src/head.rs b/src/uu/head/src/head.rs index 67f73a397..c33ec693b 100644 --- a/src/uu/head/src/head.rs +++ b/src/uu/head/src/head.rs @@ -455,7 +455,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = match HeadOptions::get_from(args) { Ok(o) => o, Err(s) => { - return Err(USimpleError::new(1, s.to_string())); + return Err(USimpleError::new(1, s)); } }; uu_head(&args)