~matthiasbeyer/imag

2a107477c0c5ce12177428ca5c94518550dc03fd — Matthias Beyer 4 years ago 2dc17f8
libimagcontact: Move from error-chain to failure

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
M lib/domain/libimagcontact/Cargo.toml => lib/domain/libimagcontact/Cargo.toml +3 -3
@@ 20,11 20,11 @@ is-it-maintained-open-issues      = { repository = "matthiasbeyer/imag" }
maintenance                       = { status     = "actively-developed" }

[dependencies]
error-chain  = "0.12"
failure      = "0.1"
log          = "0.4"
toml         = "0.4"
toml-query   = "0.7"
vobject      = { git = "https://github.com/matthiasbeyer/rust-vobject", branch = "update-errorchain" }
toml-query   = { git = "https://github.com/matthiasbeyer/toml-query", branch = "failure" }
vobject      = { git = "https://github.com/matthiasbeyer/rust-vobject", branch = "master" }
uuid         = "0.7"
serde        = "1"
serde_derive = "1"

M lib/domain/libimagcontact/src/contact.rs => lib/domain/libimagcontact/src/contact.rs +5 -5
@@ 20,15 20,15 @@
use toml::to_string as toml_to_string;
use toml::from_str as toml_from_str;
use toml_query::read::TomlValueReadExt;
use failure::Fallible as Result;
use failure::Error;

use libimagstore::store::Entry;
use libimagentryutil::isa::Is;
use libimagentryutil::isa::IsKindHeaderPathProvider;
use libimagerror::errors::ErrorMsg as EM;

use deser::DeserVcard;
use error::Result;
use error::ContactError as CE;
use error::ContactErrorKind as CEK;

/// Trait to be implemented on ::libimagstore::store::Entry
pub trait Contact {


@@ 48,14 48,14 @@ provide_kindflag_path!(pub IsContact, "contact.is_contact");
impl Contact for Entry {

    fn is_contact(&self) -> Result<bool> {
        self.is::<IsContact>().map_err(From::from)
        self.is::<IsContact>()
    }

    fn deser(&self) -> Result<DeserVcard> {
        let data = self
            .get_header()
            .read("contact.data")?
            .ok_or_else(|| CE::from_kind(CEK::HeaderDataMissing("contact.data")))?;
            .ok_or_else(|| Error::from(EM::EntryHeaderFieldMissing("contact.data")))?;

        // ugly hack
        let data_str            = toml_to_string(&data)?;

D lib/domain/libimagcontact/src/error.rs => lib/domain/libimagcontact/src/error.rs +0 -65
@@ 1,65 0,0 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use libimagstore::storeid::StoreId;

error_chain! {
    types {
        ContactError, ContactErrorKind, ResultExt, Result;
    }

    links {
        StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
        VObjectError(::vobject::error::VObjectError, ::vobject::error::VObjectErrorKind);
        EntryUtilError(::libimagentryutil::error::EntryUtilError, ::libimagentryutil::error::EntryUtilErrorKind);
    }

    foreign_links {
        Io(::std::io::Error);
        TomlDe(::toml::de::Error);
        TomlSer(::toml::ser::Error);
        TomlQueryError(::toml_query::error::Error);
        UuidError(::uuid::parser::ParseError);
    }

    errors {

        HeaderTypeError(ty: &'static str, loc: &'static str) {
            description("Type error in header")
            display("Type error in header, expected {} at '{}', found other type", ty, loc)
        }

        HeaderDataMissing(datapath: &'static str) {
            description("Data missing in header")
            display("Data missing in header at '{}'", datapath)
        }

        EntryNotFound(sid: StoreId) {
            description("Entry not found with StoreId")
            display("Entry {:?} not found", sid)
        }

        UidMissing(buf: String) {
            description("Vcard object has no UID")
            display("Vcard has no UID : {}", buf)
        }

    }
}


M lib/domain/libimagcontact/src/iter.rs => lib/domain/libimagcontact/src/iter.rs +7 -6
@@ 20,11 20,11 @@
use libimagstore::storeid::StoreIdIterator;
use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagerror::errors::ErrorMsg as EM;

use contact::Contact;
use error::ContactError as CE;
use error::ContactErrorKind as CEK;
use error::Result;
use failure::Fallible as Result;
use failure::Error;

pub struct ContactIter<'a>(StoreIdIterator, &'a Store);



@@ 44,11 44,12 @@ impl<'a> Iterator for ContactIter<'a> {
        loop {
            match self.0.next() {
                None          => return None,
                Some(Err(e))  => return Some(Err(e).map_err(CE::from)),
                Some(Err(e))  => return Some(Err(e).map_err(Error::from)),
                Some(Ok(sid)) => match self.1.get(sid.clone()).map_err(From::from) {
                    Err(e)          => return Some(Err(e)),
                    Ok(None)        => return Some(Err(CE::from_kind(CEK::EntryNotFound(sid)))),
                    Ok(Some(entry)) => match entry.is_contact().map_err(From::from) {
                    Ok(None)        => return
                        Some(Err(Error::from(EM::EntryNotFound(sid.local_display_string())))),
                    Ok(Some(entry)) => match entry.is_contact().map_err(Error::from) {
                        Ok(true)    => return Some(Ok(entry)),
                        Ok(false)   => continue,
                        Err(e)      => return Some(Err(e)),

M lib/domain/libimagcontact/src/lib.rs => lib/domain/libimagcontact/src/lib.rs +1 -2
@@ 36,7 36,7 @@
#![recursion_limit="128"]

#[macro_use] extern crate log;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate failure;
extern crate vobject;
extern crate toml;
extern crate toml_query;


@@ 51,7 51,6 @@ extern crate libimagerror;
module_entry_path_mod!("contact");

pub mod contact;
pub mod error;
pub mod iter;
pub mod store;
pub mod deser;

M lib/domain/libimagcontact/src/store.rs => lib/domain/libimagcontact/src/store.rs +5 -5
@@ 24,6 24,8 @@ 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 libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreId;


@@ 35,9 37,6 @@ use libimagentryutil::isa::Is;
use contact::IsContact;
use deser::DeserVcard;
use module_path::ModuleEntryPath;
use error::ContactError as CE;
use error::ContactErrorKind as CEK;
use error::Result;
use util;

pub trait ContactStore<'a> {


@@ 95,10 94,11 @@ impl<'a> ContactStore<'a> for Store {
///
/// That means calculating the StoreId and the Value from the vcard data
fn prepare_fetching_from_store(buf: &str) -> Result<(StoreId, Value)> {
    let vcard = Vcard::build(&buf)?;
    let vcard = Vcard::build(&buf).map_err(Error::from)?;
    debug!("Parsed: {:?}", vcard);

    let uid = vcard.uid().ok_or_else(|| CE::from_kind(CEK::UidMissing(buf.to_string())))?;
    let uid = vcard.uid()
        .ok_or_else(|| Error::from(format_err!("UID Missing: {}", buf.to_string())))?;

    let value = { // dirty ugly hack
        let serialized = DeserVcard::from(vcard);

M lib/domain/libimagcontact/src/util.rs => lib/domain/libimagcontact/src/util.rs +1 -1
@@ 22,7 22,7 @@ use std::fmt::Debug;
use std::fs::File;
use std::io::Read;

use error::Result;
use failure::Fallible as Result;

pub fn read_to_string<A: AsRef<Path> + Debug>(pb: A) -> Result<String> {
    let mut cont = String::new();