From dfc02a56799504e268b02c089d9070ec1d038f9b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 20 Feb 2019 13:07:59 +0100 Subject: [PATCH] Move helper function to libimagentref::util This moves the helper function for getting the `Config` object from the configuration file via the runtime. Signed-off-by: Matthias Beyer --- bin/core/imag-ref/Cargo.toml | 5 ---- bin/core/imag-ref/src/main.rs | 19 ++------------ bin/domain/imag-mail/src/main.rs | 15 +++-------- lib/entry/libimagentryref/Cargo.toml | 7 ++++- lib/entry/libimagentryref/src/lib.rs | 2 ++ lib/entry/libimagentryref/src/util.rs | 37 +++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 35 deletions(-) create mode 100644 lib/entry/libimagentryref/src/util.rs diff --git a/bin/core/imag-ref/Cargo.toml b/bin/core/imag-ref/Cargo.toml index bf3cb6c0..0c4b5987 100644 --- a/bin/core/imag-ref/Cargo.toml +++ b/bin/core/imag-ref/Cargo.toml @@ -37,8 +37,3 @@ version = "^2.29" default-features = false features = ["color", "suggestions", "wrap_help"] -[dependencies.toml-query] -version = "0.8" -default-features = false -features = ["typed"] - diff --git a/bin/core/imag-ref/src/main.rs b/bin/core/imag-ref/src/main.rs index 63edf367..a9500beb 100644 --- a/bin/core/imag-ref/src/main.rs +++ b/bin/core/imag-ref/src/main.rs @@ -35,9 +35,7 @@ )] #[macro_use] extern crate log; -#[macro_use] extern crate failure; extern crate clap; -extern crate toml_query; extern crate libimagstore; #[macro_use] extern crate libimagrt; @@ -52,8 +50,6 @@ use ui::build_ui; use std::process::exit; use std::io::Write; -use failure::Fallible as Result; - use libimagerror::trace::MapErrTrace; use libimagerror::exit::ExitUnwrap; use libimagrt::setup::generate_runtime_setup; @@ -62,7 +58,7 @@ use libimagentryref::reference::Ref; use libimagentryref::reference::MutRef; use libimagentryref::reference::RefFassade; use libimagentryref::hasher::default::DefaultHasher; -use libimagentryref::reference::Config as RefConfig; +use libimagentryref::util::get_ref_config; fn main() { let version = make_imag_version!(); @@ -92,7 +88,7 @@ fn main() { fn deref(rt: &Runtime) { let cmd = rt.cli().subcommand_matches("deref").unwrap(); let ids = rt.ids::<::ui::PathProvider>().map_err_trace_exit_unwrap(); - let cfg = get_ref_config(&rt).map_err_trace_exit_unwrap(); + let cfg = get_ref_config(&rt, "imag-ref").map_err_trace_exit_unwrap(); let out = rt.stdout(); let mut outlock = out.lock(); @@ -163,14 +159,3 @@ fn create(rt: &Runtime) { unimplemented!() } -fn get_ref_config(rt: &Runtime) -> Result { - use toml_query::read::TomlValueReadExt; - - let setting_name = "ref.basepathes"; - - rt.config() - .ok_or_else(|| format_err!("No configuration, cannot find collection name for ref collection"))? - .read_deserialized::(setting_name)? - .ok_or_else(|| format_err!("Setting missing: {}", setting_name)) -} - diff --git a/bin/domain/imag-mail/src/main.rs b/bin/domain/imag-mail/src/main.rs index 59d7f649..8f7418df 100644 --- a/bin/domain/imag-mail/src/main.rs +++ b/bin/domain/imag-mail/src/main.rs @@ -51,7 +51,6 @@ use std::io::Write; use std::path::PathBuf; use failure::Fallible as Result; -use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadTypeExt; use libimagerror::trace::{MapErrTrace, trace_error}; @@ -62,6 +61,7 @@ use libimagmail::mail::Mail; use libimagmail::store::MailStore; use libimagmail::util; use libimagentryref::reference::{Ref, RefFassade}; +use libimagentryref::util::get_ref_config; use libimagrt::runtime::Runtime; use libimagrt::setup::generate_runtime_setup; use libimagutil::info_result::*; @@ -101,7 +101,7 @@ fn main() { fn import_mail(rt: &Runtime) { let collection_name = get_ref_collection_name(rt).map_err_trace_exit_unwrap(); - let refconfig = get_ref_config(rt).map_err_trace_exit_unwrap(); + let refconfig = get_ref_config(rt, "imag-mail").map_err_trace_exit_unwrap(); let scmd = rt.cli().subcommand_matches("import-mail").unwrap(); let store = rt.store(); @@ -126,7 +126,7 @@ fn import_mail(rt: &Runtime) { } fn list(rt: &Runtime) { - let refconfig = get_ref_config(rt).map_err_trace_exit_unwrap(); + let refconfig = get_ref_config(rt, "imag-mail").map_err_trace_exit_unwrap(); let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe via clap let print_content = scmd.is_present("list-read"); @@ -251,12 +251,3 @@ fn get_ref_collection_name(rt: &Runtime) -> Result { .ok_or_else(|| format_err!("Setting missing: {}", setting_name)) } -fn get_ref_config(rt: &Runtime) -> Result<::libimagentryref::reference::Config> { - let setting_name = "ref.basepathes"; - - rt.config() - .ok_or_else(|| format_err!("No configuration, cannot find collection name for mail collection"))? - .read_deserialized::<::libimagentryref::reference::Config>(setting_name)? - .ok_or_else(|| format_err!("Setting missing: {}", setting_name)) -} - diff --git a/lib/entry/libimagentryref/Cargo.toml b/lib/entry/libimagentryref/Cargo.toml index 60692acd..ccec9afa 100644 --- a/lib/entry/libimagentryref/Cargo.toml +++ b/lib/entry/libimagentryref/Cargo.toml @@ -25,14 +25,19 @@ log = "0.4.0" failure = "0.1" sha-1 = "0.8" toml = "0.4" -toml-query = "0.8" serde = "1" serde_derive = "1" libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror" } +libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" } libimagentryutil = { version = "0.10.0", path = "../../../lib/entry/libimagentryutil" } +[dependencies.toml-query] +version = "0.8" +default-features = false +features = ["typed"] + [dev-dependencies] env_logger = "0.5" diff --git a/lib/entry/libimagentryref/src/lib.rs b/lib/entry/libimagentryref/src/lib.rs index 4d8e02e8..95c40b20 100644 --- a/lib/entry/libimagentryref/src/lib.rs +++ b/lib/entry/libimagentryref/src/lib.rs @@ -45,6 +45,7 @@ extern crate toml_query; extern crate sha1; extern crate libimagstore; +extern crate libimagrt; extern crate libimagerror; #[macro_use] extern crate libimagentryutil; #[macro_use] extern crate failure; @@ -54,4 +55,5 @@ extern crate env_logger; pub mod hasher; pub mod reference; +pub mod util; diff --git a/lib/entry/libimagentryref/src/util.rs b/lib/entry/libimagentryref/src/util.rs new file mode 100644 index 00000000..1daf2e75 --- /dev/null +++ b/lib/entry/libimagentryref/src/util.rs @@ -0,0 +1,37 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015-2019 Matthias Beyer 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 failure::Fallible as Result; + +use libimagrt::runtime::Runtime; + +use reference::Config as RefConfig; + +pub fn get_ref_config(rt: &Runtime, app_name: &'static str) -> Result { + use toml_query::read::TomlValueReadExt; + + let setting_name = "ref.basepathes"; + + rt.config() + .ok_or_else(|| format_err!("No configuration, cannot find collection name for {}", app_name))? + .read_deserialized::(setting_name)? + .ok_or_else(|| format_err!("Setting missing: {}", setting_name)) +} + +