2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2016-02-15 12:27:40 +00:00
|
|
|
use libimagstore::store::Entry;
|
2018-02-25 16:04:05 +00:00
|
|
|
use libimagstore::store::Store;
|
2018-10-30 17:40:51 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2017-08-26 15:53:08 +00:00
|
|
|
use toml_query::read::TomlValueReadExt;
|
2017-09-07 20:13:00 +00:00
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::Error;
|
|
|
|
use failure::err_msg;
|
2017-08-26 15:53:08 +00:00
|
|
|
|
2019-05-30 15:54:48 +00:00
|
|
|
use crate::iter::LinkIter;
|
|
|
|
use crate::iter::IntoValues;
|
|
|
|
use crate::link::Link;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2016-02-07 15:25:17 +00:00
|
|
|
use toml::Value;
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
pub trait Linkable {
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2016-02-15 12:26:00 +00:00
|
|
|
/// Get the internal links from the implementor object
|
2019-06-21 19:33:22 +00:00
|
|
|
fn links(&self) -> Result<LinkIter>;
|
2016-02-15 12:26:00 +00:00
|
|
|
|
|
|
|
/// Add an internal link to the implementor object
|
2019-06-21 19:33:22 +00:00
|
|
|
fn add_link(&mut self, link: &mut Entry) -> Result<()>;
|
2016-02-15 12:26:00 +00:00
|
|
|
|
|
|
|
/// Remove an internal link from the implementor object
|
2019-06-21 19:33:22 +00:00
|
|
|
fn remove_link(&mut self, link: &mut Entry) -> Result<()>;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2018-02-25 16:04:05 +00:00
|
|
|
/// Remove _all_ internal links
|
|
|
|
fn unlink(&mut self, store: &Store) -> Result<()>;
|
|
|
|
|
2017-02-22 16:40:28 +00:00
|
|
|
/// Add internal annotated link
|
2019-06-21 19:33:22 +00:00
|
|
|
fn add_annotated_link(&mut self, link: &mut Entry, annotation: String) -> Result<()>;
|
2016-02-03 14:47:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
impl Linkable for Entry {
|
2016-02-15 12:26:00 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
fn links(&self) -> Result<LinkIter> {
|
2018-04-30 13:48:08 +00:00
|
|
|
debug!("Getting internal links");
|
|
|
|
trace!("Getting internal links from header of '{}' = {:?}", self.get_location(), self.get_header());
|
2017-08-26 15:53:08 +00:00
|
|
|
let res = self
|
|
|
|
.get_header()
|
2017-09-07 20:13:00 +00:00
|
|
|
.read("links.internal")
|
2019-05-17 22:14:34 +00:00
|
|
|
.context(format_err!("Failed to read header 'links.internal' of '{}'", self.get_location()))
|
2018-10-30 17:40:51 +00:00
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.context(EM::EntryHeaderError)
|
|
|
|
.map_err(Error::from)
|
2017-08-26 15:53:08 +00:00
|
|
|
.map(|r| r.cloned());
|
|
|
|
process_rw_result(res)
|
2016-02-15 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
fn add_link(&mut self, link: &mut Entry) -> Result<()> {
|
2018-04-30 13:48:08 +00:00
|
|
|
debug!("Adding internal link: {:?}", link);
|
2017-02-22 16:40:28 +00:00
|
|
|
let location = link.get_location().clone().into();
|
2019-06-21 19:33:22 +00:00
|
|
|
add_link_with_instance(self, link, location)
|
2016-02-15 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
fn remove_link(&mut self, link: &mut Entry) -> Result<()> {
|
2018-04-30 13:48:08 +00:00
|
|
|
debug!("Removing internal link: {:?}", link);
|
2018-12-13 00:28:01 +00:00
|
|
|
|
|
|
|
// Cloning because of borrowing
|
|
|
|
let own_loc = self.get_location().clone();
|
|
|
|
let other_loc = link.get_location().clone();
|
2016-02-21 13:48:21 +00:00
|
|
|
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Removing internal link from {:?} to {:?}", own_loc, other_loc);
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
let links = link.links()?;
|
2018-12-13 00:28:01 +00:00
|
|
|
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)?;
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
self.links()
|
2016-04-08 21:57:16 +00:00
|
|
|
.and_then(|links| {
|
2018-12-13 00:28:01 +00:00
|
|
|
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)
|
2016-02-15 12:26:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-25 16:04:05 +00:00
|
|
|
fn unlink(&mut self, store: &Store) -> Result<()> {
|
2019-06-21 19:33:22 +00:00
|
|
|
for id in self.links()?.map(|l| l.get_store_id().clone()) {
|
2019-05-17 22:14:34 +00:00
|
|
|
match store.get(id).context("Failed to get entry")? {
|
2019-06-21 19:33:22 +00:00
|
|
|
Some(mut entry) => self.remove_link(&mut entry)?,
|
2018-10-30 17:40:51 +00:00
|
|
|
None => return Err(err_msg("Link target does not exist")),
|
2018-02-25 16:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
fn add_annotated_link(&mut self, link: &mut Entry, annotation: String) -> Result<()> {
|
2017-02-22 16:40:28 +00:00
|
|
|
let new_link = Link::Annotated {
|
|
|
|
link: link.get_location().clone(),
|
|
|
|
annotation: annotation,
|
|
|
|
};
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
add_link_with_instance(self, link, new_link)
|
2017-02-22 16:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
fn add_link_with_instance(this: &mut Entry, link: &mut Entry, instance: Link) -> Result<()> {
|
2017-02-22 16:40:28 +00:00
|
|
|
debug!("Adding internal link from {:?} to {:?}", this.get_location(), instance);
|
|
|
|
|
|
|
|
add_foreign_link(link, this.get_location().clone())
|
|
|
|
.and_then(|_| {
|
2019-06-21 19:33:22 +00:00
|
|
|
this.links()
|
2017-02-22 16:40:28 +00:00
|
|
|
.and_then(|links| {
|
|
|
|
let links = links.chain(LinkIter::new(vec![instance]));
|
|
|
|
rewrite_links(this.get_header_mut(), links)
|
|
|
|
})
|
|
|
|
})
|
2016-02-15 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 10:32:59 +00:00
|
|
|
fn rewrite_links<I: Iterator<Item = Link>>(header: &mut Value, links: I) -> Result<()> {
|
2017-10-30 19:17:21 +00:00
|
|
|
let links = links.into_values()
|
2017-02-22 09:53:27 +00:00
|
|
|
.into_iter()
|
2018-10-30 17:40:51 +00:00
|
|
|
.fold(Ok(vec![]), |acc: Result<Vec<_>>, elem| {
|
2016-08-03 09:29:38 +00:00
|
|
|
acc.and_then(move |mut v| {
|
2018-10-30 17:40:51 +00:00
|
|
|
v.push(elem.context(EM::ConversionError)?);
|
|
|
|
Ok(v)
|
2016-08-03 09:29:38 +00:00
|
|
|
})
|
2017-10-30 19:17:21 +00:00
|
|
|
})?;
|
2016-08-03 09:29:38 +00:00
|
|
|
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Setting new link array: {:?}", links);
|
2017-08-26 15:53:08 +00:00
|
|
|
let process = header
|
2017-09-07 20:13:00 +00:00
|
|
|
.insert("links.internal", Value::Array(links))
|
2019-05-17 22:14:34 +00:00
|
|
|
.context(format_err!("Failed to insert header 'links.internal'"))
|
2018-10-30 17:40:51 +00:00
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.map_err(Error::from);
|
2016-08-03 09:29:38 +00:00
|
|
|
process_rw_result(process).map(|_| ())
|
2016-04-08 22:07:30 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
/// When Linking A -> B, the specification wants us to link back B -> A.
|
|
|
|
/// This is a helper function which does this.
|
|
|
|
fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Linking back from {:?} to {:?}", target.get_location(), from);
|
2019-06-21 19:33:22 +00:00
|
|
|
target.links()
|
2016-10-13 16:28:15 +00:00
|
|
|
.and_then(|links| {
|
2017-10-30 19:17:21 +00:00
|
|
|
let links = links
|
2017-02-22 09:53:27 +00:00
|
|
|
.chain(LinkIter::new(vec![from.into()]))
|
2016-10-13 16:28:15 +00:00
|
|
|
.into_values()
|
2017-02-22 09:53:27 +00:00
|
|
|
.into_iter()
|
2018-10-30 17:40:51 +00:00
|
|
|
.fold(Ok(vec![]), |acc: Result<Vec<_>>, elem| {
|
2016-08-03 09:30:23 +00:00
|
|
|
acc.and_then(move |mut v| {
|
2018-10-30 17:40:51 +00:00
|
|
|
v.push(elem.context(EM::ConversionError)?);
|
|
|
|
Ok(v)
|
2016-08-03 09:30:23 +00:00
|
|
|
})
|
2017-10-30 19:17:21 +00:00
|
|
|
})?;
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("Setting links in {:?}: {:?}", target.get_location(), links);
|
2017-08-26 15:53:08 +00:00
|
|
|
|
|
|
|
let res = target
|
|
|
|
.get_header_mut()
|
2017-09-07 20:13:00 +00:00
|
|
|
.insert("links.internal", Value::Array(links))
|
2019-05-17 22:14:34 +00:00
|
|
|
.context(format_err!("Failed to insert header 'links.internal'"))
|
2018-10-30 17:40:51 +00:00
|
|
|
.context(EM::EntryHeaderReadError)
|
|
|
|
.map_err(Error::from);
|
2017-08-26 15:53:08 +00:00
|
|
|
|
|
|
|
process_rw_result(res).map(|_| ())
|
2016-02-21 13:48:21 +00:00
|
|
|
})
|
2016-02-15 12:27:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 15:53:08 +00:00
|
|
|
fn process_rw_result(links: Result<Option<Value>>) -> Result<LinkIter> {
|
2016-08-25 17:00:42 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2016-06-11 04:50:58 +00:00
|
|
|
let links = match links {
|
|
|
|
Err(e) => {
|
|
|
|
debug!("RW action on store failed. Generating LinkError");
|
2018-10-30 17:40:51 +00:00
|
|
|
return Err(e).context(EM::EntryHeaderReadError).map_err(Error::from)
|
2016-06-11 04:50:58 +00:00
|
|
|
},
|
|
|
|
Ok(None) => {
|
|
|
|
debug!("We got no value from the header!");
|
2016-10-13 16:28:15 +00:00
|
|
|
return Ok(LinkIter::new(vec![]))
|
2016-06-11 04:50:58 +00:00
|
|
|
},
|
|
|
|
Ok(Some(Value::Array(l))) => l,
|
|
|
|
Ok(Some(_)) => {
|
|
|
|
debug!("We expected an Array for the links, but there was a non-Array!");
|
2018-10-30 17:40:51 +00:00
|
|
|
return Err(err_msg("Link type error"));
|
2016-04-12 19:55:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-22 16:20:23 +00:00
|
|
|
if !links.iter().all(|l| is_match!(*l, Value::String(_)) || is_match!(*l, Value::Table(_))) {
|
|
|
|
debug!("At least one of the Values which were expected in the Array of links is not a String or a Table!");
|
2016-04-12 19:55:30 +00:00
|
|
|
debug!("Generating LinkError");
|
2018-10-30 17:40:51 +00:00
|
|
|
return Err(err_msg("Existing Link type error"));
|
2016-02-14 16:50:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
let links : Vec<Link> = links.into_iter()
|
2016-02-14 16:50:06 +00:00
|
|
|
.map(|link| {
|
2017-02-22 16:20:23 +00:00
|
|
|
debug!("Matching the link: {:?}", link);
|
2016-02-14 16:50:06 +00:00
|
|
|
match link {
|
2018-12-05 18:01:42 +00:00
|
|
|
Value::String(s) => StoreId::new(PathBuf::from(s))
|
2017-02-22 09:53:27 +00:00
|
|
|
.map(|s| Link::Id { link: s })
|
2017-09-23 13:03:29 +00:00
|
|
|
.map_err(From::from)
|
2017-02-22 09:53:27 +00:00
|
|
|
,
|
2017-02-22 16:20:23 +00:00
|
|
|
Value::Table(mut tab) => {
|
|
|
|
debug!("Destructuring table");
|
|
|
|
if !tab.contains_key("link")
|
|
|
|
|| !tab.contains_key("annotation") {
|
|
|
|
debug!("Things missing... returning Error instance");
|
2018-10-30 17:40:51 +00:00
|
|
|
Err(err_msg("Link parser error"))
|
2017-02-22 16:20:23 +00:00
|
|
|
} else {
|
2017-10-30 19:17:21 +00:00
|
|
|
let link = tab.remove("link")
|
2018-10-30 17:40:51 +00:00
|
|
|
.ok_or(err_msg("Link parser: field missing"))?;
|
2017-02-22 16:20:23 +00:00
|
|
|
|
2017-10-30 19:17:21 +00:00
|
|
|
let anno = tab.remove("annotation")
|
2018-10-30 17:40:51 +00:00
|
|
|
.ok_or(err_msg("Link parser: Field missing"))?;
|
2017-02-22 16:20:23 +00:00
|
|
|
|
|
|
|
debug!("Ok, here we go with building a Link::Annotated");
|
|
|
|
match (link, anno) {
|
|
|
|
(Value::String(link), Value::String(anno)) => {
|
2018-12-05 18:01:42 +00:00
|
|
|
StoreId::new(PathBuf::from(link))
|
2017-09-23 13:03:29 +00:00
|
|
|
.map_err(From::from)
|
2017-02-22 16:20:23 +00:00
|
|
|
.map(|link| {
|
|
|
|
Link::Annotated {
|
|
|
|
link: link,
|
|
|
|
annotation: anno,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2018-10-30 17:40:51 +00:00
|
|
|
_ => Err(err_msg("Link parser: Field type error")),
|
2017-02-22 16:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 16:50:06 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
})
|
2017-10-30 19:17:21 +00:00
|
|
|
.collect::<Result<Vec<Link>>>()?;
|
2016-02-14 16:50:06 +00:00
|
|
|
|
2016-04-12 19:55:30 +00:00
|
|
|
debug!("Ok, the RW action was successful, returning link vector now!");
|
2016-10-13 16:28:15 +00:00
|
|
|
Ok(LinkIter::new(links))
|
2016-02-14 16:50:06 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 08:33:30 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
use super::Linkable;
|
2017-09-23 12:13:16 +00:00
|
|
|
use super::Link;
|
2016-10-15 08:33:30 +00:00
|
|
|
|
2016-10-15 08:58:50 +00:00
|
|
|
fn setup_logging() {
|
2017-12-07 21:07:01 +00:00
|
|
|
let _ = ::env_logger::try_init();
|
2016-10-15 08:58:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 08:33:30 +00:00
|
|
|
pub fn get_store() -> Store {
|
2018-12-02 22:52:14 +00:00
|
|
|
Store::new_inmemory(PathBuf::from("/"), &None).unwrap()
|
2016-10-15 08:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_entry_no_links() {
|
2016-10-15 08:58:50 +00:00
|
|
|
setup_logging();
|
2016-10-15 08:33:30 +00:00
|
|
|
let store = get_store();
|
|
|
|
let entry = store.create(PathBuf::from("test_new_entry_no_links")).unwrap();
|
2019-06-21 19:33:22 +00:00
|
|
|
let links = entry.links();
|
2016-10-15 08:33:30 +00:00
|
|
|
assert!(links.is_ok());
|
|
|
|
let links = links.unwrap();
|
|
|
|
assert_eq!(links.collect::<Vec<_>>().len(), 0);
|
|
|
|
}
|
|
|
|
|
2016-10-15 08:41:13 +00:00
|
|
|
#[test]
|
|
|
|
fn test_link_two_entries() {
|
2016-10-15 08:58:50 +00:00
|
|
|
setup_logging();
|
2016-10-15 08:41:13 +00:00
|
|
|
let store = get_store();
|
|
|
|
let mut e1 = store.create(PathBuf::from("test_link_two_entries1")).unwrap();
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.links().is_ok());
|
2016-10-15 08:41:13 +00:00
|
|
|
|
|
|
|
let mut e2 = store.create(PathBuf::from("test_link_two_entries2")).unwrap();
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e2.links().is_ok());
|
2016-10-15 08:41:13 +00:00
|
|
|
|
|
|
|
{
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.add_link(&mut e2).is_ok());
|
2016-10-15 08:41:13 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
let e1_links = e1.links().unwrap().collect::<Vec<_>>();
|
|
|
|
let e2_links = e2.links().unwrap().collect::<Vec<_>>();
|
2016-10-15 08:41:13 +00:00
|
|
|
|
2016-10-15 09:24:14 +00:00
|
|
|
debug!("1 has links: {:?}", e1_links);
|
|
|
|
debug!("2 has links: {:?}", e2_links);
|
|
|
|
|
2016-10-15 08:41:13 +00:00
|
|
|
assert_eq!(e1_links.len(), 1);
|
|
|
|
assert_eq!(e2_links.len(), 1);
|
|
|
|
|
2018-12-02 22:52:14 +00:00
|
|
|
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));
|
2016-10-15 08:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.remove_link(&mut e2).is_ok());
|
2016-10-15 08:41:13 +00:00
|
|
|
|
2018-11-01 10:51:07 +00:00
|
|
|
debug!("{:?}", e2.to_str());
|
2019-06-21 19:33:22 +00:00
|
|
|
let e2_links = e2.links().unwrap().collect::<Vec<_>>();
|
2016-10-15 08:41:13 +00:00
|
|
|
assert_eq!(e2_links.len(), 0, "Expected [], got: {:?}", e2_links);
|
|
|
|
|
2018-11-01 10:51:07 +00:00
|
|
|
debug!("{:?}", e1.to_str());
|
2019-06-21 19:33:22 +00:00
|
|
|
let e1_links = e1.links().unwrap().collect::<Vec<_>>();
|
2016-10-15 08:41:13 +00:00
|
|
|
assert_eq!(e1_links.len(), 0, "Expected [], got: {:?}", e1_links);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 09:31:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_multiple_links() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
let mut e1 = store.retrieve(PathBuf::from("1")).unwrap();
|
|
|
|
let mut e2 = store.retrieve(PathBuf::from("2")).unwrap();
|
|
|
|
let mut e3 = store.retrieve(PathBuf::from("3")).unwrap();
|
|
|
|
let mut e4 = store.retrieve(PathBuf::from("4")).unwrap();
|
|
|
|
let mut e5 = store.retrieve(PathBuf::from("5")).unwrap();
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.add_link(&mut e2).is_ok());
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e4.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.add_link(&mut e3).is_ok());
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.add_link(&mut e4).is_ok());
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 3);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e5.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.add_link(&mut e5).is_ok());
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 4);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e5.links().unwrap().collect::<Vec<_>>().len(), 1);
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e5.remove_link(&mut e1).is_ok());
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 3);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e5.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e4.remove_link(&mut e1).is_ok());
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e4.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e3.remove_link(&mut e1).is_ok());
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e4.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e2.remove_link(&mut e1).is_ok());
|
2016-10-15 09:31:30 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e4.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e5.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2016-10-15 09:31:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-14 21:17:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_link_deleting() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
let mut e1 = store.retrieve(PathBuf::from("1")).unwrap();
|
|
|
|
let mut e2 = store.retrieve(PathBuf::from("2")).unwrap();
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2017-07-14 21:17:32 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.add_link(&mut e2).is_ok());
|
2017-07-14 21:17:32 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
2017-07-14 21:17:32 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.remove_link(&mut e2).is_ok());
|
2017-07-14 21:17:32 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2017-07-14 21:17:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 21:21:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_link_deleting_multiple_links() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
|
|
|
|
let mut e1 = store.retrieve(PathBuf::from("1")).unwrap();
|
|
|
|
let mut e2 = store.retrieve(PathBuf::from("2")).unwrap();
|
|
|
|
let mut e3 = store.retrieve(PathBuf::from("3")).unwrap();
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.add_link(&mut e2).is_ok()); // 1-2
|
|
|
|
assert!(e1.add_link(&mut e3).is_ok()); // 1-2, 1-3
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 1);
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e2.add_link(&mut e3).is_ok()); // 1-2, 1-3, 2-3
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 2);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 2);
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.remove_link(&mut e2).is_ok()); // 1-3, 2-3
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 2);
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e1.remove_link(&mut e3).is_ok()); // 2-3
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 1);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 1);
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert!(e2.remove_link(&mut e3).is_ok());
|
2017-07-14 21:21:38 +00:00
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
assert_eq!(e1.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e2.links().unwrap().collect::<Vec<_>>().len(), 0);
|
|
|
|
assert_eq!(e3.links().unwrap().collect::<Vec<_>>().len(), 0);
|
2017-07-14 21:21:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 12:13:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_link_annotating() {
|
|
|
|
setup_logging();
|
|
|
|
let store = get_store();
|
|
|
|
let mut entry1 = store.create(PathBuf::from("test_link_annotating-1")).unwrap();
|
|
|
|
let mut entry2 = store.create(PathBuf::from("test_link_annotating-2")).unwrap();
|
|
|
|
|
2019-06-21 19:33:22 +00:00
|
|
|
let res = entry1.add_annotated_link(&mut entry2, String::from("annotation"));
|
2017-09-23 12:13:16 +00:00
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
{
|
2019-06-21 19:33:22 +00:00
|
|
|
for link in entry1.links().unwrap() {
|
2017-09-23 12:13:16 +00:00
|
|
|
match link {
|
|
|
|
Link::Annotated {annotation, ..} => assert_eq!(annotation, "annotation"),
|
|
|
|
_ => assert!(false, "Non-annotated link found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-06-21 19:33:22 +00:00
|
|
|
for link in entry2.links().unwrap() {
|
2017-09-23 12:13:16 +00:00
|
|
|
match link {
|
|
|
|
Link::Id {..} => {},
|
|
|
|
Link::Annotated {..} => assert!(false, "Annotated link found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 08:33:30 +00:00
|
|
|
}
|