Refactor libimagcontact for new libimagentryref API

This commit is contained in:
Matthias Beyer 2018-02-14 00:12:23 +01:00
parent 1153fb180b
commit 719daf952b
5 changed files with 48 additions and 9 deletions

View File

@ -25,9 +25,15 @@ log = "0.3"
toml = "0.4" toml = "0.4"
toml-query = "0.4" toml-query = "0.4"
vobject = "0.4" vobject = "0.4"
uuid = { version = "0.6", features = ["v4"] }
libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" }
libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" } libimagerror = { version = "0.7.0", path = "../../../lib/core/libimagerror" }
libimagentryref = { version = "0.7.0", path = "../../../lib/entry/libimagentryref/" }
libimagentryutil = { version = "0.7.0", path = "../../../lib/entry/libimagentryutil/" } libimagentryutil = { version = "0.7.0", path = "../../../lib/entry/libimagentryutil/" }
[dependencies.libimagentryref]
version = "0.7.0"
path = "../../../lib/entry/libimagentryref/"
default-features = false
features = ["generators", "generators-sha1"]

View File

@ -54,7 +54,7 @@ impl Contact for Entry {
fn get_contact_data(&self) -> Result<ContactData> { fn get_contact_data(&self) -> Result<ContactData> {
let component = self let component = self
.fs_file() .get_path()
.map_err(From::from) .map_err(From::from)
.and_then(util::read_to_string) .and_then(util::read_to_string)
.and_then(util::parse)?; .and_then(util::parse)?;

View File

@ -34,6 +34,7 @@ error_chain! {
foreign_links { foreign_links {
Io(::std::io::Error); Io(::std::io::Error);
TomlQueryError(::toml_query::error::Error); TomlQueryError(::toml_query::error::Error);
UuidError(::uuid::ParseError);
} }
errors { errors {

View File

@ -38,6 +38,7 @@
extern crate vobject; extern crate vobject;
extern crate toml; extern crate toml;
extern crate toml_query; extern crate toml_query;
extern crate uuid;
#[macro_use] extern crate libimagstore; #[macro_use] extern crate libimagstore;
extern crate libimagerror; extern crate libimagerror;

View File

@ -17,22 +17,54 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::result::Result as RResult;
use std::ffi::OsStr;
use vobject::parse_component; use vobject::parse_component;
use uuid::Uuid;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreIdIterator; use libimagstore::storeid::StoreIdIterator;
use libimagentryref::refstore::RefStore; use libimagentryref::refstore::RefStore;
use libimagentryref::flags::RefFlags; use libimagentryref::refstore::UniqueRefPathGenerator;
use libimagentryref::generators::sha1::Sha1;
use libimagentryutil::isa::Is; use libimagentryutil::isa::Is;
use contact::IsContact; use contact::IsContact;
use error::ContactError as CE;
use error::Result; use error::Result;
use util; use util;
pub trait ContactStore<'a> : RefStore { struct UniqueContactPathGenerator;
impl UniqueRefPathGenerator for UniqueContactPathGenerator {
type Error = CE;
/// The collection the `StoreId` should be created for
fn collection() -> &'static str {
"contact"
}
/// A function which should generate a unique string for a Path
fn unique_hash<A: AsRef<Path>>(path: A) -> RResult<String, Self::Error> {
debug!("Generating unique hash for path: {:?}", path.as_ref());
if let Some(p) = path.as_ref().file_name().and_then(OsStr::to_str).map(String::from) {
debug!("Found UUID string: '{}'", p);
Uuid::parse_str(&p)
.map_err(CE::from)
.map(|u| format!("{}", u.hyphenated())) // FIXME I don't know how to do in not-ugly
} else { // else, we sha1 the (complete) content
debug!("Couldn't find UUID string, using SHA1 of contents");
Sha1::unique_hash(path).map_err(CE::from)
}
}
}
pub trait ContactStore<'a> : RefStore<'a> {
// creating // creating
@ -42,7 +74,7 @@ pub trait ContactStore<'a> : RefStore {
/// ///
/// Needs the `p` argument as we're finally creating a reference by path, the buffer is only for /// Needs the `p` argument as we're finally creating a reference by path, the buffer is only for
/// collecting metadata. /// collecting metadata.
fn create_from_buf(&'a self, p: &PathBuf, buf: &String) -> Result<FileLockEntry<'a>>; fn create_from_buf<P: AsRef<Path>>(&'a self, p: P, buf: &String) -> Result<FileLockEntry<'a>>;
// getting // getting
@ -63,12 +95,11 @@ impl<'a> ContactStore<'a> for Store {
} }
/// Create contact ref from buffer /// Create contact ref from buffer
fn create_from_buf(&'a self, p: &PathBuf, buf: &String) -> Result<FileLockEntry<'a>> { fn create_from_buf<P: AsRef<Path>>(&'a self, p: P, buf: &String) -> Result<FileLockEntry<'a>> {
let component = parse_component(&buf)?; let component = parse_component(&buf)?;
debug!("Parsed: {:?}", component); debug!("Parsed: {:?}", component);
let flags = RefFlags::default().with_content_hashing(true).with_permission_tracking(false); RefStore::create_ref::<UniqueContactPathGenerator, P>(self, p)
RefStore::create(self, p.clone(), flags)
.map_err(From::from) .map_err(From::from)
.and_then(|mut entry| { .and_then(|mut entry| {
entry.set_isflag::<IsContact>() entry.set_isflag::<IsContact>()
@ -78,7 +109,7 @@ impl<'a> ContactStore<'a> for Store {
} }
fn all_contacts(&'a self) -> Result<StoreIdIterator> { fn all_contacts(&'a self) -> Result<StoreIdIterator> {
self.all_references().map_err(From::from) unimplemented!()
} }
} }