2016-05-12 13:28:24 +00:00
|
|
|
//! BookmarkCollection module
|
|
|
|
//!
|
|
|
|
//! A BookmarkCollection is nothing more than a simple store entry. One can simply call functions
|
|
|
|
//! 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.
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
2016-07-08 14:21:24 +00:00
|
|
|
use regex::Regex;
|
|
|
|
|
2016-05-12 13:28:24 +00:00
|
|
|
use error::BookmarkErrorKind as BEK;
|
|
|
|
use error::MapErrInto;
|
|
|
|
use result::Result;
|
|
|
|
use module_path::ModuleEntryPath;
|
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::storeid::IntoStoreId;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagentrylink::external::ExternalLinker;
|
|
|
|
use libimagentrylink::internal::InternalLinker;
|
2016-07-07 22:27:45 +00:00
|
|
|
use libimagentrylink::internal::Link as StoreLink;
|
2016-07-07 22:11:54 +00:00
|
|
|
use libimagerror::into::IntoError;
|
2016-05-12 13:28:24 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2016-07-07 22:27:45 +00:00
|
|
|
use link::Link;
|
|
|
|
|
2016-05-12 13:28:24 +00:00
|
|
|
pub struct BookmarkCollection<'a> {
|
|
|
|
fle: FileLockEntry<'a>,
|
|
|
|
store: &'a Store,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// {Internal, External}Linker is implemented as Deref is implemented
|
|
|
|
impl<'a> Deref for BookmarkCollection<'a> {
|
|
|
|
type Target = FileLockEntry<'a>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &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>> {
|
|
|
|
let id = ModuleEntryPath::new(name).into_storeid();
|
|
|
|
store.create(id)
|
|
|
|
.map(|fle| {
|
|
|
|
BookmarkCollection {
|
|
|
|
fle: fle,
|
|
|
|
store: store,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err_into(BEK::StoreReadError)
|
|
|
|
}
|
|
|
|
|
2016-07-07 22:11:54 +00:00
|
|
|
pub fn get(store: &'a Store, name: &str) -> Result<BookmarkCollection<'a>> {
|
|
|
|
let id = ModuleEntryPath::new(name).into_storeid();
|
|
|
|
store.get(id)
|
|
|
|
.map_err_into(BEK::StoreReadError)
|
|
|
|
.and_then(|fle| {
|
|
|
|
match fle {
|
|
|
|
None => Err(BEK::CollectionNotFound.into_error()),
|
|
|
|
Some(e) => Ok(BookmarkCollection {
|
|
|
|
fle: e,
|
|
|
|
store: store,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
})
|
2016-05-12 13:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete(store: &Store, name: &str) -> Result<()> {
|
2016-07-07 22:12:05 +00:00
|
|
|
store.delete(ModuleEntryPath::new(name).into_storeid()).map_err_into(BEK::StoreReadError)
|
2016-05-12 13:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn links(&self) -> Result<Vec<Url>> {
|
|
|
|
self.fle.get_external_links(&self.store).map_err_into(BEK::LinkError)
|
|
|
|
}
|
|
|
|
|
2016-07-07 22:27:45 +00:00
|
|
|
pub fn link_entries(&self) -> Result<Vec<StoreLink>> {
|
2016-05-12 13:28:24 +00:00
|
|
|
use libimagentrylink::external::is_external_link_storeid;
|
|
|
|
|
|
|
|
self.fle
|
|
|
|
.get_internal_links()
|
|
|
|
.map(|v| v.into_iter().filter(|id| is_external_link_storeid(id)).collect())
|
|
|
|
.map_err_into(BEK::StoreReadError)
|
|
|
|
}
|
|
|
|
|
2016-07-08 14:30:11 +00:00
|
|
|
pub fn add_link(&mut self, l: Link) -> Result<()> {
|
|
|
|
use link::IntoUrl;
|
|
|
|
|
|
|
|
l.into_url()
|
|
|
|
.and_then(|url| self.add_external_link(self.store, url).map_err_into(BEK::LinkingError))
|
|
|
|
.map_err_into(BEK::LinkError)
|
2016-07-07 22:27:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 14:41:37 +00:00
|
|
|
pub fn get_links_matching(&self, r: Regex) -> Result<Vec<Link>> {
|
2016-07-08 14:40:28 +00:00
|
|
|
self.get_external_links(self.store)
|
|
|
|
.map_err_into(BEK::LinkError)
|
|
|
|
.map(|v| {
|
|
|
|
v.into_iter()
|
|
|
|
.map(Url::into_string)
|
|
|
|
.filter(|urlstr| r.is_match(&urlstr[..]))
|
|
|
|
.map(Link::from)
|
2016-07-08 14:41:37 +00:00
|
|
|
.collect()
|
2016-07-08 14:40:28 +00:00
|
|
|
})
|
2016-07-07 22:27:45 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 14:54:27 +00:00
|
|
|
pub fn remove_link(&mut self, l: Link) -> Result<()> {
|
|
|
|
use link::IntoUrl;
|
|
|
|
|
|
|
|
l.into_url()
|
|
|
|
.and_then(|url| {
|
|
|
|
self.remove_external_link(self.store, url).map_err_into(BEK::LinkingError)
|
|
|
|
})
|
|
|
|
.map_err_into(BEK::LinkError)
|
2016-07-07 22:27:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 13:28:24 +00:00
|
|
|
}
|
|
|
|
|