[common][server] Get returns Option
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Armin Friedl 2020-02-09 13:30:19 +01:00
parent 1e43bd5a7b
commit 7833fcd354
Signed by: armin
GPG key ID: 48C726EEE7FBCBC8
2 changed files with 15 additions and 17 deletions

View file

@ -13,9 +13,11 @@ use std::{
path::Path,
io::BufReader,
fs::File,
ops::Deref
};
#[allow(unused_imports)]
use std::ops::Deref; // we use this but rustc doesn't know
use quick_error::quick_error;
use seckey::SecKey;

View file

@ -61,33 +61,29 @@ impl Coffer for CofferMap {
}
}
fn get(&self, key: &CofferKey) -> CofferResult<CofferValue> {
fn get(&self, key: &CofferKey) -> Option<CofferValue> {
let lock = self.read();
let res = lock.get(&key.shard)
lock.get(&key.shard)
.and_then( |shard| { shard.get(&key.key) } )
.ok_or(CofferError::Msg("Key not found"))?;
Ok(res.clone())
.map(|o| o.clone())
}
fn get_shard<T>(&self, shard: T) -> CofferResult<CofferShard>
fn get_shard<T>(&self, shard: T) -> Option<CofferShard>
where T: AsRef<str>
{
let lock = self.read();
debug!{"Coffer {:?}", *lock}
let coffer_shard = lock.get(shard.as_ref())
.ok_or(CofferError::Msg("Shard not found"))?;
let map_to_vec = |map: &HashMap<String, CofferValue>| {
map.iter()
.map(|(k,v)| (k.clone(), v.clone()))
.collect::<Vec<(String, CofferValue)>>()
};
let mut res = CofferShard(Vec::new());
for (k,v) in coffer_shard {
res.0.push((k.clone(), v.clone()));
}
Ok(res)
lock.get(shard.as_ref())
.and_then(|s| Some(CofferShard(map_to_vec(s))))
}
}