diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-06-14 11:05:38 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-07-04 19:29:02 +0200 |
commit | b219f06a6595638efaef745b715c23ae35eb8e6e (patch) | |
tree | a03fca0042eede9a2bbbfcda3e1b133336f12b06 /libimagref | |
parent | 5cfedf35700009cacc2346366aced4cd22a3da28 (diff) |
Impl Ref::open()
Diffstat (limited to 'libimagref')
-rw-r--r-- | libimagref/src/reference.rs | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index 67c210bd..6b994441 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -8,17 +8,34 @@ use std::ops::DerefMut; use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreId; use libimagstore::store::Store; +use libimagerror::into::IntoError; -use result::Result; +use toml::Value; + +use error::RefErrorKind as REK; use flags::RefFlags; +use result::Result; pub struct Ref<'a>(FileLockEntry<'a>); impl<'a> Ref<'a> { - /// Try to open `si` as Ref object from the store - pub fn open(store: &Store, si: StoreId) -> Result<Ref<'a>> { - unimplemented!() + /// Try to get `si` as Ref object from the store + pub fn get(store: &'a Store, si: StoreId) -> Result<Ref<'a>> { + match store.get(si) { + Err(e) => return Err(REK::StoreReadError.into_error_with_cause(Box::new(e))), + Ok(None) => return Err(REK::RefNotInStore.into_error()), + Ok(Some(fle)) => Ref::read_reference(&fle).map(|_| Ref(fle)), + } + } + + fn read_reference(fle: &FileLockEntry<'a>) -> Result<PathBuf> { + match fle.get_header().read("ref.reference") { + Ok(Some(Value::String(s))) => Ok(PathBuf::from(s)), + Ok(Some(_)) => Err(REK::HeaderTypeError.into_error()), + Ok(None) => Err(REK::HeaderFieldMissingError.into_error()), + Err(e) => Err(REK::StoreReadError.into_error_with_cause(Box::new(e))), + } } /// Create a Ref object which refers to `pb` |