libimagdiaryfrontend/
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
37#[macro_use] extern crate log;
38#[macro_use] extern crate failure;
39extern crate resiter;
40extern crate clap;
41extern crate chrono;
42extern crate toml;
43extern crate toml_query;
44extern crate itertools;
45extern crate option_inspect;
46
47extern crate libimagdiary;
48extern crate libimagentryedit;
49extern crate libimagentryview;
50extern crate libimagerror;
51extern crate libimaginteraction;
52extern crate libimagrt;
53extern crate libimagstore;
54extern crate libimagtimeui;
55extern crate libimagutil;
56
57use std::io::Write;
58
59use libimagrt::runtime::Runtime;
60use libimagrt::application::ImagApplication;
61
62use itertools::Itertools;
63use clap::App;
64use failure::Fallible as Result;
65use failure::err_msg;
66use failure::Error;
67
68mod create;
69mod delete;
70mod list;
71mod ui;
72mod util;
73mod view;
74
75use crate::create::create;
76use crate::delete::delete;
77use crate::list::list;
78use crate::view::view;
79
80/// Marker enum for implementing ImagApplication on
81///
82/// This is used by binaries crates to execute business logic
83/// or to build a CLI completion.
84pub enum ImagDiary {}
85impl ImagApplication for ImagDiary {
86    fn run(rt: Runtime) -> Result<()> {
87        match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? {
88            "diaries" => diaries(&rt),
89            "create"  => create(&rt),
90            "delete"  => delete(&rt),
91            "list"    => list(&rt),
92            "view"    => view(&rt),
93            other     => {
94                debug!("Unknown command");
95                if rt.handle_unknown_subcommand("imag-diary", other, rt.cli())?.success() {
96                    Ok(())
97                } else {
98                    Err(err_msg("Failed to handle unknown subcommand"))
99                }
100            },
101        }
102    }
103
104    fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
105        ui::build_ui(app)
106    }
107
108    fn name() -> &'static str {
109        env!("CARGO_PKG_NAME")
110    }
111
112    fn description() -> &'static str {
113        "Personal Diary/Diaries"
114    }
115
116    fn version() -> &'static str {
117        env!("CARGO_PKG_VERSION")
118    }
119}
120
121fn diaries(rt: &Runtime) -> Result<()> {
122    use libimagdiary::diary::Diary;
123
124    let out         = rt.stdout();
125    let mut outlock = out.lock();
126
127    rt.store()
128        .diary_names()?
129        .collect::<Result<Vec<String>>>()?
130        .into_iter()
131        .unique()
132        .map(|n| writeln!(outlock, "{}", n).map_err(Error::from))
133        .collect()
134}
135