Merge pull request #1208 from matthiasbeyer/libimagdiary-bookmarkcollection-nonwrapping

libimagdiary: Do not wrap store types
This commit is contained in:
Matthias Beyer 2018-01-08 23:11:54 +01:00 committed by GitHub
commit 7cf60d5bec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 118 deletions

View file

@ -51,9 +51,10 @@ use toml_query::read::TomlValueReadExt;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagbookmark::collection::BookmarkCollection; use libimagbookmark::collection::BookmarkCollection;
use libimagbookmark::collection::BookmarkCollectionStore;
use libimagbookmark::error::BookmarkError as BE;
use libimagbookmark::link::Link as BookmarkLink; use libimagbookmark::link::Link as BookmarkLink;
use libimagerror::trace::{MapErrTrace, trace_error, trace_error_exit}; use libimagerror::trace::{MapErrTrace, trace_error, trace_error_exit};
use libimagutil::info_result::*;
mod ui; mod ui;
@ -85,16 +86,18 @@ fn add(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("add").unwrap(); let scmd = rt.cli().subcommand_matches("add").unwrap();
let coll = get_collection_name(rt, "add", "collection"); let coll = get_collection_name(rt, "add", "collection");
BookmarkCollection::get(rt.store(), &coll) let mut collection = BookmarkCollectionStore::get(rt.store(), &coll)
.and_then(|mut collection| { .map_err_trace_exit_unwrap(1)
.ok_or(BE::from(format!("No BookmarkcollectionStore '{}' found", coll)))
.map_err_trace_exit_unwrap(1);
for url in scmd.values_of("urls").unwrap() { // unwrap saved by clap for url in scmd.values_of("urls").unwrap() { // unwrap saved by clap
let _ = collection.add_link(BookmarkLink::from(url))?; let _ = collection
.add_link(rt.store(), BookmarkLink::from(url))
.map_err_trace_exit_unwrap(1);
} }
Ok(())
}) info!("Ready");
.map_err_trace()
.map_info_str("Ready")
.ok();
} }
fn collection(rt: &Runtime) { fn collection(rt: &Runtime) {
@ -102,7 +105,7 @@ fn collection(rt: &Runtime) {
if scmd.is_present("add") { // adding a new collection if scmd.is_present("add") { // adding a new collection
let name = scmd.value_of("add").unwrap(); let name = scmd.value_of("add").unwrap();
if let Ok(_) = BookmarkCollection::new(rt.store(), &name) { if let Ok(_) = BookmarkCollectionStore::new(rt.store(), &name) {
info!("Created: {}", name); info!("Created: {}", name);
} else { } else {
warn!("Creating collection {} failed", name); warn!("Creating collection {} failed", name);
@ -112,7 +115,7 @@ fn collection(rt: &Runtime) {
if scmd.is_present("remove") { // remove a collection if scmd.is_present("remove") { // remove a collection
let name = scmd.value_of("remove").unwrap(); let name = scmd.value_of("remove").unwrap();
if let Ok(_) = BookmarkCollection::delete(rt.store(), &name) { if let Ok(_) = BookmarkCollectionStore::delete(rt.store(), &name) {
info!("Deleted: {}", name); info!("Deleted: {}", name);
} else { } else {
warn!("Deleting collection {} failed", name); warn!("Deleting collection {} failed", name);
@ -124,9 +127,12 @@ fn collection(rt: &Runtime) {
fn list(rt: &Runtime) { fn list(rt: &Runtime) {
let coll = get_collection_name(rt, "list", "collection"); let coll = get_collection_name(rt, "list", "collection");
BookmarkCollection::get(rt.store(), &coll) let collection = BookmarkCollectionStore::get(rt.store(), &coll)
.map(|collection| { .map_err_trace_exit_unwrap(1)
match collection.links() { .ok_or(BE::from(format!("No BookmarkcollectionStore '{}' found", coll)))
.map_err_trace_exit_unwrap(1);
match collection.links(rt.store()) {
Ok(links) => { Ok(links) => {
debug!("Listing..."); debug!("Listing...");
for (i, link) in links.enumerate() { for (i, link) in links.enumerate() {
@ -139,8 +145,7 @@ fn list(rt: &Runtime) {
}, },
Err(e) => trace_error_exit(&e, 1), Err(e) => trace_error_exit(&e, 1),
} }
})
.ok();
info!("Ready"); info!("Ready");
} }
@ -148,13 +153,17 @@ fn remove(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("remove").unwrap(); let scmd = rt.cli().subcommand_matches("remove").unwrap();
let coll = get_collection_name(rt, "list", "collection"); let coll = get_collection_name(rt, "list", "collection");
BookmarkCollection::get(rt.store(), &coll) let mut collection = BookmarkCollectionStore::get(rt.store(), &coll)
.map(|mut collection| { .map_err_trace_exit_unwrap(1)
.ok_or(BE::from(format!("No BookmarkcollectionStore '{}' found", coll)))
.map_err_trace_exit_unwrap(1);
for url in scmd.values_of("urls").unwrap() { // enforced by clap for url in scmd.values_of("urls").unwrap() { // enforced by clap
collection.remove_link(BookmarkLink::from(url)).map_err(|e| trace_error(&e)).ok(); collection
.remove_link(rt.store(), BookmarkLink::from(url))
.map_err_trace_exit_unwrap(1);
} }
})
.ok();
info!("Ready"); info!("Ready");
} }

View file

@ -32,6 +32,7 @@ This section contains the changelog from the last release to the next release.
* Minor changes * Minor changes
* Internals were refactored from `match`ing all the things into function * Internals were refactored from `match`ing all the things into function
chaining chaining
* `libimagbookmark` does not longer wrap types from the store.
* Bugfixes * Bugfixes
## 0.5.0 ## 0.5.0

View file

@ -23,20 +23,16 @@
//! from the libimagentrylink::external::ExternalLinker trait on this to generate external links. //! from the libimagentrylink::external::ExternalLinker trait on this to generate external links.
//! //!
//! The BookmarkCollection type offers helper functions to get all links or such things. //! The BookmarkCollection type offers helper functions to get all links or such things.
use std::ops::Deref;
use std::ops::DerefMut;
use regex::Regex; use regex::Regex;
use error::BookmarkErrorKind as BEK;
use error::BookmarkError as BE;
use error::ResultExt;
use error::Result; use error::Result;
use module_path::ModuleEntryPath; use module_path::ModuleEntryPath;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagstore::storeid::IntoStoreId; use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::IntoStoreId;
use libimagentrylink::external::ExternalLinker; use libimagentrylink::external::ExternalLinker;
use libimagentrylink::external::iter::UrlIter; use libimagentrylink::external::iter::UrlIter;
use libimagentrylink::internal::InternalLinker; use libimagentrylink::internal::InternalLinker;
@ -46,99 +42,81 @@ use link::Link;
use self::iter::LinksMatchingRegexIter; use self::iter::LinksMatchingRegexIter;
pub struct BookmarkCollection<'a> { pub trait BookmarkCollectionStore<'a> {
fle: FileLockEntry<'a>, fn new(&'a self, name: &str) -> Result<FileLockEntry<'a>>;
store: &'a Store, fn get(&'a self, name: &str) -> Result<Option<FileLockEntry<'a>>>;
fn delete(&'a self, name: &str) -> Result<()>;
} }
/// {Internal, External}Linker is implemented as Deref is implemented impl<'a> BookmarkCollectionStore<'a> for Store {
impl<'a> Deref for BookmarkCollection<'a> {
type Target = FileLockEntry<'a>;
fn deref(&self) -> &FileLockEntry<'a> { fn new(&'a self, name: &str) -> Result<FileLockEntry<'a>> {
&self.fle
}
}
impl<'a> DerefMut for BookmarkCollection<'a> {
fn deref_mut(&mut self) -> &mut FileLockEntry<'a> {
&mut self.fle
}
}
impl<'a> BookmarkCollection<'a> {
pub fn new(store: &'a Store, name: &str) -> Result<BookmarkCollection<'a>> {
ModuleEntryPath::new(name) ModuleEntryPath::new(name)
.into_storeid() .into_storeid()
.and_then(|id| store.create(id)) .and_then(|id| self.create(id).map_err(From::from))
.map(|fle| { .map_err(From::from)
BookmarkCollection {
fle: fle,
store: store,
}
})
.chain_err(|| BEK::StoreReadError)
} }
pub fn get(store: &'a Store, name: &str) -> Result<BookmarkCollection<'a>> { fn get(&'a self, name: &str) -> Result<Option<FileLockEntry<'a>>> {
ModuleEntryPath::new(name) ModuleEntryPath::new(name)
.into_storeid() .into_storeid()
.and_then(|id| store.get(id)) .and_then(|id| self.get(id).map_err(From::from))
.chain_err(|| BEK::StoreReadError) .map_err(From::from)
.and_then(|fle| {
fle.ok_or(BE::from_kind(BEK::CollectionNotFound))
.map(|e| BookmarkCollection { fle: e, store: store })
})
} }
pub fn delete(store: &Store, name: &str) -> Result<()> { fn delete(&'a self, name: &str) -> Result<()> {
ModuleEntryPath::new(name) ModuleEntryPath::new(name)
.into_storeid() .into_storeid()
.and_then(|id| store.delete(id)) .and_then(|id| self.delete(id).map_err(From::from))
.chain_err(|| BEK::StoreReadError) .map_err(From::from)
} }
pub fn links(&self) -> Result<UrlIter> {
self.fle.get_external_links(&self.store).chain_err(|| BEK::LinkError)
} }
pub fn link_entries(&self) -> Result<Vec<StoreLink>> { pub trait BookmarkCollection : Sized + InternalLinker + ExternalLinker {
fn links<'a>(&self, store: &'a Store) -> Result<UrlIter<'a>>;
fn link_entries(&self) -> Result<Vec<StoreLink>>;
fn add_link(&mut self, store: &Store, l: Link) -> Result<()>;
fn get_links_matching<'a>(&self, store: &'a Store, r: Regex) -> Result<LinksMatchingRegexIter<'a>>;
fn remove_link(&mut self, store: &Store, l: Link) -> Result<()>;
}
impl BookmarkCollection for Entry {
fn links<'a>(&self, store: &'a Store) -> Result<UrlIter<'a>> {
self.get_external_links(store).map_err(From::from)
}
fn link_entries(&self) -> Result<Vec<StoreLink>> {
use libimagentrylink::external::is_external_link_storeid; use libimagentrylink::external::is_external_link_storeid;
self.fle self.get_internal_links()
.get_internal_links()
.map(|v| v.filter(|id| is_external_link_storeid(id)).collect()) .map(|v| v.filter(|id| is_external_link_storeid(id)).collect())
.chain_err(|| BEK::StoreReadError) .map_err(From::from)
} }
pub fn add_link(&mut self, l: Link) -> Result<()> { fn add_link(&mut self, store: &Store, l: Link) -> Result<()> {
use link::IntoUrl; use link::IntoUrl;
l.into_url() l.into_url()
.and_then(|url| self.add_external_link(self.store, url).chain_err(|| BEK::LinkingError)) .and_then(|url| self.add_external_link(store, url).map_err(From::from))
.chain_err(|| BEK::LinkError) .map_err(From::from)
} }
pub fn get_links_matching(&self, r: Regex) -> Result<LinksMatchingRegexIter<'a>> { fn get_links_matching<'a>(&self, store: &'a Store, r: Regex) -> Result<LinksMatchingRegexIter<'a>> {
use self::iter::IntoLinksMatchingRegexIter; use self::iter::IntoLinksMatchingRegexIter;
self.get_external_links(self.store) self.get_external_links(store)
.chain_err(|| BEK::LinkError)
.map(|iter| iter.matching_regex(r)) .map(|iter| iter.matching_regex(r))
.map_err(From::from)
} }
pub fn remove_link(&mut self, l: Link) -> Result<()> { fn remove_link(&mut self, store: &Store, l: Link) -> Result<()> {
use link::IntoUrl; use link::IntoUrl;
l.into_url() l.into_url()
.and_then(|url| { .and_then(|url| self.remove_external_link(store, url).map_err(From::from))
self.remove_external_link(self.store, url).chain_err(|| BEK::LinkingError) .map_err(From::from)
})
.chain_err(|| BEK::LinkError)
} }
} }
@ -146,7 +124,7 @@ impl<'a> BookmarkCollection<'a> {
pub mod iter { pub mod iter {
use link::Link; use link::Link;
use error::Result; use error::Result;
use error::{ResultExt, BookmarkErrorKind as BEK}; use error::BookmarkError as BE;
pub struct LinkIter<I>(I) pub struct LinkIter<I>(I)
where I: Iterator<Item = Link>; where I: Iterator<Item = Link>;
@ -189,7 +167,7 @@ pub mod iter {
loop { loop {
let n = match self.0.next() { let n = match self.0.next() {
Some(Ok(n)) => n, Some(Ok(n)) => n,
Some(Err(e)) => return Some(Err(e).chain_err(|| BEK::LinkError)), Some(Err(e)) => return Some(Err(BE::from(e))),
None => return None, None => return None,
}; };

View file

@ -22,17 +22,12 @@ error_chain! {
BookmarkError, BookmarkErrorKind, ResultExt, Result; BookmarkError, BookmarkErrorKind, ResultExt, Result;
} }
links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
LinkError(::libimagentrylink::error::LinkError, ::libimagentrylink::error::LinkErrorKind);
}
errors { errors {
StoreReadError {
description("Store read error")
display("Store read error")
}
LinkError {
description("Link error")
display("Link error")
}
LinkParsingError { LinkParsingError {
description("Link parsing error") description("Link parsing error")
display("Link parsing error") display("Link parsing error")