summaryrefslogtreecommitdiffstats
path: root/lib/entry/libimagentrylink/src/external.rs
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-09-04 23:02:45 +0200
committerMatthias Beyer <mail@beyermatthias.de>2017-09-04 23:02:45 +0200
commitc115215fa4d4e460b08dd7854a5c711a3e819c80 (patch)
tree23a8be2e09100cefb7ed1577edb07af1b06ab5d6 /lib/entry/libimagentrylink/src/external.rs
parent13af22ac16717235043de25b67194992c0c3a846 (diff)
parent6d1dab31179eb4414b45d9a94f453833ddc558ce (diff)
Merge branch 'master' into libimagerror/integration
This merge solved a _LOT_ of conflicts and was a rather complicated one, as parts of the conflict-resolution involved rewriting of half the stuff. This merge commit fixes all the things so a `cargo check --all` succeeds, but I did not yet check whether tests run without failure.
Diffstat (limited to 'lib/entry/libimagentrylink/src/external.rs')
-rw-r--r--lib/entry/libimagentrylink/src/external.rs84
1 files changed, 41 insertions, 43 deletions
diff --git a/lib/entry/libimagentrylink/src/external.rs b/lib/entry/libimagentrylink/src/external.rs
index b13ecbc5..7da4f345 100644
--- a/lib/entry/libimagentrylink/src/external.rs
+++ b/lib/entry/libimagentrylink/src/external.rs
@@ -35,7 +35,6 @@ 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;
@@ -58,37 +57,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 {
-impl<'a> Link<'a> {
+ fn get_link_uri_from_filelockentry(&self) -> Result<Option<Url>>;
- pub fn new(fle: FileLockEntry<'a>) -> Link<'a> {
- Link { link: fle }
- }
+ fn get_url(&self) -> Result<Option<Url>>;
+
+}
+
+impl Link for Entry {
- /// 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()
+ .chain_err(|| LEK::EntryHeaderReadError)
.and_then(|opt| match opt {
Some(&Value::String(ref s)) => {
debug!("Found url, parsing: {:?}", s);
- Url::parse(&s[..]).ok()
+ Url::parse(&s[..]).chain_err(|| LEK::InvalidUri).map(Some)
},
- _ => None
+ Some(_) => Err(LE::from_kind(LEK::LinkParserFieldTypeError)),
+ 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(ref s))) => {
Url::parse(&s[..])
.map(Some)
@@ -261,23 +255,32 @@ pub mod iter {
type Item = Result<Url>;
fn next(&mut self) -> Option<Self::Item> {
- use super::get_external_link_from_file;
-
- self.0
- .next()
- .map(|id| {
- debug!("Retrieving entry for id: '{:?}'", id);
- self.1
- .retrieve(id.clone())
- .chain_err(|| LEK::StoreReadError)
- .map_dbg_err(|_| format!("Retrieving entry for id: '{:?}' failed", id))
- .and_then(|f| {
- debug!("Store::retrieve({:?}) succeeded", id);
- debug!("getting external link from file now");
- get_external_link_from_file(&f)
- .map_dbg_err(|e| format!("URL -> Err = {:?}", e))
- })
- })
+ use external::Link;
+
+ loop {
+ let next = self.0
+ .next()
+ .map(|id| {
+ debug!("Retrieving entry for id: '{:?}'", id);
+ self.1
+ .retrieve(id.clone())
+ .chain_err(|| LEK::StoreReadError)
+ .map_dbg_err(|_| format!("Retrieving entry for id: '{:?}' failed", id))
+ .and_then(|f| {
+ debug!("Store::retrieve({:?}) succeeded", id);
+ debug!("getting external link from file now");
+ 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
+ }
+ }
}
}
@@ -291,11 +294,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::from_kind(LEK::StoreReadError))
-}
-
/// 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.