Refactor: Use helper for type conversions instead of doing everything three times
This commit is contained in:
parent
915e711c3e
commit
59071c9948
1 changed files with 14 additions and 37 deletions
|
@ -45,25 +45,14 @@ impl InternalLinker for Entry {
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
let link = link.get_location().clone();
|
let link = link.get_location().clone();
|
||||||
let link = link.to_str();
|
new_links.push(link);
|
||||||
if link.is_none() {
|
}
|
||||||
|
|
||||||
|
let new_links = links_into_values(new_links);
|
||||||
|
if new_links.iter().any(|o| o.is_none()) {
|
||||||
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
|
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
|
||||||
}
|
}
|
||||||
let link = link.unwrap();
|
let new_links = new_links.into_iter().map(|o| o.unwrap()).collect();
|
||||||
|
|
||||||
new_links.push(String::from(link));
|
|
||||||
}
|
|
||||||
|
|
||||||
let new_links = new_links
|
|
||||||
.into_iter()
|
|
||||||
.unique()
|
|
||||||
.map(|s| Value::String(s))
|
|
||||||
.sorted_by(|a, b| {
|
|
||||||
match (a, b) {
|
|
||||||
(&Value::String(ref a), &Value::String(ref b)) => Ord::cmp(a, b),
|
|
||||||
_ => unreachable!()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
process_rw_result(self.get_header_mut().set("imag.links", Value::Array(new_links)))
|
process_rw_result(self.get_header_mut().set("imag.links", Value::Array(new_links)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,8 +89,8 @@ impl InternalLinker for Entry {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
|
fn links_into_values(links: Vec<StoreId>) -> Vec<Option<Value>> {
|
||||||
let links : Vec<Option<Value>> = links
|
links
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|s| s.to_str().map(|s| String::from(s)))
|
.map(|s| s.to_str().map(|s| String::from(s)))
|
||||||
.unique()
|
.unique()
|
||||||
|
@ -112,7 +101,11 @@ fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
|
||||||
(&None, _) | (_, &None) => Ordering::Equal,
|
(&None, _) | (_, &None) => Ordering::Equal,
|
||||||
_ => unreachable!()
|
_ => unreachable!()
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
|
||||||
|
let links = links_into_values(links);
|
||||||
|
|
||||||
if links.iter().any(|o| o.is_none()) {
|
if links.iter().any(|o| o.is_none()) {
|
||||||
// if any type convert failed we fail as well
|
// if any type convert failed we fail as well
|
||||||
|
@ -131,23 +124,7 @@ fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
|
||||||
target.get_internal_links()
|
target.get_internal_links()
|
||||||
.and_then(|mut links| {
|
.and_then(|mut links| {
|
||||||
links.push(from);
|
links.push(from);
|
||||||
let links : Vec<Option<Value>> = links
|
let links = links_into_values(links);
|
||||||
.into_iter()
|
|
||||||
.map(|s| {
|
|
||||||
match s.to_str() {
|
|
||||||
Some(s) => Some(String::from(s)),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unique()
|
|
||||||
.map(|elem| elem.map(|s| Value::String(s)))
|
|
||||||
.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!()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if links.iter().any(|o| o.is_none()) {
|
if links.iter().any(|o| o.is_none()) {
|
||||||
Err(LinkError::new(LinkErrorKind::InternalConversionError, None))
|
Err(LinkError::new(LinkErrorKind::InternalConversionError, None))
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue