Merge pull request #1027 from matthiasbeyer/libimagentrylink/extension-as-trait

libimagentrylink: Extensions as traits
This commit is contained in:
Matthias Beyer 2017-08-28 12:21:31 +02:00 committed by GitHub
commit 1f340a3e17

View file

@ -35,12 +35,12 @@ use std::collections::BTreeMap;
use std::fmt::Debug;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::toml_ext::TomlValueExt;
use libimagutil::debug_result::*;
use libimagerror::into::IntoError;
use error::LinkError as LE;
use error::LinkErrorKind as LEK;
@ -56,37 +56,32 @@ use url::Url;
use crypto::sha1::Sha1;
use crypto::digest::Digest;
/// "Link" Type, just an abstraction over `FileLockEntry` to have some convenience internally.
pub struct Link<'a> {
link: FileLockEntry<'a>
pub trait Link {
fn get_link_uri_from_filelockentry(&self) -> Result<Option<Url>>;
fn get_url(&self) -> Result<Option<Url>>;
}
impl<'a> Link<'a> {
impl Link for Entry {
pub fn new(fle: FileLockEntry<'a>) -> Link<'a> {
Link { link: fle }
}
/// Get a link Url object from a `FileLockEntry`, ignore errors.
fn get_link_uri_from_filelockentry(file: &FileLockEntry<'a>) -> Option<Url> {
file.get_header()
fn get_link_uri_from_filelockentry(&self) -> Result<Option<Url>> {
self.get_header()
.read("imag.content.url")
.ok()
.map_err_into(LEK::EntryHeaderReadError)
.and_then(|opt| match opt {
Some(Value::String(s)) => {
debug!("Found url, parsing: {:?}", s);
Url::parse(&s[..]).ok()
Url::parse(&s[..]).map_err_into(LEK::InvalidUri).map(Some)
},
_ => None
Some(_) => Err(LEK::LinkParserFieldTypeError.into_error()),
None => Ok(None),
})
}
pub fn get_url(&self) -> Result<Option<Url>> {
let opt = self.link
.get_header()
.read("imag.content.url");
match opt {
fn get_url(&self) -> Result<Option<Url>> {
match self.get_header().read("imag.content.url") {
Ok(Some(Value::String(s))) => {
Url::parse(&s[..])
.map(Some)
@ -259,9 +254,10 @@ pub mod iter {
type Item = Result<Url>;
fn next(&mut self) -> Option<Self::Item> {
use super::get_external_link_from_file;
use external::Link;
self.0
loop {
let next = self.0
.next()
.map(|id| {
debug!("Retrieving entry for id: '{:?}'", id);
@ -272,10 +268,18 @@ pub mod iter {
.and_then(|f| {
debug!("Store::retrieve({:?}) succeeded", id);
debug!("getting external link from file now");
get_external_link_from_file(&f)
f.get_link_uri_from_filelockentry()
.map_dbg_err(|e| format!("URL -> Err = {:?}", e))
})
})
});
match next {
Some(Ok(Some(link))) => return Some(Ok(link)),
Some(Ok(None)) => continue,
Some(Err(e)) => return Some(Err(e)),
None => return None
}
}
}
}
@ -289,11 +293,6 @@ pub fn is_external_link_storeid<A: AsRef<StoreId> + Debug>(id: A) -> bool {
id.as_ref().local().starts_with("links/external")
}
fn get_external_link_from_file(entry: &FileLockEntry) -> Result<Url> {
Link::get_link_uri_from_filelockentry(entry) // TODO: Do not hide error by using this function
.ok_or(LE::new(LEK::StoreReadError, None))
}
/// Implement `ExternalLinker` for `Entry`, hiding the fact that there is no such thing as an external
/// link in an entry, but internal links to other entries which serve as external links, as one
/// entry in the store can only have one external link.