libimagcreatecmd/
lib.rs

1//
2// imag - the personal information management suite for the commandline
3// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; version
8// 2.1 of the License.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18//
19
20#![forbid(unsafe_code)]
21
22#![deny(
23    non_camel_case_types,
24    non_snake_case,
25    path_statements,
26    trivial_numeric_casts,
27    unstable_features,
28    unused_allocation,
29    unused_import_braces,
30    unused_imports,
31    unused_must_use,
32    unused_mut,
33    unused_qualifications,
34    while_true,
35)]
36
37extern crate clap;
38extern crate failure;
39
40extern crate libimagerror;
41extern crate libimagrt;
42extern crate libimagstore;
43
44use failure::Fallible as Result;
45use failure::err_msg;
46use clap::App;
47
48use libimagrt::runtime::Runtime;
49use libimagrt::application::ImagApplication;
50use libimagrt::iter::ReportTouchedResultEntry;
51use libimagstore::iter::create::StoreIdCreateIteratorExtension;
52use libimagstore::iter::retrieve::StoreIdRetrieveIteratorExtension;
53
54mod ui;
55
56
57
58pub enum ImagCreate {}
59impl ImagApplication for ImagCreate {
60    fn run(rt: Runtime) -> Result<()> {
61        let force = rt.cli().is_present("force");
62
63        let ids = rt.ids::<crate::ui::PathProvider>()?
64            .ok_or_else(|| err_msg("No ids supplied"))?
65            .into_iter()
66            .map(Ok);
67
68        if force {
69            ids.into_retrieve_iter(rt.store()).map_report_touched(&rt).collect::<Result<Vec<_>>>()
70        } else {
71            ids.into_create_iter(rt.store()).map_report_touched(&rt).collect::<Result<Vec<_>>>()
72        }.map(|_| ())
73    }
74
75    fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
76        ui::build_ui(app)
77    }
78
79    fn name() -> &'static str {
80        env!("CARGO_PKG_NAME")
81    }
82
83    fn description() -> &'static str {
84        "Plumbing tool to create entries"
85    }
86
87    fn version() -> &'static str {
88        env!("CARGO_PKG_VERSION")
89    }
90}
91