libimagentrycategory: Replace read with typed read

This commit is contained in:
Matthias Beyer 2018-01-12 16:32:18 +01:00
parent 163bf249ff
commit 7176c130ff
2 changed files with 14 additions and 24 deletions

View file

@ -19,6 +19,7 @@
use toml_query::insert::TomlValueInsertExt; use toml_query::insert::TomlValueInsertExt;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadExt;
use toml_query::read::TomlValueReadTypeExt;
use toml::Value; use toml::Value;
use libimagstore::store::Entry; use libimagstore::store::Entry;
@ -81,22 +82,16 @@ impl EntryCategory for Entry {
fn get_category(&self) -> Result<Option<Category>> { fn get_category(&self) -> Result<Option<Category>> {
self.get_header() self.get_header()
.read("category.value") .read_string("category.value")
.chain_err(|| CEK::HeaderReadError) .chain_err(|| CEK::HeaderReadError)
.and_then(|opt| { .and_then(|o| o.map(Category::from).ok_or(CE::from_kind(CEK::TypeError)))
opt.map(|v| { .map(Some)
v.as_str()
.map(String::from)
.map(Category::from)
})
.ok_or(CE::from_kind(CEK::TypeError))
})
} }
fn has_category(&self) -> Result<bool> { fn has_category(&self) -> Result<bool> {
self.get_header().read(&String::from("category.value")) self.get_header().read("category.value")
.chain_err(|| CEK::HeaderReadError) .chain_err(|| CEK::HeaderReadError)
.map(|e| e.is_some()) .map(|x| x.is_some())
} }
} }

View file

@ -20,7 +20,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use toml_query::insert::TomlValueInsertExt; use toml_query::insert::TomlValueInsertExt;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadTypeExt;
use toml::Value; use toml::Value;
use libimagstore::store::Store; use libimagstore::store::Store;
@ -194,14 +194,13 @@ mod tests {
assert!(category.is_some()); assert!(category.is_some());
let category = category.unwrap(); let category = category.unwrap();
let header_field = category.get_header().read(CATEGORY_REGISTER_NAME_FIELD_PATH); let header_field = category.get_header().read_string(CATEGORY_REGISTER_NAME_FIELD_PATH);
assert!(header_field.is_ok(), format!("Expected Ok(_), got: {:?}", header_field)); assert!(header_field.is_ok(), format!("Expected Ok(_), got: {:?}", header_field));
let header_field = header_field.unwrap(); let header_field = header_field.unwrap();
match header_field { match header_field {
Some(&Value::String(ref s)) => assert_eq!(category_name, s), Some(ref s) => assert_eq!(category_name, s),
Some(_) => assert!(false, "Header field has wrong type"), None => assert!(false, "Header field not present"),
None => assert!(false, "Header field not present"),
} }
} }
} }
@ -226,12 +225,10 @@ fn represents_category(store: &Store, sid: StoreId, name: &str) -> Result<bool>
.and_then(|fle| { .and_then(|fle| {
if let Some(fle) = fle { if let Some(fle) = fle {
fle.get_header() fle.get_header()
.read(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH)) .read_string(&String::from(CATEGORY_REGISTER_NAME_FIELD_PATH))
.chain_err(|| CEK::HeaderReadError)? .chain_err(|| CEK::HeaderReadError)?
.ok_or(CE::from_kind(CEK::TypeError))?
.as_str()
.map(|s| s == name)
.ok_or(CE::from_kind(CEK::TypeError)) .ok_or(CE::from_kind(CEK::TypeError))
.map(|s| s == name)
} else { } else {
Ok(false) Ok(false)
} }
@ -278,12 +275,10 @@ impl<'a> Iterator for CategoryNameIter<'a> {
.get(sid)? .get(sid)?
.ok_or_else(|| CE::from_kind(CEK::StoreReadError))? .ok_or_else(|| CE::from_kind(CEK::StoreReadError))?
.get_header() .get_header()
.read(&query) .read_string(&query)
.chain_err(|| CEK::HeaderReadError)? .chain_err(|| CEK::HeaderReadError)?
.and_then(Value::as_str)
.map(String::from)
.map(Category::from) .map(Category::from)
.ok_or_else(|| CE::from_kind(CEK::TypeError)) .ok_or_else(|| CE::from_kind(CEK::StoreReadError))
}) })
} }
} }