added measurement units
This commit is contained in:
parent
76fba46224
commit
33db6da554
4 changed files with 36 additions and 6 deletions
|
@ -15,13 +15,16 @@ pub fn create(rt: &Runtime) {
|
||||||
.value_of("initval")
|
.value_of("initval")
|
||||||
.and_then(|i| FromStr::from_str(i).ok())
|
.and_then(|i| FromStr::from_str(i).ok())
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
|
let unit = scmd
|
||||||
|
.value_of("unit")
|
||||||
|
.unwrap_or("unit");
|
||||||
|
|
||||||
match Counter::new(rt.store(), String::from(name), init) {
|
match Counter::new(rt.store(), String::from(name), init, String::from(unit)) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("Could not create Counter '{}' with initial value '{}'", name, init);
|
warn!("Could not create Counter '{}' with initial value '{} {}'", name, init, unit);
|
||||||
trace_error_exit(&e, 1);
|
trace_error_exit(&e, 1);
|
||||||
},
|
},
|
||||||
Ok(_) => info!("Created Counter '{}' with initial value '{}'", name, init),
|
Ok(_) => info!("Created Counter '{}' with initial value '{} {}'", name, init, unit),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,13 +13,16 @@ pub fn list(rt: &Runtime) {
|
||||||
counter.map(|c| {
|
counter.map(|c| {
|
||||||
let name = c.name();
|
let name = c.name();
|
||||||
let value = c.value();
|
let value = c.value();
|
||||||
|
let unit = c.unit();
|
||||||
|
|
||||||
if name.is_err() {
|
if name.is_err() {
|
||||||
trace_error(&name.unwrap_err());
|
trace_error(&name.unwrap_err());
|
||||||
} else if value.is_err() {
|
} else if value.is_err() {
|
||||||
trace_error(&value.unwrap_err());
|
trace_error(&value.unwrap_err());
|
||||||
|
} else if unit.is_err() {
|
||||||
|
trace_error(&unit.unwrap_err());
|
||||||
} else {
|
} else {
|
||||||
println!("{} - {}", name.unwrap(), value.unwrap());
|
println!("{} - {} {}", name.unwrap(), value.unwrap(), unit.unwrap());
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map_err(|e| trace_error(&e))
|
.map_err(|e| trace_error(&e))
|
||||||
|
|
|
@ -48,7 +48,14 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.required(false)
|
.required(false)
|
||||||
.help("Initial value")
|
.help("Initial value")
|
||||||
.value_name("VALUE")))
|
.value_name("VALUE"))
|
||||||
|
.arg(Arg::with_name("unit")
|
||||||
|
.long("unit")
|
||||||
|
.short("u")
|
||||||
|
.takes_value(true)
|
||||||
|
.required(false)
|
||||||
|
.help("measurement unit")
|
||||||
|
.value_name("UNIT")))
|
||||||
|
|
||||||
.subcommand(SubCommand::with_name("delete")
|
.subcommand(SubCommand::with_name("delete")
|
||||||
.about("Delete a counter")
|
.about("Delete a counter")
|
||||||
|
|
|
@ -16,6 +16,7 @@ use error::CounterError as CE;
|
||||||
use error::CounterErrorKind as CEK;
|
use error::CounterErrorKind as CEK;
|
||||||
|
|
||||||
pub type CounterName = String;
|
pub type CounterName = String;
|
||||||
|
pub type CounterUnit = String;
|
||||||
|
|
||||||
pub struct Counter<'a> {
|
pub struct Counter<'a> {
|
||||||
fle: FileLockEntry<'a>,
|
fle: FileLockEntry<'a>,
|
||||||
|
@ -23,7 +24,7 @@ pub struct Counter<'a> {
|
||||||
|
|
||||||
impl<'a> Counter<'a> {
|
impl<'a> Counter<'a> {
|
||||||
|
|
||||||
pub fn new(store: &Store, name: CounterName, init: i64) -> Result<Counter> {
|
pub fn new(store: &Store, name: CounterName, init: i64, unit: CounterUnit) -> Result<Counter> {
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
debug!("Creating new counter: '{}' with value: {}", name, init);
|
debug!("Creating new counter: '{}' with value: {}", name, init);
|
||||||
|
@ -51,6 +52,11 @@ impl<'a> Counter<'a> {
|
||||||
if setres.is_err() {
|
if setres.is_err() {
|
||||||
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
|
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let setres = header.set("counter.unit", Value::String(unit));
|
||||||
|
if setres.is_err() {
|
||||||
|
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err()))));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lockentry
|
lockentry
|
||||||
|
@ -121,6 +127,17 @@ impl<'a> Counter<'a> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn unit(&self) -> Result<CounterUnit> {
|
||||||
|
self.fle.get_header().read("counter.unit")
|
||||||
|
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e))))
|
||||||
|
.and_then(|u| {
|
||||||
|
match u {
|
||||||
|
Some(Value::String(s)) => Ok(s),
|
||||||
|
_ => Err(CE::new(CEK::HeaderTypeError, None)),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load(name: CounterName, store: &Store) -> Result<Counter> {
|
pub fn load(name: CounterName, store: &Store) -> Result<Counter> {
|
||||||
debug!("Loading counter: '{}'", name);
|
debug!("Loading counter: '{}'", name);
|
||||||
match store.retrieve(ModuleEntryPath::new(name).into_storeid()) {
|
match store.retrieve(ModuleEntryPath::new(name).into_storeid()) {
|
||||||
|
|
Loading…
Reference in a new issue