2016-04-16 14:09:00 +00:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2016-02-15 12:27:40 +00:00
|
|
|
use libimagstore::store::Entry;
|
2016-04-08 22:07:30 +00:00
|
|
|
use libimagstore::store::EntryHeader;
|
2016-02-14 16:50:06 +00:00
|
|
|
use libimagstore::store::Result as StoreResult;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
|
|
|
use error::{LinkError, LinkErrorKind};
|
|
|
|
use result::Result;
|
|
|
|
|
2016-02-07 15:25:17 +00:00
|
|
|
use toml::Value;
|
2016-04-16 13:54:38 +00:00
|
|
|
use itertools::Itertools;
|
2016-02-07 15:25:17 +00:00
|
|
|
|
2016-04-08 21:57:16 +00:00
|
|
|
pub type Link = StoreId;
|
2016-02-21 13:48:21 +00:00
|
|
|
|
2016-02-15 12:26:00 +00:00
|
|
|
pub trait InternalLinker {
|
2016-02-03 14:47:14 +00:00
|
|
|
|
2016-02-15 12:26:00 +00:00
|
|
|
/// Get the internal links from the implementor object
|
2016-02-21 13:48:21 +00:00
|
|
|
fn get_internal_links(&self) -> Result<Vec<Link>>;
|
2016-02-15 12:26:00 +00:00
|
|
|
|
|
|
|
/// Set the internal links for the implementor object
|
2016-02-21 13:48:21 +00:00
|
|
|
fn set_internal_links(&mut self, links: Vec<&mut Entry>) -> Result<Vec<Link>>;
|
2016-02-15 12:26:00 +00:00
|
|
|
|
|
|
|
/// Add an internal link to the implementor object
|
2016-02-21 13:48:21 +00:00
|
|
|
fn add_internal_link(&mut self, link: &mut Entry) -> Result<()>;
|
2016-02-15 12:26:00 +00:00
|
|
|
|
|
|
|
/// Remove an internal link from the implementor object
|
2016-02-21 13:48:21 +00:00
|
|
|
fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()>;
|
2016-02-03 14:47:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
impl InternalLinker for Entry {
|
2016-02-15 12:26:00 +00:00
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
fn get_internal_links(&self) -> Result<Vec<Link>> {
|
|
|
|
process_rw_result(self.get_header().read("imag.links"))
|
2016-02-15 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the links in a header and return the old links, if any.
|
2016-02-21 13:48:21 +00:00
|
|
|
fn set_internal_links(&mut self, links: Vec<&mut Entry>) -> Result<Vec<Link>> {
|
|
|
|
let self_location = self.get_location().clone();
|
|
|
|
let mut new_links = vec![];
|
|
|
|
|
|
|
|
for link in links {
|
|
|
|
if let Err(e) = add_foreign_link(link, self_location.clone()) {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
let link = link.get_location().clone();
|
2016-04-16 14:09:34 +00:00
|
|
|
new_links.push(link);
|
2016-02-21 13:48:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 14:09:34 +00:00
|
|
|
let new_links = links_into_values(new_links);
|
|
|
|
if new_links.iter().any(|o| o.is_none()) {
|
|
|
|
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
|
|
|
|
}
|
|
|
|
let new_links = new_links.into_iter().map(|o| o.unwrap()).collect();
|
2016-02-21 13:48:21 +00:00
|
|
|
process_rw_result(self.get_header_mut().set("imag.links", Value::Array(new_links)))
|
2016-02-15 12:26:00 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
fn add_internal_link(&mut self, link: &mut Entry) -> Result<()> {
|
|
|
|
let new_link = link.get_location().clone();
|
|
|
|
|
|
|
|
add_foreign_link(link, self.get_location().clone())
|
|
|
|
.and_then(|_| {
|
|
|
|
self.get_internal_links()
|
|
|
|
.and_then(|mut links| {
|
2016-04-08 21:57:16 +00:00
|
|
|
links.push(new_link);
|
2016-04-08 22:07:30 +00:00
|
|
|
rewrite_links(self.get_header_mut(), links)
|
2016-02-21 13:48:21 +00:00
|
|
|
})
|
2016-02-15 12:26:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()> {
|
2016-04-08 21:57:16 +00:00
|
|
|
let own_loc = link.get_location().clone();
|
2016-02-21 13:48:21 +00:00
|
|
|
let other_loc = link.get_location().clone();
|
|
|
|
|
|
|
|
link.get_internal_links()
|
2016-04-08 21:57:16 +00:00
|
|
|
.and_then(|links| {
|
2016-04-08 22:07:30 +00:00
|
|
|
let links = links.into_iter().filter(|l| l.clone() != own_loc).collect();
|
|
|
|
rewrite_links(self.get_header_mut(), links)
|
2016-02-21 13:48:21 +00:00
|
|
|
})
|
|
|
|
.and_then(|_| {
|
|
|
|
self.get_internal_links()
|
2016-04-08 21:57:16 +00:00
|
|
|
.and_then(|links| {
|
2016-04-08 22:07:30 +00:00
|
|
|
let links = links.into_iter().filter(|l| l.clone() != other_loc).collect();
|
|
|
|
rewrite_links(link.get_header_mut(), links)
|
2016-02-21 13:48:21 +00:00
|
|
|
})
|
2016-02-15 12:26:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-15 12:19:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 14:09:34 +00:00
|
|
|
fn links_into_values(links: Vec<StoreId>) -> Vec<Option<Value>> {
|
|
|
|
links
|
2016-04-08 22:07:30 +00:00
|
|
|
.into_iter()
|
2016-04-16 13:54:38 +00:00
|
|
|
.map(|s| s.to_str().map(|s| String::from(s)))
|
|
|
|
.unique()
|
|
|
|
.map(|elem| elem.map(|s| Value::String(s)))
|
2016-04-16 14:09:00 +00:00
|
|
|
.sorted_by(|a, b| {
|
|
|
|
match (a, b) {
|
|
|
|
(&Some(Value::String(ref a)), &Some(Value::String(ref b))) => Ord::cmp(a, b),
|
|
|
|
(&None, _) | (_, &None) => Ordering::Equal,
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
2016-04-16 14:09:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
|
|
|
|
let links = links_into_values(links);
|
2016-04-08 22:07:30 +00:00
|
|
|
|
|
|
|
if links.iter().any(|o| o.is_none()) {
|
|
|
|
// if any type convert failed we fail as well
|
|
|
|
Err(LinkError::new(LinkErrorKind::InternalConversionError, None))
|
|
|
|
} else {
|
|
|
|
// I know it is ugly
|
|
|
|
let links = links.into_iter().map(|opt| opt.unwrap()).collect();
|
|
|
|
let process = header.set("imag.links", Value::Array(links));
|
|
|
|
process_rw_result(process).map(|_| ())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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<()> {
|
|
|
|
target.get_internal_links()
|
|
|
|
.and_then(|mut links| {
|
2016-04-08 21:57:16 +00:00
|
|
|
links.push(from);
|
2016-04-16 14:09:34 +00:00
|
|
|
let links = links_into_values(links);
|
2016-04-08 21:57:16 +00:00
|
|
|
if links.iter().any(|o| o.is_none()) {
|
|
|
|
Err(LinkError::new(LinkErrorKind::InternalConversionError, None))
|
|
|
|
} else {
|
|
|
|
let links = links.into_iter().map(|opt| opt.unwrap()).collect();
|
|
|
|
process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links)))
|
|
|
|
.map(|_| ())
|
|
|
|
}
|
2016-02-21 13:48:21 +00:00
|
|
|
})
|
2016-02-15 12:27:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 13:48:21 +00:00
|
|
|
fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<Vec<Link>> {
|
2016-02-14 16:50:06 +00:00
|
|
|
if links.is_err() {
|
2016-04-12 19:55:30 +00:00
|
|
|
debug!("RW action on store failed. Generating LinkError");
|
2016-02-14 16:50:06 +00:00
|
|
|
let lerr = LinkError::new(LinkErrorKind::EntryHeaderReadError,
|
2016-04-17 19:08:16 +00:00
|
|
|
Some(Box::new(links.unwrap_err())));
|
2016-02-14 16:50:06 +00:00
|
|
|
return Err(lerr);
|
|
|
|
}
|
|
|
|
let links = links.unwrap();
|
|
|
|
|
2016-04-12 19:55:30 +00:00
|
|
|
if links.is_none() {
|
|
|
|
debug!("We got no value from the header!");
|
|
|
|
return Ok(vec![])
|
|
|
|
}
|
|
|
|
let links = links.unwrap();
|
|
|
|
|
|
|
|
let links = {
|
|
|
|
match links {
|
|
|
|
Value::Array(a) => a,
|
|
|
|
_ => {
|
|
|
|
debug!("We expected an Array for the links, but there was a non-Array!");
|
|
|
|
return Err(LinkError::new(LinkErrorKind::ExistingLinkTypeWrong, None));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if !links.iter().all(|l| match l { &Value::String(_) => true, _ => false }) {
|
|
|
|
debug!("At least one of the Values which were expected in the Array of links is a non-String!");
|
|
|
|
debug!("Generating LinkError");
|
2016-02-14 16:50:06 +00:00
|
|
|
return Err(LinkError::new(LinkErrorKind::ExistingLinkTypeWrong, None));
|
|
|
|
}
|
|
|
|
|
|
|
|
let links : Vec<Link> = links.into_iter()
|
|
|
|
.map(|link| {
|
|
|
|
match link {
|
2016-04-08 21:57:16 +00:00
|
|
|
Value::String(s) => StoreId::from(s),
|
2016-02-14 16:50:06 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2016-04-12 19:55:30 +00:00
|
|
|
debug!("Ok, the RW action was successful, returning link vector now!");
|
2016-02-21 13:48:21 +00:00
|
|
|
Ok(links)
|
2016-02-14 16:50:06 +00:00
|
|
|
}
|
|
|
|
|