2016-02-13 19:25:11 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
2016-07-15 19:56:53 +00:00
|
|
|
use libimagerror::trace::trace_error_exit;
|
2016-02-13 19:25:11 +00:00
|
|
|
use libimagcounter::counter::Counter;
|
2016-07-18 20:05:18 +00:00
|
|
|
use libimagcounter::counter::CounterUnit;
|
2016-02-13 19:25:11 +00:00
|
|
|
|
|
|
|
pub fn create(rt: &Runtime) {
|
|
|
|
rt.cli()
|
|
|
|
.subcommand_matches("create")
|
|
|
|
.map(|scmd| {
|
|
|
|
debug!("Found 'create' subcommand...");
|
|
|
|
|
|
|
|
let name = scmd.value_of("name").unwrap(); // safe because clap enforces
|
|
|
|
let init : i64 = scmd
|
|
|
|
.value_of("initval")
|
|
|
|
.and_then(|i| FromStr::from_str(i).ok())
|
|
|
|
.unwrap_or(0);
|
2016-07-18 20:05:18 +00:00
|
|
|
|
2016-07-14 17:25:58 +00:00
|
|
|
let unit = scmd
|
|
|
|
.value_of("unit")
|
2016-07-18 20:05:18 +00:00
|
|
|
.map(CounterUnit::new);
|
2016-02-13 19:25:11 +00:00
|
|
|
|
2016-07-18 20:05:18 +00:00
|
|
|
Counter::new(rt.store(), String::from(name), init)
|
|
|
|
.and_then(|c| c.with_unit(unit))
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
warn!("Could not create Counter '{}' with initial value '{}'", name, init);
|
2016-07-15 19:56:53 +00:00
|
|
|
trace_error_exit(&e, 1);
|
2016-07-18 20:05:18 +00:00
|
|
|
});
|
2016-02-13 19:25:11 +00:00
|
|
|
});
|
|
|
|
}
|