diff --git a/lib/entry/libimagentryref/src/reference.rs b/lib/entry/libimagentryref/src/reference.rs index e2b081d2..a02d48aa 100644 --- a/lib/entry/libimagentryref/src/reference.rs +++ b/lib/entry/libimagentryref/src/reference.rs @@ -20,6 +20,7 @@ //! The Ref object is a helper over the link functionality, so one is able to create references to //! files outside of the imag store. +use std::path::Path; use std::path::PathBuf; use std::result::Result as RResult; @@ -27,8 +28,10 @@ use libimagentryutil::isa::Is; use libimagentryutil::isa::IsKindHeaderPathProvider; use libimagstore::store::Entry; +use toml::Value; use toml_query::read::TomlValueReadExt; use toml_query::delete::TomlValueDeleteExt; +use toml_query::insert::TomlValueInsertExt; use refstore::UniqueRefPathGenerator; use error::Result; @@ -45,6 +48,9 @@ pub trait Ref { /// Does not need a `UniqueRefPathGenerator` as it reads the hash stored in the header fn get_hash(&self) -> Result<&str>; + /// Make this object a ref + fn make_ref>(&mut self, hash: String, path: P) -> Result<()>; + /// Get the referenced path. /// /// Does not need a `UniqueRefPathGenerator` as it reads the path stored in the header. @@ -89,6 +95,21 @@ impl Ref for Entry { .and_then(|v| v.as_str().ok_or_else(|| REK::HeaderTypeError("ref.hash", "string").into())) } + fn make_ref>(&mut self, hash: String, path: P) -> Result<()> { + let path_str : String = path + .as_ref() + .to_str() + .map(String::from) + .ok_or_else(|| RE::from(REK::PathUTF8Error))?; + + let _ = self.set_isflag::()?; + let hdr = self.get_header_mut(); + hdr.insert("ref.path", Value::String(String::from(path_str)))?; + hdr.insert("ref.hash", Value::String(hash))?; + + Ok(()) + } + fn get_path(&self) -> Result { self.get_header() .read("ref.path") diff --git a/lib/entry/libimagentryref/src/refstore.rs b/lib/entry/libimagentryref/src/refstore.rs index c428efb2..f8ca94d0 100644 --- a/lib/entry/libimagentryref/src/refstore.rs +++ b/lib/entry/libimagentryref/src/refstore.rs @@ -23,14 +23,9 @@ use std::path::PathBuf; use libimagstore::store::FileLockEntry; use libimagstore::store::Store; use libimagstore::storeid::StoreId; -use libimagentryutil::isa::Is; - -use toml_query::insert::TomlValueInsertExt; -use toml::Value; use error::RefError as RE; -use error::RefErrorKind as REK; -use reference::IsRef; +use reference::Ref; /// A UniqueRefPathGenerator generates unique Pathes /// @@ -108,21 +103,15 @@ impl<'a> RefStore<'a> for Store { fn create_ref>(&'a self, path: A) -> Result, 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)?; + let sid = StoreId::new_baseless(pathbuf.clone()).map_err(RE::from)?; debug!("Creating: {:?}", sid); self.create(sid) .map_err(RE::from) .and_then(|mut fle| { - let _ = fle.set_isflag::()?; - { - let hdr = fle.get_header_mut(); - hdr.insert("ref.path", Value::String(String::from(path_str)))?; - hdr.insert("ref.hash", Value::String(hash))?; - } + fle.make_ref(hash, pathbuf)?; Ok(fle) }) .map_err(RPG::Error::from)