Merge pull request #325 from matthiasbeyer/libimaglink/internal-linking-use-storeid
Libimaglink/internal linking use storeid
This commit is contained in:
commit
cd2bc0bba4
1 changed files with 45 additions and 50 deletions
|
@ -8,7 +8,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,81 +56,76 @@ 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();
|
rewrite_links(self.get_header_mut(), links)
|
||||||
let process = self.get_header_mut().set("imag.links", Value::Array(links));
|
|
||||||
process_rw_result(process)
|
|
||||||
.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 = links.into_iter().filter(|l| l.clone() != own_loc).collect();
|
||||||
.filter(|l| l.clone() != own_loc)
|
rewrite_links(self.get_header_mut(), links)
|
||||||
.map(|s| Value::String(s))
|
|
||||||
.collect();
|
|
||||||
process_rw_result(link.get_header_mut().set("imag.links", Value::Array(links)))
|
|
||||||
.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 = links.into_iter().filter(|l| l.clone() != other_loc).collect();
|
||||||
.into_iter()
|
rewrite_links(link.get_header_mut(), links)
|
||||||
.filter(|l| l.clone() != other_loc)
|
|
||||||
.map(|s| Value::String(s))
|
|
||||||
.collect();
|
|
||||||
process_rw_result(self.get_header_mut().set("imag.links", Value::Array(links)))
|
|
||||||
.map(|_| ())
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
|
||||||
|
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 = header.set("imag.links", Value::Array(links));
|
||||||
|
process_rw_result(process).map(|_| ())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 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 +144,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!(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue