use std::path::PathBuf;
use std::path::Path;
use toml::Value;
use toml::to_string as toml_to_string;
use toml::from_str as toml_from_str;
use toml_query::insert::TomlValueInsertExt;
use vobject::vcard::Vcard;
use failure::Error;
use failure::Fallible as Result;
use failure::ResultExt;
use libimagstore::storeid::StoreId;
use libimagstore::iter::Entries;
use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagentryutil::isa::Is;
use libimagentryref::reference::{MutRef, Config as RefConfig};
use crate::contact::IsContact;
use crate::deser::DeserVcard;
use crate::util;
pub trait ContactStore<'a> {
fn create_from_path<CN>(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>;
fn retrieve_from_path<CN>(&'a self,
p: &PathBuf,
rc: &RefConfig,
collection_name: CN,
force: bool)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>;
fn create_from_buf<CN, P>(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>,
P: AsRef<Path>;
fn retrieve_from_buf<CN, P>(&'a self,
buf: &str,
path: P,
rc: &RefConfig,
collection_name: CN,
force: bool)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>,
P: AsRef<Path>;
fn all_contacts(&'a self) -> Result<Entries<'a>>;
}
impl<'a> ContactStore<'a> for Store {
fn create_from_path<CN>(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>
{
util::read_to_string(p).and_then(|buf| self.create_from_buf(&buf, p, rc, collection_name))
}
fn retrieve_from_path<CN>(&'a self, p: &PathBuf, rc: &RefConfig, collection_name: CN, force: bool)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>
{
util::read_to_string(p)
.and_then(|buf| self.retrieve_from_buf(&buf, p, rc, collection_name, force))
}
fn create_from_buf<CN, P>(&'a self, buf: &str, path: P, rc: &RefConfig, collection_name: CN)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>,
P: AsRef<Path>
{
let (sid, value) = prepare_fetching_from_store(buf)?;
postprocess_fetched_entry(self.create(sid)?, value, path, rc, collection_name, false)
}
fn retrieve_from_buf<CN, P>(&'a self,
buf: &str,
path: P,
rc: &RefConfig,
collection_name: CN,
force: bool)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>,
P: AsRef<Path>
{
let (sid, value) = prepare_fetching_from_store(buf)?;
postprocess_fetched_entry(self.retrieve(sid)?, value, path, rc, collection_name, force)
}
fn all_contacts(&'a self) -> Result<Entries<'a>> {
self.entries()?.in_collection("contact")
}
}
fn prepare_fetching_from_store(buf: &str) -> Result<(StoreId, Value)> {
let vcard = Vcard::build(&buf).context("Cannot parse Vcard").map_err(Error::from)?;
debug!("Parsed: {:?}", vcard);
let uid = vcard.uid()
.ok_or_else(|| format_err!("UID Missing: {}", buf.to_string()))?;
let value = { let serialized = DeserVcard::from(vcard);
let serialized = toml_to_string(&serialized)?;
toml_from_str::<Value>(&serialized)?
};
let sid = crate::module_path::new_id(uid.raw())?;
Ok((sid, value))
}
#[inline]
fn postprocess_fetched_entry<'a, CN, P>(mut entry: FileLockEntry<'a>,
value: Value,
path: P,
rc: &RefConfig,
collection_name: CN,
force: bool)
-> Result<FileLockEntry<'a>>
where CN: AsRef<str>,
P: AsRef<Path>
{
use libimagentryref::reference::RefFassade;
use libimagentryref::hasher::sha1::Sha1Hasher;
entry.set_isflag::<IsContact>()?;
entry.get_header_mut().insert("contact.data", value)?;
entry.as_ref_with_hasher_mut::<Sha1Hasher>().make_ref(path, collection_name, rc, force)?;
Ok(entry)
}