summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-03-24 22:50:34 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-06-01 13:59:37 +0200
commitd6e602948c0a540ec4544a9f1ecbd85c311a212d (patch)
treeb9a8a3a656ee9b7076702a94f4bc4952a76b2a63
parent82aef0550e1cc590623901dd6207e1f277bd743e (diff)
Move import function to own module
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--bin/domain/imag-mail/src/import.rs59
-rw-r--r--bin/domain/imag-mail/src/lib.rs33
2 files changed, 61 insertions, 31 deletions
diff --git a/bin/domain/imag-mail/src/import.rs b/bin/domain/imag-mail/src/import.rs
new file mode 100644
index 00000000..60f0274a
--- /dev/null
+++ b/bin/domain/imag-mail/src/import.rs
@@ -0,0 +1,59 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2020 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 failure::Fallible as Result;
+use failure::err_msg;
+
+use libimagrt::runtime::Runtime;
+use toml_query::read::TomlValueReadExt;
+use libimagmail::store::MailStore;
+use libimagmail::notmuch::connection::NotmuchConnection;
+use libimagentrytag::tag::is_tag_str;
+
+use config::MailConfig;
+
+pub fn import(rt: &Runtime) -> Result<()> {
+ let config = rt.config()
+ .ok_or_else(|| err_msg("Configuration missing"))?
+ .read_partial::<MailConfig>()?
+ .ok_or_else(|| err_msg("Configuration for \"mail\" missing"))?;
+ let scmd = rt.cli().subcommand_matches("import").unwrap();
+ let query = scmd.value_of("query").unwrap();
+ let store = rt.store();
+ let notmuch_path = config.get_notmuch_database_path(rt);
+ let notmuch_connection = NotmuchConnection::open(notmuch_path)?;
+ let import_nm_tags = config.get_import_notmuch_tags();
+
+ if let Some(ref tag) = config.get_import_tag() {
+ let _ = is_tag_str(tag)?;
+ }
+
+ debug!("Importing mail with query = '{}'", query);
+
+ let r = store
+ .with_connection(&notmuch_connection)
+ .import_with_query(query, config.get_import_tag().map(String::clone), import_nm_tags)?
+ .into_iter()
+ .map(|fle| rt.report_touched(fle.get_location()))
+ .collect::<Result<Vec<_>>>()
+ .map(|_| ());
+
+ r
+}
+
diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs
index 4c851548..866dcac9 100644
--- a/bin/domain/imag-mail/src/lib.rs
+++ b/bin/domain/imag-mail/src/lib.rs
@@ -76,9 +76,9 @@ use libimagrt::runtime::Runtime;
use libimagrt::application::ImagApplication;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagstore::store::FileLockEntry;
-use libimagentrytag::tag::is_tag_str;
mod config;
+mod import;
mod ui;
mod util;
@@ -92,7 +92,7 @@ pub enum ImagMail {}
impl ImagApplication for ImagMail {
fn run(rt: Runtime) -> Result<()> {
match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? {
- "import" => import(&rt),
+ "import" => import::import(&rt),
"list" => list(&rt),
"print-id" => print_id(&rt),
other => {
@@ -123,35 +123,6 @@ impl ImagApplication for ImagMail {
}
}
-fn import(rt: &Runtime) -> Result<()> {
- let config = rt.config()
- .ok_or_else(|| err_msg("Configuration missing"))?
- .read_partial::<MailConfig>()?
- .ok_or_else(|| err_msg("Configuration for \"mail\" missing"))?;
- let scmd = rt.cli().subcommand_matches("import").unwrap();
- let query = scmd.value_of("query").unwrap();
- let store = rt.store();
- let notmuch_path = config.get_notmuch_database_path(rt);
- let notmuch_connection = NotmuchConnection::open(notmuch_path)?;
- let import_nm_tags = config.get_import_notmuch_tags();
-
- if let Some(ref tag) = config.get_import_tag() {
- let _ = is_tag_str(tag)?;
- }
-
- debug!("Importing mail with query = '{}'", query);
-
- let r = store
- .with_connection(&notmuch_connection)
- .import_with_query(query, config.get_import_tag().map(String::clone), import_nm_tags)?
- .into_iter()
- .map(|fle| rt.report_touched(fle.get_location()))
- .collect::<Result<Vec<_>>>()
- .map(|_| ());
-
- r
-}
-
fn list(rt: &Runtime) -> Result<()> {
let config = rt.config()
.ok_or_else(|| err_msg("Configuration missing"))?