diff --git a/imag-counter/src/create.rs b/imag-counter/src/create.rs index 3b3ccb6c..9b455df5 100644 --- a/imag-counter/src/create.rs +++ b/imag-counter/src/create.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use libimagrt::runtime::Runtime; use libimagerror::trace::trace_error_exit; use libimagcounter::counter::Counter; +use libimagcounter::counter::CounterUnit; pub fn create(rt: &Runtime) { rt.cli() @@ -16,12 +17,15 @@ pub fn create(rt: &Runtime) { .and_then(|i| FromStr::from_str(i).ok()) .unwrap_or(0); - match Counter::new(rt.store(), String::from(name), init) { - Err(e) => { + let unit = scmd + .value_of("unit") + .map(CounterUnit::new); + + 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); trace_error_exit(&e, 1); - }, - Ok(_) => info!("Created Counter '{}' with initial value '{}'", name, init), - } + }); }); } diff --git a/imag-counter/src/list.rs b/imag-counter/src/list.rs index 57f1ef32..0c16c019 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 { + } else if unit.is_none() { println!("{} - {}", name.unwrap(), value.unwrap()); + } else { + 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..7fdc8a66 100644 --- a/libimagcounter/src/counter.rs +++ b/libimagcounter/src/counter.rs @@ -3,6 +3,8 @@ use std::ops::DerefMut; use toml::Value; use std::collections::BTreeMap; +use std::fmt; +use std::fmt::Display; use libimagstore::store::Store; use libimagstore::storeid::StoreIdIterator; @@ -14,11 +16,28 @@ use module_path::ModuleEntryPath; use result::Result; use error::CounterError as CE; use error::CounterErrorKind as CEK; +use error::error::MapErrInto; pub type CounterName = String; +#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] +pub struct CounterUnit(String); + +impl Display for CounterUnit { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "({})", self.0) + } +} + +impl CounterUnit { + pub fn new>(unit: S) -> CounterUnit { + CounterUnit(unit.into()) + } +} + pub struct Counter<'a> { fle: FileLockEntry<'a>, + unit: Option, } impl<'a> Counter<'a> { @@ -56,7 +75,21 @@ impl<'a> Counter<'a> { lockentry }; - Ok(Counter { fle: fle }) + Ok(Counter { fle: fle, unit: None }) + } + + pub fn with_unit(mut self, unit: Option) -> Result> { + self.unit = unit; + + if let Some(u) = self.unit.clone() { + let mut header = self.fle.deref_mut().get_header_mut(); + let setres = header.set("counter.unit", Value::String(u.0)); + if setres.is_err() { + self.unit = None; + return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err())))); + } + }; + Ok(self) } pub fn inc(&mut self) -> Result<()> { @@ -121,12 +154,24 @@ impl<'a> Counter<'a> { }) } + pub fn unit(&self) -> Option<&CounterUnit> { + self.unit.as_ref() + } + + pub fn read_unit(&self) -> Result> { + self.fle.get_header().read("counter.unit") + .map_err_into(CEK::StoreReadError) + .and_then(|s| match s { + Some(Value::String(s)) => Ok(Some(CounterUnit::new(s))), + Some(_) => Err(CE::new(CEK::HeaderTypeError, None)), + None => Ok(None), + }) + } + pub fn load(name: CounterName, store: &Store) -> Result { debug!("Loading counter: '{}'", name); - match store.retrieve(ModuleEntryPath::new(name).into_storeid()) { - Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), - Ok(c) => Ok(Counter { fle: c }), - } + let id = ModuleEntryPath::new(name).into_storeid(); + Counter::from_storeid(store, id) } pub fn delete(name: CounterName, store: &Store) -> Result<()> { @@ -153,7 +198,15 @@ impl<'a> FromStoreId for Counter<'a> { debug!("Loading counter from storeid: '{:?}'", id); match store.retrieve(id) { Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), - Ok(c) => Ok(Counter { fle: c }), + Ok(c) => { + let mut counter = Counter { fle: c, unit: None }; + counter.read_unit() + .map_err_into(CEK::StoreReadError) + .and_then(|u| { + counter.unit = u; + Ok(counter) + }) + } } }