diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-02-13 22:07:17 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2018-02-19 12:14:32 +0100 |
commit | 81a8826740c9e3a52356d1ecdfe9cc44c9f7a2da (patch) | |
tree | fdb1821a78d244d9de96814476540fa97046d67c /lib/entry | |
parent | 1014f58cfcc2090ee72b1fc43088e0b78fb5e474 (diff) |
Add impl for RefStore::{get,create,retrieve}_ref()
Diffstat (limited to 'lib/entry')
-rw-r--r-- | lib/entry/libimagentryref/src/refstore.rs | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/lib/entry/libimagentryref/src/refstore.rs b/lib/entry/libimagentryref/src/refstore.rs index 5f1c780d..69a96eb8 100644 --- a/lib/entry/libimagentryref/src/refstore.rs +++ b/lib/entry/libimagentryref/src/refstore.rs @@ -93,30 +93,48 @@ pub trait RefStore<'a> { impl<'a> RefStore<'a> for Store { - fn get_ref<RPG: UniqueRefPathGenerator>(&self) -> Result<Option<FileLockEntry<'a>>, RPG::Error> { - unimplemented!() - } - - fn create_ref<RPG: UniqueRefPathGenerator>(&self) -> Result<FileLockEntry<'a>, RPG::Error> { - unimplemented!() - } - - fn retrieve_ref<RPG: UniqueRefPathGenerator>(&self) Result<FileLockEntry<'a>, RPG::Error> { - unimplemented!() - } + fn get_ref<RPG: UniqueRefPathGenerator>(&'a self, hash: &String) + -> Result<Option<FileLockEntry<'a>>, RPG::Error> + { + let sid = StoreId::new_baseless(PathBuf::from(format!("{}/{}", RPG::collection(), hash))) + .map_err(RE::from)?; - fn delete_ref<RPG: UniqueRefPathGenerator>(&self) -> Result<(), RPG::Error> { - unimplemented!() + debug!("Getting: {:?}", sid); + self.get(sid) + .map_err(RE::from) + .map_err(RPG::Error::from) } - fn ref_exists<RPG: UniqueRefPathGenerator>(&self) -> Result<bool, RPG::Error> { - unimplemented!() + fn create_ref<RPG: UniqueRefPathGenerator, A: AsRef<Path>>(&'a self, path: A) + -> Result<FileLockEntry<'a>, RPG::Error> + { + let path_str = path.as_ref().to_str().map(String::from).ok_or(REK::PathUTF8Error.into())?; + let hash = RPG::unique_hash(path)?; + let pathbuf = PathBuf::from(format!("{}/{}", RPG::collection(), hash)); + let sid = StoreId::new_baseless(pathbuf).map_err(RE::from)?; + + debug!("Creating: {:?}", sid); + self.create(sid) + .map_err(RE::from) + .and_then(|mut fle| { + let _ = fle.set_isflag::<IsRef>()?; + { + let hdr = fle.get_header_mut(); + hdr.insert("ref.path", Value::String(String::from(path_str)))?; + hdr.insert("ref.hash", Value::String(hash))?; + } + Ok(fle) + }) + .map_err(RPG::Error::from) } - fn move_ref_by_id<OLDRPG: UniqueRefPathGenerator, NEWRPG: UniqueRefPathGenerator>(&self) - -> Result<(), Either<OLDRPG::Error, NEWRPG::Error>> + fn retrieve_ref<RPG: UniqueRefPathGenerator, A: AsRef<Path>>(&'a self, path: A) + -> Result<FileLockEntry<'a>, RPG::Error> { - unimplemented!() + match self.get_ref::<RPG>(&RPG::unique_hash(path.as_ref())?)? { + Some(r) => Ok(r), + None => self.create_ref::<RPG, A>(path), + } } } |