agent: use chain of Result to avoid early return

Use rust `Result`'s `or_else`/`and_then` can write clean codes.
And can avoid early return by check wether the `Result`
is `Ok` or `Err`.

Signed-off-by: bin liu <bin@hyper.sh>
This commit is contained in:
bin liu 2020-10-10 22:22:54 +08:00
parent b7309943af
commit eae685dc53

View File

@ -222,23 +222,17 @@ fn get_bool_value(param: &str) -> Result<bool> {
let v = fields[1]; let v = fields[1];
// bool // first try to parse as bool value
let t: std::result::Result<bool, std::str::ParseBoolError> = v.parse(); v.parse::<bool>().or_else(|_err1| {
if t.is_ok() { // then try to parse as integer value
return Ok(t.unwrap()); v.parse::<u64>().or_else(|_err2| Ok(0)).and_then(|v| {
}
// integer
let i: std::result::Result<u64, std::num::ParseIntError> = v.parse();
if i.is_err() {
return Ok(false);
}
// only `0` returns false, otherwise returns true // only `0` returns false, otherwise returns true
Ok(match i.unwrap() { Ok(match v {
0 => false, 0 => false,
_ => true, _ => true,
}) })
})
})
} }
fn get_container_pipe_size(param: &str) -> Result<i32> { fn get_container_pipe_size(param: &str) -> Result<i32> {