From ff5e6653ed05d0efe8b30261bbf02cc0f762915d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 22 Apr 2019 12:43:19 +0200 Subject: [PATCH] Add option to override ref data in contact entries This patch adds a flag to the ContactStore::retrieve* functions so one can override the ref data in the store for the retrieved entry. Signed-off-by: Matthias Beyer --- lib/domain/libimagcontact/src/store.rs | 40 ++++++++++++++++++++------ 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/domain/libimagcontact/src/store.rs b/lib/domain/libimagcontact/src/store.rs index 3fd3771d..eac759ab 100644 --- a/lib/domain/libimagcontact/src/store.rs +++ b/lib/domain/libimagcontact/src/store.rs @@ -45,7 +45,11 @@ pub trait ContactStore<'a> { -> Result> where CN: AsRef; - fn retrieve_from_path(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN) + fn retrieve_from_path(&'a self, + p: &PathBuf, + rc: &RefConfig, + collection_name: CN, + force: bool) -> Result> where CN: AsRef; @@ -54,7 +58,12 @@ pub trait ContactStore<'a> { where CN: AsRef, P: AsRef; - fn retrieve_from_buf(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN) + fn retrieve_from_buf(&'a self, + buf: &str, + path: P, + rc: &RefConfig, + collection_name: CN, + force: bool) -> Result> where CN: AsRef, P: AsRef; @@ -82,11 +91,12 @@ impl<'a> ContactStore<'a> for Store { /// /// Uses the collection with `collection_name` from RefConfig to store the reference to the /// file. - fn retrieve_from_path(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN) + fn retrieve_from_path(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN, force: bool) -> Result> where CN: AsRef { - util::read_to_string(p).and_then(|buf| self.retrieve_from_buf(&buf, p, rc, collection_name)) + util::read_to_string(p) + .and_then(|buf| self.retrieve_from_buf(&buf, p, rc, collection_name, force)) } /// Create a contact from a buffer @@ -96,13 +106,19 @@ impl<'a> ContactStore<'a> for Store { /// /// Needs the `path` passed where the buffer was read from, because we want to create a /// reference to it. + /// + /// # Note + /// + /// This does _never_ force-override existing reference data, thus the `force` parameter of + /// `postprocess_fetched_entry()` is hardcoded to `false`. + /// fn create_from_buf(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN) -> Result> where CN: AsRef, P: AsRef { let (sid, value) = prepare_fetching_from_store(buf)?; - postprocess_fetched_entry(self.create(sid)?, value, path, rc, collection_name) + postprocess_fetched_entry(self.create(sid)?, value, path, rc, collection_name, false) } /// Retrieve a contact from a buffer @@ -112,13 +128,18 @@ impl<'a> ContactStore<'a> for Store { /// /// Needs the `path` passed where the buffer was read from, because we want to create a /// reference to it. - fn retrieve_from_buf(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN) + fn retrieve_from_buf(&'a self, + buf: &str, + path: P, + rc: &RefConfig, + collection_name: CN, + force: bool) -> Result> where CN: AsRef, P: AsRef { let (sid, value) = prepare_fetching_from_store(buf)?; - postprocess_fetched_entry(self.retrieve(sid)?, value, path, rc, collection_name) + postprocess_fetched_entry(self.retrieve(sid)?, value, path, rc, collection_name, force) } fn all_contacts(&'a self) -> Result> { @@ -159,7 +180,8 @@ fn postprocess_fetched_entry<'a, CN, P>(mut entry: FileLockEntry<'a>, value: Value, path: P, rc: &RefConfig, - collection_name: CN) + collection_name: CN, + force: bool) -> Result> where CN: AsRef, P: AsRef @@ -169,7 +191,7 @@ fn postprocess_fetched_entry<'a, CN, P>(mut entry: FileLockEntry<'a>, entry.set_isflag::()?; entry.get_header_mut().insert("contact.data", value)?; - entry.as_ref_with_hasher_mut::().make_ref(path, collection_name, rc, false)?; + entry.as_ref_with_hasher_mut::().make_ref(path, collection_name, rc, force)?; Ok(entry) }