From 719daf952b99cbe92c5e59f996e8851da378d810 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 14 Feb 2018 00:12:23 +0100 Subject: [PATCH 1/3] Refactor libimagcontact for new libimagentryref API --- lib/domain/libimagcontact/Cargo.toml | 8 ++++- lib/domain/libimagcontact/src/contact.rs | 2 +- lib/domain/libimagcontact/src/error.rs | 1 + lib/domain/libimagcontact/src/lib.rs | 1 + lib/domain/libimagcontact/src/store.rs | 45 ++++++++++++++++++++---- 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/domain/libimagcontact/Cargo.toml b/lib/domain/libimagcontact/Cargo.toml index 23608c9e..cc1d3cb0 100644 --- a/lib/domain/libimagcontact/Cargo.toml +++ b/lib/domain/libimagcontact/Cargo.toml @@ -25,9 +25,15 @@ log = "0.3" toml = "0.4" toml-query = "0.4" vobject = "0.4" +uuid = { version = "0.6", features = ["v4"] } libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" } 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/" } +[dependencies.libimagentryref] +version = "0.7.0" +path = "../../../lib/entry/libimagentryref/" +default-features = false +features = ["generators", "generators-sha1"] + diff --git a/lib/domain/libimagcontact/src/contact.rs b/lib/domain/libimagcontact/src/contact.rs index f94b7ae1..375eeed4 100644 --- a/lib/domain/libimagcontact/src/contact.rs +++ b/lib/domain/libimagcontact/src/contact.rs @@ -54,7 +54,7 @@ impl Contact for Entry { fn get_contact_data(&self) -> Result { let component = self - .fs_file() + .get_path() .map_err(From::from) .and_then(util::read_to_string) .and_then(util::parse)?; diff --git a/lib/domain/libimagcontact/src/error.rs b/lib/domain/libimagcontact/src/error.rs index 20749389..45535bde 100644 --- a/lib/domain/libimagcontact/src/error.rs +++ b/lib/domain/libimagcontact/src/error.rs @@ -34,6 +34,7 @@ error_chain! { foreign_links { Io(::std::io::Error); TomlQueryError(::toml_query::error::Error); + UuidError(::uuid::ParseError); } errors { diff --git a/lib/domain/libimagcontact/src/lib.rs b/lib/domain/libimagcontact/src/lib.rs index 54dae122..31d0ac60 100644 --- a/lib/domain/libimagcontact/src/lib.rs +++ b/lib/domain/libimagcontact/src/lib.rs @@ -38,6 +38,7 @@ extern crate vobject; extern crate toml; extern crate toml_query; +extern crate uuid; #[macro_use] extern crate libimagstore; extern crate libimagerror; diff --git a/lib/domain/libimagcontact/src/store.rs b/lib/domain/libimagcontact/src/store.rs index 9cb8cd0e..a2f622d8 100644 --- a/lib/domain/libimagcontact/src/store.rs +++ b/lib/domain/libimagcontact/src/store.rs @@ -17,22 +17,54 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::path::Path; use std::path::PathBuf; +use std::result::Result as RResult; +use std::ffi::OsStr; use vobject::parse_component; +use uuid::Uuid; use libimagstore::store::Store; use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreIdIterator; use libimagentryref::refstore::RefStore; -use libimagentryref::flags::RefFlags; +use libimagentryref::refstore::UniqueRefPathGenerator; +use libimagentryref::generators::sha1::Sha1; use libimagentryutil::isa::Is; use contact::IsContact; +use error::ContactError as CE; use error::Result; 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>(path: A) -> RResult { + 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 @@ -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 /// collecting metadata. - fn create_from_buf(&'a self, p: &PathBuf, buf: &String) -> Result>; + fn create_from_buf>(&'a self, p: P, buf: &String) -> Result>; // getting @@ -63,12 +95,11 @@ impl<'a> ContactStore<'a> for Store { } /// Create contact ref from buffer - fn create_from_buf(&'a self, p: &PathBuf, buf: &String) -> Result> { + fn create_from_buf>(&'a self, p: P, buf: &String) -> Result> { let component = parse_component(&buf)?; debug!("Parsed: {:?}", component); - let flags = RefFlags::default().with_content_hashing(true).with_permission_tracking(false); - RefStore::create(self, p.clone(), flags) + RefStore::create_ref::(self, p) .map_err(From::from) .and_then(|mut entry| { entry.set_isflag::() @@ -78,7 +109,7 @@ impl<'a> ContactStore<'a> for Store { } fn all_contacts(&'a self) -> Result { - self.all_references().map_err(From::from) + unimplemented!() } } From 672873c2f1963016d95b6d2d4e5fe10cf9f06fe7 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 14 Feb 2018 12:35:42 +0100 Subject: [PATCH 2/3] Make UniqueContactPathGenerator pub --- lib/domain/libimagcontact/src/store.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/domain/libimagcontact/src/store.rs b/lib/domain/libimagcontact/src/store.rs index a2f622d8..fdfc8384 100644 --- a/lib/domain/libimagcontact/src/store.rs +++ b/lib/domain/libimagcontact/src/store.rs @@ -38,7 +38,7 @@ use error::ContactError as CE; use error::Result; use util; -struct UniqueContactPathGenerator; +pub struct UniqueContactPathGenerator; impl UniqueRefPathGenerator for UniqueContactPathGenerator { type Error = CE; From 9ced9008b5cf3e3b33c24f4dc4b0f17b4f47bc0a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 14 Feb 2018 12:38:00 +0100 Subject: [PATCH 3/3] Refactor imag-contact for new libimagentryref API --- bin/domain/imag-contact/Cargo.toml | 2 +- bin/domain/imag-contact/src/create.rs | 8 ++------ bin/domain/imag-contact/src/main.rs | 5 +++-- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/bin/domain/imag-contact/Cargo.toml b/bin/domain/imag-contact/Cargo.toml index 71a4b1c9..b44fc224 100644 --- a/bin/domain/imag-contact/Cargo.toml +++ b/bin/domain/imag-contact/Cargo.toml @@ -28,7 +28,7 @@ toml-query = "0.6" handlebars = "0.29" vobject = "0.4" walkdir = "1" -uuid = { version = "0.5", features = ["v4"] } +uuid = { version = "0.6", features = ["v4"] } libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" } libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" } diff --git a/bin/domain/imag-contact/src/create.rs b/bin/domain/imag-contact/src/create.rs index 51278c28..9b44b703 100644 --- a/bin/domain/imag-contact/src/create.rs +++ b/bin/domain/imag-contact/src/create.rs @@ -31,13 +31,13 @@ use toml::Value; use uuid::Uuid; use libimagcontact::error::ContactError as CE; +use libimagcontact::store::UniqueContactPathGenerator; use libimagrt::runtime::Runtime; use libimagerror::str::ErrFromStr; use libimagerror::trace::MapErrTrace; use libimagerror::trace::trace_error; use libimagutil::warn_result::WarnResult; use libimagentryref::refstore::RefStore; -use libimagentryref::flags::RefFlags; 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 !scmd.is_present("dont-track") { - let flags = RefFlags::default() - .with_content_hashing(true) - .with_permission_tracking(false); - - RefStore::create(rt.store(), location, flags) + RefStore::create_ref::(rt.store(), location) .map_err_trace_exit_unwrap(1); info!("Created entry in store"); diff --git a/bin/domain/imag-contact/src/main.rs b/bin/domain/imag-contact/src/main.rs index c2785901..35ad1e0b 100644 --- a/bin/domain/imag-contact/src/main.rs +++ b/bin/domain/imag-contact/src/main.rs @@ -67,6 +67,7 @@ use libimagerror::trace::MapErrTrace; use libimagerror::io::ToExitCode; use libimagerror::exit::ExitUnwrap; use libimagcontact::store::ContactStore; +use libimagcontact::store::UniqueContactPathGenerator; use libimagcontact::error::ContactError as CE; use libimagcontact::contact::Contact; use libimagstore::iter::get::StoreIdGetIteratorExtension; @@ -130,7 +131,7 @@ fn list(rt: &Runtime) { }) .enumerate() .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| { error!("Element is not a VCARD object: {:?}", e); exit(1) @@ -190,7 +191,7 @@ fn show(rt: &Runtime) { let hash = scmd.value_of("hash").map(String::from).unwrap(); // safed by clap let contact_data = rt.store() - .get_by_hash(hash.clone()) + .get_ref::(hash.clone()) .map_err_trace_exit_unwrap(1) .ok_or(CE::from(format!("No entry for hash {}", hash))) .map_err_trace_exit_unwrap(1)