imag-category: implement ImagApplication

Signed-off-by: Leon Schuermann <leon@is.currently.online>
This commit is contained in:
Leon Schuermann 2019-09-14 18:26:50 +02:00 committed by Matthias Beyer
parent ec75892516
commit 79cfa8d9d8
3 changed files with 93 additions and 24 deletions

View file

@ -36,3 +36,10 @@ version = "2.33.0"
default-features = false default-features = false
features = ["color", "suggestions", "wrap_help"] features = ["color", "suggestions", "wrap_help"]
[lib]
name = "libimagcategorycmd"
path = "src/lib.rs"
[[bin]]
name = "imag-category"
path = "src/bin.rs"

View file

@ -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!(libimagcategorycmd, ImagCategory);

View file

@ -42,15 +42,18 @@ extern crate failure;
extern crate libimagentrycategory; extern crate libimagentrycategory;
extern crate libimagerror; extern crate libimagerror;
#[macro_use] extern crate libimagrt; extern crate libimagrt;
extern crate libimagstore; extern crate libimagstore;
extern crate libimaginteraction; extern crate libimaginteraction;
use failure::Fallible as Result;
use clap::App;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::exit::ExitUnwrap; use libimagerror::exit::ExitUnwrap;
use libimagerror::io::ToExitCode; use libimagerror::io::ToExitCode;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup; use libimagrt::application::ImagApplication;
mod ui; mod ui;
@ -63,13 +66,13 @@ use libimagerror::iter::TraceIterator;
use libimagentrycategory::entry::EntryCategory; use libimagentrycategory::entry::EntryCategory;
use libimagentrycategory::category::Category; use libimagentrycategory::category::Category;
fn main() { /// Marker enum for implementing ImagApplication on
let version = make_imag_version!(); ///
let rt = generate_runtime_setup("imag-category", /// This is used by binaries crates to execute business logic
&version, /// or to build a CLI completion.
"Add a category to entries and manage categories", pub enum ImagCategory {}
ui::build_ui); impl ImagApplication for ImagCategory {
fn run(rt: Runtime) -> Result<()> {
if let Some(name) = rt.cli().subcommand_name() { if let Some(name) = rt.cli().subcommand_name() {
match name { match name {
"set" => set(&rt), "set" => set(&rt),
@ -87,8 +90,28 @@ fn main() {
}, },
} }
} }
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) { fn set(rt: &Runtime) {
let scmd = rt.cli().subcommand_matches("set").unwrap(); // safed by main() 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 let name = scmd.value_of("set-name").map(String::from).unwrap(); // safed by clap