Rewrite libimagentryview interface

In the previous versions, the sink (where the entries should be written
to) was not passed.

This did conflict with the libimagrt holding the stdout/stderr handles,
because it automatically writes to stdout (which we don't want to do in
some cases).

Passing the sink is way nicer. This patch changes libimagentryview so
that the sink is passed to the viewer.
This commit is contained in:
Matthias Beyer 2018-04-18 14:47:06 +02:00
parent 726e4c515f
commit 780dd90c8f
5 changed files with 38 additions and 13 deletions

View File

@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagentryedit::edit::edit_in_tmpfile; use libimagentryedit::edit::edit_in_tmpfile;
@ -35,7 +37,9 @@ impl<'a> EditorView<'a> {
} }
impl<'a> Viewer for EditorView<'a> { impl<'a> Viewer for EditorView<'a> {
fn view_entry(&self, e: &Entry) -> Result<()> { fn view_entry<W>(&self, e: &Entry, _sink: &mut W) -> Result<()>
where W: Write
{
let mut entry = e.to_str()?.clone().to_string(); let mut entry = e.to_str()?.clone().to_string();
edit_in_tmpfile(self.0, &mut entry).chain_err(|| VEK::ViewError) edit_in_tmpfile(self.0, &mut entry).chain_err(|| VEK::ViewError)
} }

View File

@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use viewer::Viewer; use viewer::Viewer;
@ -38,11 +40,13 @@ impl PlainViewer {
impl Viewer for PlainViewer { impl Viewer for PlainViewer {
fn view_entry(&self, e: &Entry) -> Result<()> { fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> Result<()>
where W: Write
{
if self.show_header { if self.show_header {
println!("{}", e.get_header()); let _ = writeln!(sink, "{}", e.get_header())?;
} }
println!("{}", e.get_content()); let _ = writeln!(sink, "{}", e.get_content())?;
Ok(()) Ok(())
} }

View File

@ -17,6 +17,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use toml::ser::to_string; use toml::ser::to_string;
@ -48,17 +50,20 @@ impl StdoutViewer {
impl Viewer for StdoutViewer { impl Viewer for StdoutViewer {
fn view_entry(&self, e: &Entry) -> Result<()> { fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> Result<()>
where W: Write
{
if self.view_header { if self.view_header {
println!("{}", to_string(e.get_header()).unwrap_or(String::from("TOML Parser error"))); let header = to_string(e.get_header()).unwrap_or(String::from("TOML Parser error"));
let _ = writeln!(sink, "{}", header)?;
} }
if self.view_content { if self.view_content {
match self.wrap_content { match self.wrap_content {
Some(limit) => ::textwrap::wrap(e.get_content(), limit).iter().for_each(|line| { Some(limit) => for line in ::textwrap::wrap(e.get_content(), limit).iter() {
println!("{}", line) let _ = writeln!(sink, "{}", line)?;
}), },
None => println!("{}", e.get_content()), None => writeln!(sink, "{}", e.get_content())?,
} }
} }

View File

@ -22,6 +22,10 @@ error_chain! {
ViewError, ViewErrorKind, ResultExt, Result; ViewError, ViewErrorKind, ResultExt, Result;
} }
foreign_links {
IO(::std::io::Error);
}
links { links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
} }

View File

@ -17,17 +17,25 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use std::ops::Deref;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use error::Result; use error::Result;
pub trait Viewer { pub trait Viewer {
fn view_entry(&self, e: &Entry) -> Result<()>; fn view_entry<W>(&self, e: &Entry, sink: &mut W) -> Result<()>
where W: Write;
fn view_entries<I: Iterator<Item = Entry>>(&self, entries: I) -> Result<()> { fn view_entries<I, E, W>(&self, entries: I, sink: &mut W) -> Result<()>
where I: Iterator<Item = E>,
E: Deref<Target = Entry>,
W: Write
{
for entry in entries { for entry in entries {
if let Err(e) = self.view_entry(&entry) { if let Err(e) = self.view_entry(entry.deref(), sink) {
return Err(e); return Err(e);
} }
} }