diff --git a/imag-counter/src/create.rs b/imag-counter/src/create.rs index 3b3ccb6c..8bfcba67 100644 --- a/imag-counter/src/create.rs +++ b/imag-counter/src/create.rs @@ -15,13 +15,16 @@ pub fn create(rt: &Runtime) { .value_of("initval") .and_then(|i| FromStr::from_str(i).ok()) .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) => { - 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); }, - Ok(_) => info!("Created Counter '{}' with initial value '{}'", name, init), + Ok(_) => info!("Created Counter '{}' with initial value '{} {}'", name, init, unit), } }); } diff --git a/imag-counter/src/list.rs b/imag-counter/src/list.rs index 57f1ef32..59651a4c 100644 --- a/imag-counter/src/list.rs +++ b/imag-counter/src/list.rs @@ -13,13 +13,16 @@ pub fn list(rt: &Runtime) { counter.map(|c| { let name = c.name(); let value = c.value(); + let unit = c.unit(); if name.is_err() { trace_error(&name.unwrap_err()); } else if value.is_err() { trace_error(&value.unwrap_err()); + } else if unit.is_err() { + trace_error(&unit.unwrap_err()); } else { - println!("{} - {}", name.unwrap(), value.unwrap()); + println!("{} - {} {}", name.unwrap(), value.unwrap(), unit.unwrap()); } }) .map_err(|e| trace_error(&e)) diff --git a/imag-counter/src/ui.rs b/imag-counter/src/ui.rs index b942498d..2218076e 100644 --- a/imag-counter/src/ui.rs +++ b/imag-counter/src/ui.rs @@ -48,7 +48,14 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .takes_value(true) .required(false) .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") .about("Delete a counter") diff --git a/libimagcounter/src/counter.rs b/libimagcounter/src/counter.rs index a93046f7..04e186f5 100644 --- a/libimagcounter/src/counter.rs +++ b/libimagcounter/src/counter.rs @@ -16,6 +16,7 @@ use error::CounterError as CE; use error::CounterErrorKind as CEK; pub type CounterName = String; +pub type CounterUnit = String; pub struct Counter<'a> { fle: FileLockEntry<'a>, @@ -23,7 +24,7 @@ pub struct Counter<'a> { impl<'a> Counter<'a> { - pub fn new(store: &Store, name: CounterName, init: i64) -> Result { + pub fn new(store: &Store, name: CounterName, init: i64, unit: CounterUnit) -> Result { use std::ops::DerefMut; debug!("Creating new counter: '{}' with value: {}", name, init); @@ -51,6 +52,11 @@ impl<'a> Counter<'a> { if setres.is_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 @@ -121,6 +127,17 @@ impl<'a> Counter<'a> { }) } + pub fn unit(&self) -> Result { + 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 { debug!("Loading counter: '{}'", name); match store.retrieve(ModuleEntryPath::new(name).into_storeid()) {