diff --git a/libimagbookmark/Cargo.toml b/libimagbookmark/Cargo.toml new file mode 100644 index 00000000..2d005855 --- /dev/null +++ b/libimagbookmark/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "libimagbookmark" +version = "0.1.0" +authors = ["Matthias Beyer "] + +[dependencies] +log = "0.3" +semver = "0.2" +url = "1.1" +regex = "0.1" + +[dependencies.libimagstore] +path = "../libimagstore" + +[dependencies.libimagerror] +path = "../libimagerror" + +[dependencies.libimagentrylink] +path = "../libimagentrylink" + diff --git a/libimagbookmark/src/collection.rs b/libimagbookmark/src/collection.rs new file mode 100644 index 00000000..b7fc4459 --- /dev/null +++ b/libimagbookmark/src/collection.rs @@ -0,0 +1,128 @@ +//! 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; + +use regex::Regex; + +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; +use libimagentrylink::internal::Link as StoreLink; +use libimagerror::into::IntoError; +use url::Url; + +use link::Link; + +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> { + let id = ModuleEntryPath::new(name).into_storeid(); + store.create(id) + .map(|fle| { + BookmarkCollection { + fle: fle, + store: store, + } + }) + .map_err_into(BEK::StoreReadError) + } + + pub fn get(store: &'a Store, name: &str) -> Result> { + 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, + }), + } + }) + } + + pub fn delete(store: &Store, name: &str) -> Result<()> { + store.delete(ModuleEntryPath::new(name).into_storeid()).map_err_into(BEK::StoreReadError) + } + + pub fn links(&self) -> Result> { + self.fle.get_external_links(&self.store).map_err_into(BEK::LinkError) + } + + pub fn link_entries(&self) -> Result> { + 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) + } + + 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) + } + + pub fn get_links_matching(&self, r: Regex) -> Result> { + 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) + .collect() + }) + } + + 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) + } + +} + diff --git a/libimagbookmark/src/error.rs b/libimagbookmark/src/error.rs new file mode 100644 index 00000000..79849d9e --- /dev/null +++ b/libimagbookmark/src/error.rs @@ -0,0 +1,14 @@ +generate_error_module!( + generate_error_types!(BookmarkError, BookmarkErrorKind, + StoreReadError => "Store read error", + LinkError => "Link error", + LinkParsingError => "Link parsing error", + LinkingError => "Error while linking", + CollectionNotFound => "Link-Collection not found" + ); +); + +pub use self::error::BookmarkError; +pub use self::error::BookmarkErrorKind; +pub use self::error::MapErrInto; + diff --git a/libimagbookmark/src/lib.rs b/libimagbookmark/src/lib.rs new file mode 100644 index 00000000..e33a2466 --- /dev/null +++ b/libimagbookmark/src/lib.rs @@ -0,0 +1,15 @@ +#[macro_use] extern crate log; +extern crate semver; +extern crate url; +extern crate regex; + +#[macro_use] extern crate libimagstore; +#[macro_use] extern crate libimagerror; +extern crate libimagentrylink; + +module_entry_path_mod!("bookmark", "0.1.0"); + +pub mod collection; +pub mod error; +pub mod link; +pub mod result; diff --git a/libimagbookmark/src/link.rs b/libimagbookmark/src/link.rs new file mode 100644 index 00000000..078d45d7 --- /dev/null +++ b/libimagbookmark/src/link.rs @@ -0,0 +1,57 @@ +use std::ops::{Deref, DerefMut}; + +use result::Result; + +use url::Url; + +#[derive(Debug, Clone)] +pub struct Link(String); + +impl From for Link { + + fn from(s: String) -> Link { + Link(s) + } + +} + +impl<'a> From<&'a str> for Link { + + fn from(s: &'a str) -> Link { + Link(String::from(s)) + } + +} + +impl Deref for Link { + type Target = String; + + fn deref(&self) -> &String { + &self.0 + } + +} + +impl DerefMut for Link { + + fn deref_mut(&mut self) -> &mut String { + &mut self.0 + } + +} + +pub trait IntoUrl { + fn into_url(self) -> Result; +} + +impl IntoUrl for Link { + + fn into_url(self) -> Result { + use error::BookmarkErrorKind as BEK; + use error::MapErrInto; + + Url::parse(&self[..]).map_err_into(BEK::LinkParsingError) + } + +} + diff --git a/libimagbookmark/src/result.rs b/libimagbookmark/src/result.rs new file mode 100644 index 00000000..265fec97 --- /dev/null +++ b/libimagbookmark/src/result.rs @@ -0,0 +1,6 @@ +use std::result::Result as RResult; + +use error::BookmarkError; + +pub type Result = RResult; +