1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

Merge pull request #2825 from jfinkels/stat-uresult

stat: return UResult from uumain() function
This commit is contained in:
Sylvestre Ledru 2021-12-31 11:06:02 +01:00 committed by GitHub
commit cb051e7416
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,7 @@
extern crate uucore; extern crate uucore;
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::entries; use uucore::entries;
use uucore::error::{UResult, USimpleError};
use uucore::fs::display_permissions; use uucore::fs::display_permissions;
use uucore::fsext::{ use uucore::fsext::{
pretty_filetype, pretty_fstype, pretty_time, read_fs_list, statfs, BirthTime, FsMeta, pretty_filetype, pretty_fstype, pretty_time, read_fs_list, statfs, BirthTime, FsMeta,
@ -25,7 +26,10 @@ use std::{cmp, fs, iter};
macro_rules! check_bound { macro_rules! check_bound {
($str: ident, $bound:expr, $beg: expr, $end: expr) => { ($str: ident, $bound:expr, $beg: expr, $end: expr) => {
if $end >= $bound { if $end >= $bound {
return Err(format!("{}: invalid directive", $str[$beg..$end].quote())); return Err(USimpleError::new(
1,
format!("{}: invalid directive", $str[$beg..$end].quote()),
));
} }
}; };
} }
@ -332,7 +336,7 @@ fn print_it(arg: &str, output_type: OutputType, flag: u8, width: usize, precisio
} }
impl Stater { impl Stater {
pub fn generate_tokens(format_str: &str, use_printf: bool) -> Result<Vec<Token>, String> { pub fn generate_tokens(format_str: &str, use_printf: bool) -> UResult<Vec<Token>> {
let mut tokens = Vec::new(); let mut tokens = Vec::new();
let bound = format_str.len(); let bound = format_str.len();
let chars = format_str.chars().collect::<Vec<char>>(); let chars = format_str.chars().collect::<Vec<char>>();
@ -457,7 +461,7 @@ impl Stater {
Ok(tokens) Ok(tokens)
} }
fn new(matches: ArgMatches) -> Result<Stater, String> { fn new(matches: ArgMatches) -> UResult<Stater> {
let files: Vec<String> = matches let files: Vec<String> = matches
.values_of(ARG_FILES) .values_of(ARG_FILES)
.map(|v| v.map(ToString::to_string).collect()) .map(|v| v.map(ToString::to_string).collect())
@ -476,14 +480,12 @@ impl Stater {
let show_fs = matches.is_present(options::FILE_SYSTEM); let show_fs = matches.is_present(options::FILE_SYSTEM);
let default_tokens = if format_str.is_empty() { let default_tokens = if format_str.is_empty() {
Stater::generate_tokens(&Stater::default_format(show_fs, terse, false), use_printf) Stater::generate_tokens(&Stater::default_format(show_fs, terse, false), use_printf)?
.unwrap()
} else { } else {
Stater::generate_tokens(format_str, use_printf)? Stater::generate_tokens(format_str, use_printf)?
}; };
let default_dev_tokens = let default_dev_tokens =
Stater::generate_tokens(&Stater::default_format(show_fs, terse, true), use_printf) Stater::generate_tokens(&Stater::default_format(show_fs, terse, true), use_printf)?;
.unwrap();
let mount_list = if show_fs { let mount_list = if show_fs {
// mount points aren't displayed when showing filesystem information // mount points aren't displayed when showing filesystem information
@ -945,7 +947,8 @@ for details about the options it supports.
) )
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let usage = usage(); let usage = usage();
let long_usage = get_long_usage(); let long_usage = get_long_usage();
@ -954,12 +957,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.after_help(&long_usage[..]) .after_help(&long_usage[..])
.get_matches_from(args); .get_matches_from(args);
match Stater::new(matches) { let stater = Stater::new(matches)?;
Ok(stater) => stater.exec(), let exit_status = stater.exec();
Err(e) => { if exit_status == 0 {
show_error!("{}", e); Ok(())
1 } else {
} Err(exit_status.into())
} }
} }