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

sleep: use UResult (#2492)

* sleep: use UResult in util

* sleep: add in error + test for it

* sleep: UResult - removed some verbosity
This commit is contained in:
Son Nguyen 2021-07-12 11:20:23 -07:00 committed by GitHub
parent 35a9b7f1fb
commit 6c26976edb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View file

@ -11,6 +11,8 @@ extern crate uucore;
use std::thread;
use std::time::Duration;
use uucore::error::{UResult, USimpleError};
use clap::{crate_version, App, Arg};
static ABOUT: &str = "Pause for NUMBER seconds.";
@ -32,17 +34,18 @@ fn get_usage() -> String {
)
}
pub fn uumain(args: impl uucore::Args) -> i32 {
#[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let usage = get_usage();
let matches = uu_app().usage(&usage[..]).get_matches_from(args);
if let Some(values) = matches.values_of(options::NUMBER) {
let numbers = values.collect();
sleep(numbers);
return sleep(numbers);
}
0
Ok(())
}
pub fn uu_app() -> App<'static, 'static> {
@ -61,15 +64,15 @@ pub fn uu_app() -> App<'static, 'static> {
)
}
fn sleep(args: Vec<&str>) {
fn sleep(args: Vec<&str>) -> UResult<()> {
let sleep_dur =
args.iter().fold(
args.iter().try_fold(
Duration::new(0, 0),
|result, arg| match uucore::parse_time::from_str(&arg[..]) {
Ok(m) => m + result,
Err(f) => crash!(1, "{}", f),
Ok(m) => Ok(m + result),
Err(f) => Err(USimpleError::new(1, format!("{}", f))),
},
);
)?;
thread::sleep(sleep_dur);
Ok(thread::sleep(sleep_dur))
}

View file

@ -110,3 +110,10 @@ fn test_sleep_sum_duration_many() {
let duration = before_test.elapsed();
assert!(duration >= millis_900);
}
#[test]
fn test_sleep_wrong_time() {
new_ucmd!()
.args(&["0.1s", "abc"])
.fails();
}