Make links unique before writing

This commit is contained in:
Matthias Beyer 2016-04-16 15:54:38 +02:00
parent 0f7b2f16f9
commit b63e2cb7b2

View file

@ -7,6 +7,7 @@ use error::{LinkError, LinkErrorKind};
use result::Result; use result::Result;
use toml::Value; use toml::Value;
use itertools::Itertools;
pub type Link = StoreId; pub type Link = StoreId;
@ -48,9 +49,10 @@ impl InternalLinker for Entry {
} }
let link = link.unwrap(); let link = link.unwrap();
new_links.push(Value::String(String::from(link))); new_links.push(String::from(link));
} }
let new_links = new_links.into_iter().unique().map(|s| Value::String(s)).collect();
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)))
} }
@ -90,7 +92,9 @@ impl InternalLinker for Entry {
fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> { fn rewrite_links(header: &mut EntryHeader, links: Vec<StoreId>) -> Result<()> {
let links : Vec<Option<Value>> = links let links : Vec<Option<Value>> = links
.into_iter() .into_iter()
.map(|s| s.to_str().map(|s| Value::String(String::from(s)))) .map(|s| s.to_str().map(|s| String::from(s)))
.unique()
.map(|elem| elem.map(|s| Value::String(s)))
.collect(); .collect();
if links.iter().any(|o| o.is_none()) { if links.iter().any(|o| o.is_none()) {
@ -114,10 +118,12 @@ fn add_foreign_link(target: &mut Entry, from: StoreId) -> Result<()> {
.into_iter() .into_iter()
.map(|s| { .map(|s| {
match s.to_str() { match s.to_str() {
Some(s) => Some(Value::String(String::from(s))), Some(s) => Some(String::from(s)),
_ => None _ => None
} }
}) })
.unique()
.map(|elem| elem.map(|s| Value::String(s)))
.collect(); .collect();
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))