added Option<CounterUnit> as member of Struct Counter

This commit is contained in:
Kai Sickeler 2016-07-18 17:05:02 +02:00
parent aeb27072a7
commit 769512ada2
3 changed files with 32 additions and 19 deletions

View file

@ -3,7 +3,6 @@ use std::str::FromStr;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error_exit; use libimagerror::trace::trace_error_exit;
use libimagcounter::counter::Counter; use libimagcounter::counter::Counter;
use libimagcounter::counter::CounterUnit;
pub fn create(rt: &Runtime) { pub fn create(rt: &Runtime) {
rt.cli() rt.cli()
@ -20,7 +19,7 @@ pub fn create(rt: &Runtime) {
.value_of("unit") .value_of("unit")
.unwrap_or("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) => { Err(e) => {
warn!("Could not create Counter '{}' with initial value '{} {}'", name, init, unit); warn!("Could not create Counter '{}' with initial value '{} {}'", name, init, unit);
trace_error_exit(&e, 1); trace_error_exit(&e, 1);

View file

@ -19,8 +19,8 @@ pub fn list(rt: &Runtime) {
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() { } else if unit.is_none() {
trace_error(&unit.unwrap_err()); println!("{} - {}", name.unwrap(), value.unwrap());
} else { } else {
println!("{} - {} {}", name.unwrap(), value.unwrap(), unit.unwrap()); println!("{} - {} {}", name.unwrap(), value.unwrap(), unit.unwrap());
} }

View file

@ -11,7 +11,6 @@ use libimagstore::storeid::StoreIdIterator;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::IntoStoreId;
use libimagerror::into::IntoError;
use module_path::ModuleEntryPath; use module_path::ModuleEntryPath;
use result::Result; use result::Result;
@ -37,11 +36,12 @@ impl CounterUnit {
pub struct Counter<'a> { pub struct Counter<'a> {
fle: FileLockEntry<'a>, fle: FileLockEntry<'a>,
unit: Option<CounterUnit>,
} }
impl<'a> Counter<'a> { impl<'a> Counter<'a> {
pub fn new(store: &Store, name: CounterName, init: i64, unit: CounterUnit) -> Result<Counter> { pub fn new(store: &Store, name: CounterName, init: i64) -> 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);
@ -65,8 +65,7 @@ impl<'a> Counter<'a> {
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.value", Value::Integer(init)) let setres = header.set("counter.value", Value::Integer(init));
.and_then(|_| header.set("counter.unit", Value::String(unit.clone().0)));
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()))));
} }
@ -75,7 +74,20 @@ impl<'a> Counter<'a> {
lockentry lockentry
}; };
Ok(Counter { fle: fle }) Ok(Counter { fle: fle, unit: None })
}
pub fn with_unit(mut self, unit: Option<CounterUnit>) -> Result<Counter<'a>> {
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<()> { pub fn inc(&mut self) -> Result<()> {
@ -140,21 +152,19 @@ impl<'a> Counter<'a> {
}) })
} }
pub fn unit(&self) -> Result<CounterUnit> { pub fn unit(&self) -> Option<CounterUnit> {
self.fle.get_header().read("counter.unit") self.fle.get_header().read("counter.unit")
.map_err(|e| CEK::StoreWriteError.into_error_with_cause(Box::new(e))) .ok()
.and_then(|u| match u { .and_then(|s| match s {
Some(Value::String(s)) => Ok(CounterUnit(s)), Some(Value::String(s)) => Some(CounterUnit::new(s)),
_ => Err(CEK::HeaderTypeError.into_error()) _ => 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()) { let id = ModuleEntryPath::new(name).into_storeid();
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), Counter::from_storeid(store, id)
Ok(c) => Ok(Counter { fle: c }),
}
} }
pub fn delete(name: CounterName, store: &Store) -> Result<()> { pub fn delete(name: CounterName, store: &Store) -> Result<()> {
@ -181,7 +191,11 @@ impl<'a> FromStoreId for Counter<'a> {
debug!("Loading counter from storeid: '{:?}'", id); debug!("Loading counter from storeid: '{:?}'", id);
match store.retrieve(id) { match store.retrieve(id) {
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), 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)
}
} }
} }