diff --git a/bin/core/imag-category/Cargo.toml b/bin/core/imag-category/Cargo.toml index 6294a3c6..afc09fbb 100644 --- a/bin/core/imag-category/Cargo.toml +++ b/bin/core/imag-category/Cargo.toml @@ -36,3 +36,10 @@ version = "2.33.0" default-features = false features = ["color", "suggestions", "wrap_help"] +[lib] +name = "libimagcategorycmd" +path = "src/lib.rs" + +[[bin]] +name = "imag-category" +path = "src/bin.rs" diff --git a/bin/core/imag-category/src/bin.rs b/bin/core/imag-category/src/bin.rs new file mode 100644 index 00000000..e3120cae --- /dev/null +++ b/bin/core/imag-category/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!(libimagcategorycmd, ImagCategory); diff --git a/bin/core/imag-category/src/main.rs b/bin/core/imag-category/src/lib.rs similarity index 79% rename from bin/core/imag-category/src/main.rs rename to bin/core/imag-category/src/lib.rs index 44a87c12..a40bd632 100644 --- a/bin/core/imag-category/src/main.rs +++ b/bin/core/imag-category/src/lib.rs @@ -42,15 +42,18 @@ extern crate failure; extern crate libimagentrycategory; extern crate libimagerror; -#[macro_use] extern crate libimagrt; +extern crate libimagrt; extern crate libimagstore; extern crate libimaginteraction; +use failure::Fallible as Result; +use clap::App; + use libimagerror::trace::MapErrTrace; use libimagerror::exit::ExitUnwrap; use libimagerror::io::ToExitCode; use libimagrt::runtime::Runtime; -use libimagrt::setup::generate_runtime_setup; +use libimagrt::application::ImagApplication; mod ui; @@ -63,32 +66,52 @@ use libimagerror::iter::TraceIterator; use libimagentrycategory::entry::EntryCategory; use libimagentrycategory::category::Category; -fn main() { - let version = make_imag_version!(); - let rt = generate_runtime_setup("imag-category", - &version, - "Add a category to entries and manage categories", - ui::build_ui); - - if let Some(name) = rt.cli().subcommand_name() { - match name { - "set" => set(&rt), - "get" => get(&rt), - "list-category" => list_category(&rt), - "create-category" => create_category(&rt), - "delete-category" => delete_category(&rt), - "list-categories" => list_categories(&rt), - other => { - debug!("Unknown command"); - let _ = rt.handle_unknown_subcommand("imag-category", other, rt.cli()) - .map_err_trace_exit_unwrap() - .code() - .map(::std::process::exit); - }, +/// Marker enum for implementing ImagApplication on +/// +/// This is used by binaries crates to execute business logic +/// or to build a CLI completion. +pub enum ImagCategory {} +impl ImagApplication for ImagCategory { + fn run(rt: Runtime) -> Result<()> { + if let Some(name) = rt.cli().subcommand_name() { + match name { + "set" => set(&rt), + "get" => get(&rt), + "list-category" => list_category(&rt), + "create-category" => create_category(&rt), + "delete-category" => delete_category(&rt), + "list-categories" => list_categories(&rt), + other => { + debug!("Unknown command"); + let _ = rt.handle_unknown_subcommand("imag-category", other, rt.cli()) + .map_err_trace_exit_unwrap() + .code() + .map(::std::process::exit); + }, + } } + + Ok(()) + } + + 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 { + "Add a category to entries and manage categories" + } + + fn version() -> &'static str { + env!("CARGO_PKG_VERSION") } } + fn set(rt: &Runtime) { let scmd = rt.cli().subcommand_matches("set").unwrap(); // safed by main() let name = scmd.value_of("set-name").map(String::from).unwrap(); // safed by clap