diff options
Diffstat (limited to 'lib/entry/libimagentrylink/src/internal.rs')
-rw-r--r-- | lib/entry/libimagentrylink/src/internal.rs | 65 |
1 files changed, 25 insertions, 40 deletions
diff --git a/lib/entry/libimagentrylink/src/internal.rs b/lib/entry/libimagentrylink/src/internal.rs index 8323d656..d9c13ddb 100644 --- a/lib/entry/libimagentrylink/src/internal.rs +++ b/lib/entry/libimagentrylink/src/internal.rs @@ -18,8 +18,6 @@ // use std::collections::BTreeMap; -#[cfg(test)] -use std::path::PathBuf; use libimagstore::storeid::StoreId; use libimagstore::storeid::IntoStoreId; @@ -47,10 +45,10 @@ pub enum Link { impl Link { - pub fn exists(&self) -> Result<bool> { + pub fn exists(&self, store: &Store) -> Result<bool> { match *self { - Link::Id { ref link } => link.exists(), - Link::Annotated { ref link, .. } => link.exists(), + Link::Id { ref link } => store.exists(link.clone()), + Link::Annotated { ref link, .. } => store.exists(link.clone()), } .map_err(From::from) } @@ -82,19 +80,9 @@ impl Link { /// Helper wrapper around Link for StoreId fn without_base(self) -> Link { match self { - Link::Id { link: s } => Link::Id { link: s.without_base() }, - Link::Annotated { link: s, annotation: ann } => - Link::Annotated { link: s.without_base(), annotation: ann }, - } - } - - /// Helper wrapper around Link for StoreId - #[cfg(test)] - fn with_base(self, pb: PathBuf) -> Link { - match self { - Link::Id { link: s } => Link::Id { link: s.with_base(pb) }, + Link::Id { link: s } => Link::Id { link: s }, Link::Annotated { link: s, annotation: ann } => - Link::Annotated { link: s.with_base(pb), annotation: ann }, + Link::Annotated { link: s, annotation: ann }, } } @@ -447,24 +435,24 @@ impl InternalLinker for Entry { fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()> { debug!("Removing internal link: {:?}", link); - let own_loc = self.get_location().clone().without_base(); - let other_loc = link.get_location().clone().without_base(); + + // Cloning because of borrowing + let own_loc = self.get_location().clone(); + let other_loc = link.get_location().clone(); debug!("Removing internal link from {:?} to {:?}", own_loc, other_loc); - link.get_internal_links() + let links = link.get_internal_links()?; + debug!("Rewriting own links for {:?}, without {:?}", other_loc, own_loc); + + let links = links.filter(|l| !l.eq_store_id(&own_loc)); + let _ = rewrite_links(link.get_header_mut(), links)?; + + self.get_internal_links() .and_then(|links| { - debug!("Rewriting own links for {:?}, without {:?}", other_loc, own_loc); - let links = links.filter(|l| !l.eq_store_id(&own_loc)); - rewrite_links(link.get_header_mut(), links) - }) - .and_then(|_| { - self.get_internal_links() - .and_then(|links| { - debug!("Rewriting own links for {:?}, without {:?}", own_loc, other_loc); - let links = links.filter(|l| !l.eq_store_id(&other_loc)); - rewrite_links(self.get_header_mut(), links) - }) + debug!("Rewriting own links for {:?}, without {:?}", own_loc, other_loc); + let links = links.filter(|l| !l.eq_store_id(&other_loc)); + rewrite_links(self.get_header_mut(), links) }) } @@ -580,7 +568,7 @@ fn process_rw_result(links: Result<Option<Value>>) -> Result<LinkIter> { .map(|link| { debug!("Matching the link: {:?}", link); match link { - Value::String(s) => StoreId::new_baseless(PathBuf::from(s)) + Value::String(s) => StoreId::new(PathBuf::from(s)) .map(|s| Link::Id { link: s }) .map_err(From::from) , @@ -600,7 +588,7 @@ fn process_rw_result(links: Result<Option<Value>>) -> Result<LinkIter> { debug!("Ok, here we go with building a Link::Annotated"); match (link, anno) { (Value::String(link), Value::String(anno)) => { - StoreId::new_baseless(PathBuf::from(link)) + StoreId::new(PathBuf::from(link)) .map_err(From::from) .map(|link| { Link::Annotated { @@ -694,7 +682,7 @@ pub mod store_check { if is_match!(self.get(id.clone()), Ok(Some(_))) { debug!("Exists in store: {:?}", id); - if !id.exists()? { + if !self.exists(id.clone())? { warn!("Does exist in store but not on FS: {:?}", id); return Err(err_msg("Link target does not exist")) } @@ -783,7 +771,6 @@ pub mod store_check { #[cfg(test)] mod test { use std::path::PathBuf; - use std::sync::Arc; use libimagstore::store::Store; @@ -795,9 +782,7 @@ mod test { } pub fn get_store() -> Store { - use libimagstore::file_abstraction::InMemoryFileAbstraction; - let backend = Arc::new(InMemoryFileAbstraction::default()); - Store::new_with_backend(PathBuf::from("/"), &None, backend).unwrap() + Store::new_inmemory(PathBuf::from("/"), &None).unwrap() } #[test] @@ -833,8 +818,8 @@ mod test { assert_eq!(e1_links.len(), 1); assert_eq!(e2_links.len(), 1); - assert!(e1_links.first().map(|l| l.clone().with_base(store.path().clone()).eq_store_id(e2.get_location())).unwrap_or(false)); - assert!(e2_links.first().map(|l| l.clone().with_base(store.path().clone()).eq_store_id(e1.get_location())).unwrap_or(false)); + assert!(e1_links.first().map(|l| l.clone().eq_store_id(e2.get_location())).unwrap_or(false)); + assert!(e2_links.first().map(|l| l.clone().eq_store_id(e1.get_location())).unwrap_or(false)); } { |