Merge branch 'libimagcontact/rewrite-libimagref-api-usage' into libimagentryref/refactor
This commit is contained in:
commit
20654d66f7
8 changed files with 54 additions and 18 deletions
|
@ -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" }
|
||||
|
|
|
@ -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::<UniqueContactPathGenerator, _>(rt.store(), location)
|
||||
.map_err_trace_exit_unwrap(1);
|
||||
|
||||
info!("Created entry in store");
|
||||
|
|
|
@ -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::<UniqueContactPathGenerator, _>(hash.clone())
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
.ok_or(CE::from(format!("No entry for hash {}", hash)))
|
||||
.map_err_trace_exit_unwrap(1)
|
||||
|
|
|
@ -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"]
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ impl Contact for Entry {
|
|||
|
||||
fn get_contact_data(&self) -> Result<ContactData> {
|
||||
let component = self
|
||||
.fs_file()
|
||||
.get_path()
|
||||
.map_err(From::from)
|
||||
.and_then(util::read_to_string)
|
||||
.and_then(util::parse)?;
|
||||
|
|
|
@ -34,6 +34,7 @@ error_chain! {
|
|||
foreign_links {
|
||||
Io(::std::io::Error);
|
||||
TomlQueryError(::toml_query::error::Error);
|
||||
UuidError(::uuid::ParseError);
|
||||
}
|
||||
|
||||
errors {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 {
|
||||
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
|
||||
|
||||
|
@ -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<FileLockEntry<'a>>;
|
||||
fn create_from_buf<P: AsRef<Path>>(&'a self, p: P, buf: &String) -> Result<FileLockEntry<'a>>;
|
||||
|
||||
// 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<FileLockEntry<'a>> {
|
||||
fn create_from_buf<P: AsRef<Path>>(&'a self, p: P, buf: &String) -> Result<FileLockEntry<'a>> {
|
||||
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::<UniqueContactPathGenerator, P>(self, p)
|
||||
.map_err(From::from)
|
||||
.and_then(|mut entry| {
|
||||
entry.set_isflag::<IsContact>()
|
||||
|
@ -78,7 +109,7 @@ impl<'a> ContactStore<'a> for Store {
|
|||
}
|
||||
|
||||
fn all_contacts(&'a self) -> Result<StoreIdIterator> {
|
||||
self.all_references().map_err(From::from)
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue