diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-10-06 13:15:29 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2018-11-06 20:33:30 +0100 |
commit | d1f087eb57290711c66230ba7d0ff2d6ba4c9396 (patch) | |
tree | cbf92f980a14f8f720d776f6f4ed6a7885f78e2a /lib/entry/libimagentrylink/src/external.rs | |
parent | 1e6d58bf4c7988b0214406c4ffa4dfdc1eba4a8e (diff) |
Change libimag{bookmark, entrylink} external linking
...to report newly created entries, so that we can re-use these new
entries in the library-using code (for example for ID reporting).
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'lib/entry/libimagentrylink/src/external.rs')
-rw-r--r-- | lib/entry/libimagentrylink/src/external.rs | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/lib/entry/libimagentrylink/src/external.rs b/lib/entry/libimagentrylink/src/external.rs index 6b951d46..d4c6956b 100644 --- a/lib/entry/libimagentrylink/src/external.rs +++ b/lib/entry/libimagentrylink/src/external.rs @@ -108,13 +108,13 @@ pub trait ExternalLinker : InternalLinker { fn get_external_links<'a>(&self, store: &'a Store) -> Result<UrlIter<'a>>; /// Set the external links for the implementor object - fn set_external_links(&mut self, store: &Store, links: Vec<Url>) -> Result<()>; + fn set_external_links(&mut self, store: &Store, links: Vec<Url>) -> Result<Vec<StoreId>>; /// Add an external link to the implementor object - fn add_external_link(&mut self, store: &Store, link: Url) -> Result<()>; + fn add_external_link(&mut self, store: &Store, link: Url) -> Result<Vec<StoreId>>; /// Remove an external link from the implementor object - fn remove_external_link(&mut self, store: &Store, link: Url) -> Result<()>; + fn remove_external_link(&mut self, store: &Store, link: Url) -> Result<Vec<StoreId>>; } @@ -322,13 +322,20 @@ impl ExternalLinker for Entry { } /// Set the external links for the implementor object - fn set_external_links(&mut self, store: &Store, links: Vec<Url>) -> Result<()> { + /// + /// # Return Value + /// + /// Returns the StoreIds which were newly created for the new external links, if there are more + /// external links than before. + /// If there are less external links than before, an empty vec![] is returned. + /// + fn set_external_links(&mut self, store: &Store, links: Vec<Url>) -> Result<Vec<StoreId>> { // Take all the links, generate a SHA sum out of each one, filter out the already existing // store entries and store the other URIs in the header of one FileLockEntry each, in // the path /link/external/<SHA of the URL> debug!("Iterating {} links = {:?}", links.len(), links); - for link in links { // for all links + links.into_iter().map(|link| { let hash = hex::encode(Sha1::digest(&link.as_str().as_bytes())); let file_id = ModuleEntryPath::new(format!("external/{}", hash)).into_storeid() @@ -341,6 +348,8 @@ impl ExternalLinker for Entry { debug!("Hash = '{:?}'", hash); debug!("StoreId = '{:?}'", file_id); + let link_already_exists = store.get(file_id.clone())?.is_some(); + // retrieve the file from the store, which implicitely creates the entry if it does not // exist let mut file = store @@ -375,13 +384,27 @@ impl ExternalLinker for Entry { // then add an internal link to the new file or return an error if this fails let _ = self.add_internal_link(file.deref_mut())?; debug!("Error adding internal link"); - } - debug!("Ready iterating"); - Ok(()) + + Ok((link_already_exists, file_id)) + }) + .filter_map(|res| match res { + Ok((exists, entry)) => if exists { Some(Ok(entry)) } else { None }, + Err(e) => Some(Err(e)) + }) + .collect() } /// Add an external link to the implementor object - fn add_external_link(&mut self, store: &Store, link: Url) -> Result<()> { + /// + /// # Return Value + /// + /// (See ExternalLinker::set_external_links()) + /// + /// Returns the StoreIds which were newly created for the new external links, if there are more + /// external links than before. + /// If there are less external links than before, an empty vec![] is returned. + /// + fn add_external_link(&mut self, store: &Store, link: Url) -> Result<Vec<StoreId>> { // get external links, add this one, save them debug!("Getting links"); self.get_external_links(store) @@ -396,7 +419,16 @@ impl ExternalLinker for Entry { } /// Remove an external link from the implementor object - fn remove_external_link(&mut self, store: &Store, link: Url) -> Result<()> { + /// + /// # Return Value + /// + /// (See ExternalLinker::set_external_links()) + /// + /// Returns the StoreIds which were newly created for the new external links, if there are more + /// external links than before. + /// If there are less external links than before, an empty vec![] is returned. + /// + fn remove_external_link(&mut self, store: &Store, link: Url) -> Result<Vec<StoreId>> { // get external links, remove this one, save them self.get_external_links(store) .and_then(|links| { |