Merge branch 'libimagcontact/rewrite-libimagref-api-usage' into libimagentryref/refactor

This commit is contained in:
Matthias Beyer 2018-02-19 12:17:44 +01:00
commit 20654d66f7
8 changed files with 54 additions and 18 deletions

View File

@ -28,7 +28,7 @@ toml-query = "0.6"
handlebars = "0.29" handlebars = "0.29"
vobject = "0.4" vobject = "0.4"
walkdir = "1" walkdir = "1"
uuid = { version = "0.5", features = ["v4"] } uuid = { version = "0.6", features = ["v4"] }
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" } libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }
libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" } libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" }

View File

@ -31,13 +31,13 @@ use toml::Value;
use uuid::Uuid; use uuid::Uuid;
use libimagcontact::error::ContactError as CE; use libimagcontact::error::ContactError as CE;
use libimagcontact::store::UniqueContactPathGenerator;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::str::ErrFromStr; use libimagerror::str::ErrFromStr;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use libimagutil::warn_result::WarnResult; use libimagutil::warn_result::WarnResult;
use libimagentryref::refstore::RefStore; use libimagentryref::refstore::RefStore;
use libimagentryref::flags::RefFlags;
const TEMPLATE : &'static str = include_str!("../static/new-contact-template.toml"); const TEMPLATE : &'static str = include_str!("../static/new-contact-template.toml");
@ -144,11 +144,7 @@ pub fn create(rt: &Runtime) {
if let Some(location) = location { if let Some(location) = location {
if !scmd.is_present("dont-track") { if !scmd.is_present("dont-track") {
let flags = RefFlags::default() RefStore::create_ref::<UniqueContactPathGenerator, _>(rt.store(), location)
.with_content_hashing(true)
.with_permission_tracking(false);
RefStore::create(rt.store(), location, flags)
.map_err_trace_exit_unwrap(1); .map_err_trace_exit_unwrap(1);
info!("Created entry in store"); info!("Created entry in store");

View File

@ -67,6 +67,7 @@ use libimagerror::trace::MapErrTrace;
use libimagerror::io::ToExitCode; use libimagerror::io::ToExitCode;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagcontact::store::ContactStore; use libimagcontact::store::ContactStore;
use libimagcontact::store::UniqueContactPathGenerator;
use libimagcontact::error::ContactError as CE; use libimagcontact::error::ContactError as CE;
use libimagcontact::contact::Contact; use libimagcontact::contact::Contact;
use libimagstore::iter::get::StoreIdGetIteratorExtension; use libimagstore::iter::get::StoreIdGetIteratorExtension;
@ -130,7 +131,7 @@ fn list(rt: &Runtime) {
}) })
.enumerate() .enumerate()
.map(|(i, (fle, vcard))| { .map(|(i, (fle, vcard))| {
let hash = fle.get_path_hash().map_err_trace_exit_unwrap(1); let hash = String::from(fle.get_hash().map_err_trace_exit_unwrap(1));
let vcard = vcard.unwrap_or_else(|e| { let vcard = vcard.unwrap_or_else(|e| {
error!("Element is not a VCARD object: {:?}", e); error!("Element is not a VCARD object: {:?}", e);
exit(1) exit(1)
@ -190,7 +191,7 @@ fn show(rt: &Runtime) {
let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap
let contact_data = rt.store() let contact_data = rt.store()
.get_by_hash(hash.clone()) .get_ref::<UniqueContactPathGenerator, _>(hash.clone())
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.ok_or(CE::from(format!("No entry for hash {}", hash))) .ok_or(CE::from(format!("No entry for hash {}", hash)))
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)

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 { pub 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!()
} }
} }