imag-counter: Use utils to map over Err/Ok

This commit is contained in:
Matthias Beyer 2016-09-06 11:03:55 +02:00
parent 198170cf57
commit d5d83400fa
2 changed files with 10 additions and 29 deletions

View file

@ -1,5 +1,5 @@
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error; use libimagerror::trace::{MapErrTrace, trace_error};
use libimagcounter::counter::Counter; use libimagcounter::counter::Counter;
pub fn list(rt: &Runtime) { pub fn list(rt: &Runtime) {
@ -25,11 +25,11 @@ pub fn list(rt: &Runtime) {
println!("{} - {} {}", name.unwrap(), value.unwrap(), unit.unwrap()); println!("{} - {} {}", name.unwrap(), value.unwrap(), unit.unwrap());
} }
}) })
.map_err(|e| trace_error(&e)) .map_err_trace()
.ok(); .ok();
} }
}) })
.map_err(|e| trace_error(&e)) .map_err_trace()
}); });
} }

View file

@ -27,8 +27,9 @@ use std::str::FromStr;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagcounter::counter::Counter; use libimagcounter::counter::Counter;
use libimagerror::trace::{trace_error, trace_error_exit}; use libimagerror::trace::MapErrTrace;
use libimagutil::key_value_split::IntoKeyValue; use libimagutil::key_value_split::IntoKeyValue;
use libimagutil::info_result::*;
mod create; mod create;
mod delete; mod delete;
@ -73,30 +74,15 @@ fn main() {
match action { match action {
Action::Inc => { Action::Inc => {
Counter::load(String::from(name), rt.store()) Counter::load(String::from(name), rt.store())
.map(|mut counter| { .map(|mut c| c.inc().map_err_trace_exit(1).map_info_str("Ok"))
match counter.inc() {
Err(e) => trace_error_exit(&e, 1),
Ok(_) => info!("Ok"),
}
})
}, },
Action::Dec => { Action::Dec => {
Counter::load(String::from(name), rt.store()) Counter::load(String::from(name), rt.store())
.map(|mut counter| { .map(|mut c| c.dec().map_err_trace_exit(1).map_info_str("Ok"))
match counter.dec() {
Err(e) => trace_error_exit(&e, 1),
Ok(_) => info!("Ok"),
}
})
}, },
Action::Reset => { Action::Reset => {
Counter::load(String::from(name), rt.store()) Counter::load(String::from(name), rt.store())
.map(|mut counter| { .map(|mut c| c.reset().map_err_trace_exit(1).map_info_str("Ok"))
match counter.reset() {
Err(e) => trace_error_exit(&e, 1),
Ok(_) => info!("Ok"),
}
})
}, },
Action::Set => { Action::Set => {
let kv = String::from(name).into_kv(); let kv = String::from(name).into_kv();
@ -112,15 +98,10 @@ fn main() {
} }
let value : i64 = value.unwrap(); let value : i64 = value.unwrap();
Counter::load(String::from(key), rt.store()) Counter::load(String::from(key), rt.store())
.map(|mut counter| { .map(|mut c| c.set(value).map_err_trace_exit(1).map_info_str("Ok"))
match counter.set(value) {
Err(e) => trace_error_exit(&e, 1),
Ok(_) => info!("Ok"),
}
})
}, },
} }
.map_err(|e| trace_error(&e)) .map_err_trace()
.ok(); .ok();
}, },
|name| { |name| {