Merge pull request #347 from matthiasbeyer/libimaglink/unique-internal-links
Libimaglink/unique internal links
This commit is contained in:
commit
3d124df0c2
3 changed files with 28 additions and 20 deletions
|
@ -4,6 +4,7 @@ version = "0.1.0"
|
||||||
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
authors = ["Matthias Beyer <mail@beyermatthias.de>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
itertools = "0.4.7"
|
||||||
log = "0.3.4"
|
log = "0.3.4"
|
||||||
toml = "0.1.27"
|
toml = "0.1.27"
|
||||||
url = "0.5.5"
|
url = "0.5.5"
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use libimagstore::storeid::StoreId;
|
use libimagstore::storeid::StoreId;
|
||||||
use libimagstore::store::Entry;
|
use libimagstore::store::Entry;
|
||||||
use libimagstore::store::EntryHeader;
|
use libimagstore::store::EntryHeader;
|
||||||
|
@ -7,6 +9,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;
|
||||||
|
|
||||||
|
@ -42,15 +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() {
|
|
||||||
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
|
|
||||||
}
|
|
||||||
let link = link.unwrap();
|
|
||||||
|
|
||||||
new_links.push(Value::String(String::from(link)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let new_links = links_into_values(new_links);
|
||||||
|
if new_links.iter().any(|o| o.is_none()) {
|
||||||
|
return Err(LinkError::new(LinkErrorKind::InternalConversionError, None));
|
||||||
|
}
|
||||||
|
let new_links = new_links.into_iter().map(|o| o.unwrap()).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)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,11 +89,23 @@ 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| Value::String(String::from(s))))
|
.map(|s| s.to_str().map(|s| String::from(s)))
|
||||||
.collect();
|
.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!()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -110,15 +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(Value::String(String::from(s))),
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.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))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
extern crate itertools;
|
||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
extern crate url;
|
extern crate url;
|
||||||
|
|
Loading…
Reference in a new issue