mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37: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:
parent
35a9b7f1fb
commit
6c26976edb
2 changed files with 19 additions and 9 deletions
|
@ -11,6 +11,8 @@ extern crate uucore;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use uucore::error::{UResult, USimpleError};
|
||||||
|
|
||||||
use clap::{crate_version, App, Arg};
|
use clap::{crate_version, App, Arg};
|
||||||
|
|
||||||
static ABOUT: &str = "Pause for NUMBER seconds.";
|
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 usage = get_usage();
|
||||||
|
|
||||||
let matches = uu_app().usage(&usage[..]).get_matches_from(args);
|
let matches = uu_app().usage(&usage[..]).get_matches_from(args);
|
||||||
|
|
||||||
if let Some(values) = matches.values_of(options::NUMBER) {
|
if let Some(values) = matches.values_of(options::NUMBER) {
|
||||||
let numbers = values.collect();
|
let numbers = values.collect();
|
||||||
sleep(numbers);
|
return sleep(numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
0
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> App<'static, 'static> {
|
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 =
|
let sleep_dur =
|
||||||
args.iter().fold(
|
args.iter().try_fold(
|
||||||
Duration::new(0, 0),
|
Duration::new(0, 0),
|
||||||
|result, arg| match uucore::parse_time::from_str(&arg[..]) {
|
|result, arg| match uucore::parse_time::from_str(&arg[..]) {
|
||||||
Ok(m) => m + result,
|
Ok(m) => Ok(m + result),
|
||||||
Err(f) => crash!(1, "{}", f),
|
Err(f) => Err(USimpleError::new(1, format!("{}", f))),
|
||||||
},
|
},
|
||||||
);
|
)?;
|
||||||
|
|
||||||
thread::sleep(sleep_dur);
|
Ok(thread::sleep(sleep_dur))
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,3 +110,10 @@ fn test_sleep_sum_duration_many() {
|
||||||
let duration = before_test.elapsed();
|
let duration = before_test.elapsed();
|
||||||
assert!(duration >= millis_900);
|
assert!(duration >= millis_900);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sleep_wrong_time() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["0.1s", "abc"])
|
||||||
|
.fails();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue