libimagentryview/builtin/editor.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
20use std::io::Write;
21
22use libimagstore::store::Entry;
23use libimagrt::runtime::Runtime;
24use libimagentryedit::edit::edit_in_tmpfile;
25
26use crate::viewer::Viewer;
27use crate::error::Result;
28use crate::error::Error;
29use failure::ResultExt;
30
31pub struct EditorView<'a>(&'a Runtime<'a>);
32
33impl<'a> EditorView<'a> {
34 pub fn new(rt: &'a Runtime) -> EditorView<'a> {
35 EditorView(rt)
36 }
37}
38
39impl<'a> Viewer for EditorView<'a> {
40 fn view_entry<W>(&self, e: &Entry, _sink: &mut W) -> Result<()>
41 where W: Write
42 {
43 let mut entry = e.to_str()?;
44 edit_in_tmpfile(self.0, &mut entry)
45 .context("Error while viewing")
46 .map_err(Error::from)
47 }
48}
49