summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeon Schuermann <leon@is.currently.online>2019-09-14 18:53:13 +0200
committerMatthias Beyer <mail@beyermatthias.de>2019-10-26 14:41:31 +0200
commit621c5f96f85770223aab1d1184c3d9d6543a3aac (patch)
tree83f52f8104d695170907e0eb6503cc6b14892d1a
parent3e12bd99d4205a00e6ab33533efd07787143d381 (diff)
imag-habit: implement ImagApplication
Signed-off-by: Leon Schuermann <leon@is.currently.online>
-rw-r--r--bin/domain/imag-habit/Cargo.toml7
-rw-r--r--bin/domain/imag-habit/src/bin.rs39
-rw-r--r--bin/domain/imag-habit/src/lib.rs (renamed from bin/domain/imag-habit/src/main.rs)82
3 files changed, 97 insertions, 31 deletions
diff --git a/bin/domain/imag-habit/Cargo.toml b/bin/domain/imag-habit/Cargo.toml
index c4e6b80..a796c2e 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 0000000..af99641
--- /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 <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
+//
+
+#![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
index 34c4495..86fa3b3 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) {