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::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};
|
||||||
|
@ -61,23 +62,7 @@ impl InternalLinker for Entry {
|
||||||
self.get_internal_links()
|
self.get_internal_links()
|
||||||
.and_then(|mut links| {
|
.and_then(|mut links| {
|
||||||
links.push(new_link);
|
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
|
|
||||||
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));
|
|
||||||
process_rw_result(process)
|
|
||||||
.map(|_| ())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -88,50 +73,37 @@ impl InternalLinker for Entry {
|
||||||
|
|
||||||
link.get_internal_links()
|
link.get_internal_links()
|
||||||
.and_then(|links| {
|
.and_then(|links| {
|
||||||
let links : Vec<Option<Value>> = 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| {
|
|
||||||
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(|_| {
|
.and_then(|_| {
|
||||||
self.get_internal_links()
|
self.get_internal_links()
|
||||||
.and_then(|links| {
|
.and_then(|links| {
|
||||||
let links : Vec<Option<Value>> = 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| {
|
|
||||||
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(|_| ())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<()> {
|
||||||
|
|
Loading…
Reference in a new issue