diff options
author | Kai Sickeler <k.sickeler@gmail.com> | 2016-07-14 19:25:58 +0200 |
---|---|---|
committer | Kai Sickeler <k.sickeler@gmail.com> | 2016-07-27 17:40:20 +0200 |
commit | 33db6da554e0e2354a2e8c20d873322f32be8d4e (patch) | |
tree | 0bbc3ebc39029cea74e25d4570388400615c9c35 /libimagcounter | |
parent | 76fba4622423e3625717f8d66f34c96b51106eb9 (diff) |
added measurement units
Diffstat (limited to 'libimagcounter')
-rw-r--r-- | libimagcounter/src/counter.rs | 19 |
1 files changed, 18 insertions, 1 deletions
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<Counter> { + pub fn new(store: &Store, name: CounterName, init: i64, unit: CounterUnit) -> Result<Counter> { 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<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> { debug!("Loading counter: '{}'", name); match store.retrieve(ModuleEntryPath::new(name).into_storeid()) { |