From 769512ada244d0a098acb0ba2ab39a420b815af1 Mon Sep 17 00:00:00 2001 From: Kai Sickeler Date: Mon, 18 Jul 2016 17:05:02 +0200 Subject: [PATCH] added Option as member of Struct Counter --- imag-counter/src/create.rs | 3 +-- imag-counter/src/list.rs | 4 ++-- libimagcounter/src/counter.rs | 44 +++++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/imag-counter/src/create.rs b/imag-counter/src/create.rs index dab5e972..04ec3bb7 100644 --- a/imag-counter/src/create.rs +++ b/imag-counter/src/create.rs @@ -3,7 +3,6 @@ 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() @@ -20,7 +19,7 @@ pub fn create(rt: &Runtime) { .value_of("unit") .unwrap_or("unit"); - match Counter::new(rt.store(), String::from(name), init, CounterUnit::new(unit)) { + match Counter::new(rt.store(), String::from(name), init) { Err(e) => { warn!("Could not create Counter '{}' with initial value '{} {}'", name, init, unit); trace_error_exit(&e, 1); diff --git a/imag-counter/src/list.rs b/imag-counter/src/list.rs index 59651a4c..0c16c019 100644 --- a/imag-counter/src/list.rs +++ b/imag-counter/src/list.rs @@ -19,8 +19,8 @@ pub fn list(rt: &Runtime) { 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 if unit.is_none() { + println!("{} - {}", name.unwrap(), value.unwrap()); } else { println!("{} - {} {}", name.unwrap(), value.unwrap(), unit.unwrap()); } diff --git a/libimagcounter/src/counter.rs b/libimagcounter/src/counter.rs index baba38a4..12182855 100644 --- a/libimagcounter/src/counter.rs +++ b/libimagcounter/src/counter.rs @@ -11,7 +11,6 @@ use libimagstore::storeid::StoreIdIterator; use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreId; use libimagstore::storeid::IntoStoreId; -use libimagerror::into::IntoError; use module_path::ModuleEntryPath; use result::Result; @@ -37,11 +36,12 @@ impl CounterUnit { pub struct Counter<'a> { fle: FileLockEntry<'a>, + unit: Option, } impl<'a> Counter<'a> { - pub fn new(store: &Store, name: CounterName, init: i64, unit: CounterUnit) -> Result { + pub fn new(store: &Store, name: CounterName, init: i64) -> Result { use std::ops::DerefMut; debug!("Creating new counter: '{}' with value: {}", name, init); @@ -65,8 +65,7 @@ impl<'a> Counter<'a> { return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err())))); } - let setres = header.set("counter.value", Value::Integer(init)) - .and_then(|_| header.set("counter.unit", Value::String(unit.clone().0))); + let setres = header.set("counter.value", Value::Integer(init)); if setres.is_err() { return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err())))); } @@ -75,7 +74,20 @@ 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() { + return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err())))); + } + }; + Ok(self) } pub fn inc(&mut self) -> Result<()> { @@ -140,21 +152,19 @@ impl<'a> Counter<'a> { }) } - pub fn unit(&self) -> Result { + pub fn unit(&self) -> Option { self.fle.get_header().read("counter.unit") - .map_err(|e| CEK::StoreWriteError.into_error_with_cause(Box::new(e))) - .and_then(|u| match u { - Some(Value::String(s)) => Ok(CounterUnit(s)), - _ => Err(CEK::HeaderTypeError.into_error()) + .ok() + .and_then(|s| match s { + Some(Value::String(s)) => Some(CounterUnit::new(s)), + _ => 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<()> { @@ -181,7 +191,11 @@ 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.unit = counter.unit(); + Ok(counter) + } } }