shorten header reading with header function

This commit is contained in:
Kai Sickeler 2016-07-27 19:09:41 +02:00
parent 1c5cb7d21d
commit 47d2a7703c

View file

@ -11,6 +11,7 @@ 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;
@ -49,7 +50,7 @@ impl<'a> Counter<'a> {
let fle = { let fle = {
let lockentry = store.create(ModuleEntryPath::new(name.clone()).into_storeid()); let lockentry = store.create(ModuleEntryPath::new(name.clone()).into_storeid());
if lockentry.is_err() { if lockentry.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(lockentry.err().unwrap())))); return Err(CEK::StoreWriteError.into_error())
} }
let mut lockentry = lockentry.unwrap(); let mut lockentry = lockentry.unwrap();
@ -58,17 +59,17 @@ impl<'a> Counter<'a> {
let mut header = entry.get_header_mut(); let mut header = entry.get_header_mut();
let setres = header.set("counter", Value::Table(BTreeMap::new())); let setres = header.set("counter", Value::Table(BTreeMap::new()));
if setres.is_err() { if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err())))); return Err(CEK::StoreWriteError.into_error());
} }
let setres = header.set("counter.name", Value::String(name)); let setres = header.set("counter.name", Value::String(name));
if setres.is_err() { if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err())))); return Err(CEK::StoreWriteError.into_error())
} }
let setres = header.set("counter.value", Value::Integer(init)); let setres = header.set("counter.value", Value::Integer(init));
if setres.is_err() { if setres.is_err() {
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err())))); return Err(CEK::StoreWriteError.into_error())
} }
} }
@ -86,7 +87,7 @@ impl<'a> Counter<'a> {
let setres = header.set("counter.unit", Value::String(u.0)); let setres = header.set("counter.unit", Value::String(u.0));
if setres.is_err() { if setres.is_err() {
self.unit = None; self.unit = None;
return Err(CE::new(CEK::StoreWriteError, Some(Box::new(setres.unwrap_err())))); return Err(CEK::StoreWriteError.into_error())
} }
}; };
Ok(self) Ok(self)
@ -97,7 +98,7 @@ impl<'a> Counter<'a> {
match header.read("counter.value") { match header.read("counter.value") {
Ok(Some(Value::Integer(i))) => { Ok(Some(Value::Integer(i))) => {
header.set("counter.value", Value::Integer(i + 1)) header.set("counter.value", Value::Integer(i + 1))
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e)))) .map_err_into(CEK::StoreWriteError)
.map(|_| ()) .map(|_| ())
}, },
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
@ -110,7 +111,7 @@ impl<'a> Counter<'a> {
match header.read("counter.value") { match header.read("counter.value") {
Ok(Some(Value::Integer(i))) => { Ok(Some(Value::Integer(i))) => {
header.set("counter.value", Value::Integer(i - 1)) header.set("counter.value", Value::Integer(i - 1))
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e)))) .map_err_into(CEK::StoreWriteError)
.map(|_| ()) .map(|_| ())
}, },
Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))),
@ -125,30 +126,22 @@ impl<'a> Counter<'a> {
pub fn set(&mut self, v: i64) -> Result<()> { pub fn set(&mut self, v: i64) -> Result<()> {
let mut header = self.fle.deref_mut().get_header_mut(); let mut header = self.fle.deref_mut().get_header_mut();
header.set("counter.value", Value::Integer(v)) header.set("counter.value", Value::Integer(v))
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e)))) .map_err_into(CEK::StoreWriteError)
.map(|_| ()) .map(|_| ())
} }
pub fn name(&self) -> Result<CounterName> { pub fn name(&self) -> Result<CounterName> {
self.fle.get_header().read("counter.name") self.read_header_at("counter.name", |v| match v {
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e)))) Some(Value::String(s)) => Ok(s),
.and_then(|v| { _ => Err(CEK::HeaderTypeError.into_error()),
match v { })
Some(Value::String(s)) => Ok(s),
_ => Err(CE::new(CEK::HeaderTypeError, None)),
}
})
} }
pub fn value(&self) -> Result<i64> { pub fn value(&self) -> Result<i64> {
self.fle.get_header().read("counter.value") self.read_header_at("counter.value", |v| match v {
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e)))) Some(Value::Integer(i)) => Ok(i),
.and_then(|v| { _ => Err(CEK::HeaderTypeError.into_error()),
match v { })
Some(Value::Integer(i)) => Ok(i),
_ => Err(CE::new(CEK::HeaderTypeError, None)),
}
})
} }
pub fn unit(&self) -> Option<&CounterUnit> { pub fn unit(&self) -> Option<&CounterUnit> {
@ -156,13 +149,17 @@ impl<'a> Counter<'a> {
} }
pub fn read_unit(&self) -> Result<Option<CounterUnit>> { pub fn read_unit(&self) -> Result<Option<CounterUnit>> {
self.fle.get_header().read("counter.unit") self.read_header_at("counter.unit", |s| match s {
.map_err_into(CEK::StoreReadError) Some(Value::String(s)) => Ok(Some(CounterUnit::new(s))),
.and_then(|s| match s { Some(_) => Err(CEK::HeaderTypeError.into_error()),
Some(Value::String(s)) => Ok(Some(CounterUnit::new(s))), None => Ok(None),
Some(_) => Err(CE::new(CEK::HeaderTypeError, None)), })
None => Ok(None), }
})
fn read_header_at<T, F>(&self, name: &str, f: F) -> Result<T>
where F: FnOnce(Option<Value>) -> Result<T>
{
self.fle.get_header().read(name).map_err_into(CEK::StoreWriteError).and_then(f)
} }
pub fn load(name: CounterName, store: &Store) -> Result<Counter> { pub fn load(name: CounterName, store: &Store) -> Result<Counter> {
@ -174,13 +171,13 @@ impl<'a> Counter<'a> {
pub fn delete(name: CounterName, store: &Store) -> Result<()> { pub fn delete(name: CounterName, store: &Store) -> Result<()> {
debug!("Deleting counter: '{}'", name); debug!("Deleting counter: '{}'", name);
store.delete(ModuleEntryPath::new(name).into_storeid()) store.delete(ModuleEntryPath::new(name).into_storeid())
.map_err(|e| CE::new(CEK::StoreWriteError, Some(Box::new(e)))) .map_err_into(CEK::StoreWriteError)
} }
pub fn all_counters(store: &Store) -> Result<CounterIterator> { pub fn all_counters(store: &Store) -> Result<CounterIterator> {
store.retrieve_for_module("counter") store.retrieve_for_module("counter")
.map(|iter| CounterIterator::new(store, iter)) .map(|iter| CounterIterator::new(store, iter))
.map_err(|e| CE::new(CEK::StoreReadError, Some(Box::new(e)))) .map_err_into(CEK::StoreReadError)
} }
} }