From 621c5f96f85770223aab1d1184c3d9d6543a3aac Mon Sep 17 00:00:00 2001 From: Leon Schuermann Date: Sat, 14 Sep 2019 18:53:13 +0200 Subject: [PATCH] imag-habit: implement ImagApplication Signed-off-by: Leon Schuermann --- bin/domain/imag-habit/Cargo.toml | 7 ++ bin/domain/imag-habit/src/bin.rs | 39 +++++++++ bin/domain/imag-habit/src/{main.rs => lib.rs} | 82 ++++++++++++------- 3 files changed, 97 insertions(+), 31 deletions(-) create mode 100644 bin/domain/imag-habit/src/bin.rs rename bin/domain/imag-habit/src/{main.rs => lib.rs} (92%) diff --git a/bin/domain/imag-habit/Cargo.toml b/bin/domain/imag-habit/Cargo.toml index c4e6b80b..a796c2e6 100644 --- a/bin/domain/imag-habit/Cargo.toml +++ b/bin/domain/imag-habit/Cargo.toml @@ -42,3 +42,10 @@ version = "2.33.0" default-features = false features = ["color", "suggestions", "wrap_help"] +[lib] +name = "libimaghabitfrontend" +path = "src/lib.rs" + +[[bin]] +name = "imag-habit" +path = "src/bin.rs" diff --git a/bin/domain/imag-habit/src/bin.rs b/bin/domain/imag-habit/src/bin.rs new file mode 100644 index 00000000..af996417 --- /dev/null +++ b/bin/domain/imag-habit/src/bin.rs @@ -0,0 +1,39 @@ +// +// 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 +// + +#![forbid(unsafe_code)] + +#![deny( + non_camel_case_types, + non_snake_case, + path_statements, + trivial_numeric_casts, + unstable_features, + unused_allocation, + unused_import_braces, + unused_imports, + unused_must_use, + unused_mut, + unused_qualifications, + while_true, +)] + +#[macro_use] extern crate libimagrt; + +simple_imag_application_binary!(libimaghabitfrontend, ImagHabit); diff --git a/bin/domain/imag-habit/src/main.rs b/bin/domain/imag-habit/src/lib.rs similarity index 92% rename from bin/domain/imag-habit/src/main.rs rename to bin/domain/imag-habit/src/lib.rs index 34c44959..86fa3b3d 100644 --- a/bin/domain/imag-habit/src/main.rs +++ b/bin/domain/imag-habit/src/lib.rs @@ -45,7 +45,7 @@ extern crate prettytable; extern crate libimaghabit; extern crate libimagstore; -#[macro_use] extern crate libimagrt; +extern crate libimagrt; extern crate libimagerror; extern crate libimagutil; extern crate libimaginteraction; @@ -57,9 +57,11 @@ use prettytable::Table; use prettytable::Cell; use prettytable::Row; use failure::Error; +use failure::Fallible as Result; +use clap::App; use libimagrt::runtime::Runtime; -use libimagrt::setup::generate_runtime_setup; +use libimagrt::application::ImagApplication; use libimagerror::trace::{MapErrTrace, trace_error}; use libimagerror::iter::TraceIterator; use libimagerror::exit::ExitUnwrap; @@ -75,37 +77,55 @@ use libimagutil::debug_result::DebugResult; mod ui; -fn main() { - let version = make_imag_version!(); - let rt = generate_runtime_setup("imag-habit", - &version, - "Habit tracking tool", - ui::build_ui); +/// Marker enum for implementing ImagApplication on +/// +/// This is used by binaries crates to execute business logic +/// or to build a CLI completion. +pub enum ImagHabit {} +impl ImagApplication for ImagHabit { + fn run(rt: Runtime) -> Result<()> { + rt + .cli() + .subcommand_name() + .map(|name| { + debug!("Call {}", name); + match name { + "create" => create(&rt), + "delete" => delete(&rt), + "list" => list(&rt), + "today" => today(&rt, false), + "status" => today(&rt, true), + "show" => show(&rt), + "done" => done(&rt), + other => { + debug!("Unknown command"); + let _ = rt.handle_unknown_subcommand("imag-habit", other, rt.cli()) + .map_err_trace_exit_unwrap() + .code() + .map(::std::process::exit); + }, + } + }) + .unwrap_or_else(|| today(&rt, true)); + Ok(()) + } - rt - .cli() - .subcommand_name() - .map(|name| { - debug!("Call {}", name); - match name { - "create" => create(&rt), - "delete" => delete(&rt), - "list" => list(&rt), - "today" => today(&rt, false), - "status" => today(&rt, true), - "show" => show(&rt), - "done" => done(&rt), - other => { - debug!("Unknown command"); - let _ = rt.handle_unknown_subcommand("imag-habit", other, rt.cli()) - .map_err_trace_exit_unwrap() - .code() - .map(::std::process::exit); - }, - } - }) - .unwrap_or_else(|| today(&rt, true)); + fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> { + ui::build_ui(app) + } + + fn name() -> &'static str { + env!("CARGO_PKG_NAME") + } + + fn description() -> &'static str { + "Habit tracking tool" + } + + fn version() -> &'static str { + env!("CARGO_PKG_VERSION") + } } fn create(rt: &Runtime) {