Refactor common pattern into function
This commit is contained in:
parent
77fa6d71b3
commit
8d9d6651fb
1 changed files with 23 additions and 51 deletions
|
@ -1,5 +1,6 @@
|
|||
use libimagstore::storeid::StoreId;
|
||||
use libimagstore::store::Entry;
|
||||
use libimagstore::store::EntryHeader;
|
||||
use libimagstore::store::Result as StoreResult;
|
||||
|
||||
use error::{LinkError, LinkErrorKind};
|
||||
|
@ -61,8 +62,32 @@ impl InternalLinker for Entry {
|
|||
self.get_internal_links()
|
||||
.and_then(|mut links| {
|
||||
links.push(new_link);
|
||||
rewrite_links(self.get_header_mut(), links)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// try to convert them to str so we can put them back into the header
|
||||
fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()> {
|
||||
let own_loc = link.get_location().clone();
|
||||
let other_loc = link.get_location().clone();
|
||||
|
||||
link.get_internal_links()
|
||||
.and_then(|links| {
|
||||
let links = links.into_iter().filter(|l| l.clone() != own_loc).collect();
|
||||
rewrite_links(self.get_header_mut(), links)
|
||||
})
|
||||
.and_then(|_| {
|
||||
self.get_internal_links()
|
||||
.and_then(|links| {
|
||||
let links = links.into_iter().filter(|l| l.clone() != other_loc).collect();
|
||||
rewrite_links(link.get_header_mut(), links)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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))))
|
||||
|
@ -74,62 +99,9 @@ impl InternalLinker for Entry {
|
|||
} 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));
|
||||
process_rw_result(process)
|
||||
.map(|_| ())
|
||||
let process = header.set("imag.links", Value::Array(links));
|
||||
process_rw_result(process).map(|_| ())
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn remove_internal_link(&mut self, link: &mut Entry) -> Result<()> {
|
||||
let own_loc = link.get_location().clone();
|
||||
let other_loc = link.get_location().clone();
|
||||
|
||||
link.get_internal_links()
|
||||
.and_then(|links| {
|
||||
let links : Vec<Option<Value>> = links.into_iter()
|
||||
.filter(|l| l.clone() != own_loc)
|
||||
.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(self.get_header_mut().set("imag.links", Value::Array(links)))
|
||||
.map(|_| ())
|
||||
}
|
||||
})
|
||||
.and_then(|_| {
|
||||
self.get_internal_links()
|
||||
.and_then(|links| {
|
||||
let links : Vec<Option<Value>> = links
|
||||
.into_iter()
|
||||
.filter(|l| l.clone() != other_loc)
|
||||
.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(link.get_header_mut().set("imag.links", Value::Array(links)))
|
||||
.map(|_| ())
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// When Linking A -> B, the specification wants us to link back B -> A.
|
||||
|
|
Loading…
Reference in a new issue