Rewrite internal linking to use StoreId instead of String as Link type

This commit is contained in:
Matthias Beyer 2016-04-08 23:57:16 +02:00
parent 1fc7b64171
commit 77fa6d71b3

View file

@ -1,6 +1,5 @@
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagstore::store::EntryHeader;
use libimagstore::store::Result as StoreResult; use libimagstore::store::Result as StoreResult;
use error::{LinkError, LinkErrorKind}; use error::{LinkError, LinkErrorKind};
@ -8,7 +7,7 @@ use result::Result;
use toml::Value; use toml::Value;
pub type Link = String; pub type Link = StoreId;
pub trait InternalLinker { pub trait InternalLinker {
@ -56,59 +55,77 @@ impl InternalLinker for Entry {
fn add_internal_link(&mut self, link: &mut Entry) -> Result<()> { fn add_internal_link(&mut self, link: &mut Entry) -> Result<()> {
let new_link = link.get_location().clone(); let new_link = link.get_location().clone();
let new_link = new_link.to_str();
if new_link.is_none() {
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
}
let new_link = new_link.unwrap();
add_foreign_link(link, self.get_location().clone()) add_foreign_link(link, self.get_location().clone())
.and_then(|_| { .and_then(|_| {
self.get_internal_links() self.get_internal_links()
.and_then(|mut links| { .and_then(|mut links| {
links.push(String::from(new_link)); links.push(new_link);
let links = links.into_iter().map(|s| Value::String(s)).collect();
// try to convert them to str so we can put them back into the header
let links : Vec<Option<Value>> = links
.into_iter()
.map(|s| s.to_str().map(|s| Value::String(String::from(s))))
.collect();
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 = self.get_header_mut().set("imag.links", Value::Array(links)); let process = self.get_header_mut().set("imag.links", Value::Array(links));
process_rw_result(process) process_rw_result(process)
.map(|_| ()) .map(|_| ())
}
}) })
}) })
} }
fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()> { fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()> {
let own_loc = link.get_location().clone(); let own_loc = link.get_location().clone();
let own_loc = own_loc.to_str();
if own_loc.is_none() {
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
}
let own_loc = own_loc.unwrap();
let other_loc = link.get_location().clone(); let other_loc = link.get_location().clone();
let other_loc = other_loc.to_str();
if other_loc.is_none() {
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
}
let other_loc = other_loc.unwrap();
link.get_internal_links() link.get_internal_links()
.and_then(|mut links| { .and_then(|links| {
let links = links.into_iter() let links : Vec<Option<Value>> = links.into_iter()
.filter(|l| l.clone() != own_loc) .filter(|l| l.clone() != own_loc)
.map(|s| Value::String(s)) .map(|s| {
match s.to_str() {
Some(s) => Some(Value::String(String::from(s))),
_ => None
}
})
.collect(); .collect();
process_rw_result(link.get_header_mut().set("imag.links", Value::Array(links)))
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(self.get_header_mut().set("imag.links", Value::Array(links)))
.map(|_| ()) .map(|_| ())
}
}) })
.and_then(|_| { .and_then(|_| {
self.get_internal_links() self.get_internal_links()
.and_then(|mut links| { .and_then(|links| {
let links = links let links : Vec<Option<Value>> = links
.into_iter() .into_iter()
.filter(|l| l.clone() != other_loc) .filter(|l| l.clone() != other_loc)
.map(|s| Value::String(s)) .map(|s| {
match s.to_str() {
Some(s) => Some(Value::String(String::from(s))),
_ => None
}
})
.collect(); .collect();
process_rw_result(self.get_header_mut().set("imag.links", Value::Array(links))) 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(link.get_header_mut().set("imag.links", Value::Array(links)))
.map(|_| ()) .map(|_| ())
}
}) })
}) })
} }
@ -118,19 +135,25 @@ impl InternalLinker for Entry {
/// When Linking A -> B, the specification wants us to link back B -> A. /// When Linking A -> B, the specification wants us to link back B -> A.
/// This is a helper function which does this. /// This is a helper function which does this.
fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> { fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
let from = from.to_str();
if from.is_none() {
debug!("Cannot convert pathbuf '{:?}' to String", from);
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
}
let from = from.unwrap();
target.get_internal_links() target.get_internal_links()
.and_then(|mut links| { .and_then(|mut links| {
links.push(String::from(from)); links.push(from);
let links = links.into_iter().map(|s| Value::String(s)).collect(); let links : Vec<Option<Value>> = links
.into_iter()
.map(|s| {
match s.to_str() {
Some(s) => Some(Value::String(String::from(s))),
_ => None
}
})
.collect();
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))) process_rw_result(target.get_header_mut().set("imag.links", Value::Array(links)))
.map(|_| ()) .map(|_| ())
}
}) })
} }
@ -149,7 +172,7 @@ fn process_rw_result(links: StoreResult<Option<Value>>) -> Result<Vec<Link>> {
let links : Vec<Link> = links.into_iter() let links : Vec<Link> = links.into_iter()
.map(|link| { .map(|link| {
match link { match link {
Value::String(s) => String::from(s), Value::String(s) => StoreId::from(s),
_ => unreachable!(), _ => unreachable!(),
} }
}) })