imag-view: Replace read with typed read

This commit is contained in:
Matthias Beyer 2018-01-12 16:29:36 +01:00
parent 0c4be78223
commit 0a78d43741

View file

@ -51,8 +51,7 @@ use std::process::Command;
use std::process::exit; use std::process::exit;
use handlebars::Handlebars; use handlebars::Handlebars;
use toml_query::read::TomlValueReadExt; use toml_query::read::TomlValueReadTypeExt;
use toml::Value;
use libimagrt::setup::generate_runtime_setup; use libimagrt::setup::generate_runtime_setup;
use libimagerror::trace::trace_error_exit; use libimagerror::trace::trace_error_exit;
@ -98,72 +97,68 @@ fn main() {
.map_err_trace_exit_unwrap(1); .map_err_trace_exit_unwrap(1);
let query = format!("view.viewers.{}", viewer); let query = format!("view.viewers.{}", viewer);
match config.read(&query) {
Err(e) => trace_error_exit(&e, 1), let viewer_template = config
Ok(None) => { .read_string(&query)
.map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| {
error!("Cannot find '{}' in config", query); error!("Cannot find '{}' in config", query);
exit(1) exit(1)
}, });
Ok(Some(&Value::String(ref viewer_template))) => { let mut handlebars = Handlebars::new();
let mut handlebars = Handlebars::new(); handlebars.register_escape_fn(::handlebars::no_escape);
handlebars.register_escape_fn(::handlebars::no_escape);
let _ = handlebars.register_template_string("template", viewer_template) let _ = handlebars
.register_template_string("template", viewer_template)
.map_err_trace_exit_unwrap(1);
let file = {
let mut tmpfile = tempfile::NamedTempFile::new()
.map_err_trace_exit_unwrap(1);
if view_header {
let hdr = toml::ser::to_string_pretty(entry.get_header())
.map_err_trace_exit_unwrap(1); .map_err_trace_exit_unwrap(1);
let _ = tmpfile.write(format!("---\n{}---\n", hdr).as_bytes())
let file = {
let mut tmpfile = tempfile::NamedTempFile::new()
.map_err_trace_exit_unwrap(1);
if view_header {
let hdr = toml::ser::to_string_pretty(entry.get_header())
.map_err_trace_exit_unwrap(1);
let _ = tmpfile.write(format!("---\n{}---\n", hdr).as_bytes())
.map_err_trace_exit_unwrap(1);
}
if view_content {
let _ = tmpfile.write(entry.get_content().as_bytes())
.map_err_trace_exit_unwrap(1);
}
tmpfile
};
let file_path = file
.path()
.to_str()
.map(String::from)
.ok_or::<VE>("Cannot build path".to_owned().into())
.map_err_trace_exit_unwrap(1); .map_err_trace_exit_unwrap(1);
}
let mut command = { if view_content {
let mut data = BTreeMap::new(); let _ = tmpfile.write(entry.get_content().as_bytes())
data.insert("entry", file_path); .map_err_trace_exit_unwrap(1);
}
let call = handlebars.render("template", &data).map_err_trace_exit_unwrap(1); tmpfile
let mut elems = call.split_whitespace(); };
let command_string = elems
.next()
.ok_or::<VE>("No command".to_owned().into())
.map_err_trace_exit_unwrap(1);
let mut cmd = Command::new(command_string);
for arg in elems { let file_path = file
cmd.arg(arg); .path()
} .to_str()
.map(String::from)
.ok_or::<VE>("Cannot build path".to_owned().into())
.map_err_trace_exit_unwrap(1);
cmd let mut command = {
}; let mut data = BTreeMap::new();
data.insert("entry", file_path);
if !command.status().map_err_trace_exit_unwrap(1).success() { let call = handlebars.render("template", &data).map_err_trace_exit_unwrap(1);
exit(1) let mut elems = call.split_whitespace();
} let command_string = elems
}, .next()
Ok(Some(_)) => { .ok_or::<VE>("No command".to_owned().into())
error!("Type error: Expected String at {}, found non-string", query); .map_err_trace_exit_unwrap(1);
exit(1) let mut cmd = Command::new(command_string);
},
for arg in elems {
cmd.arg(arg);
}
cmd
};
if !command.status().map_err_trace_exit_unwrap(1).success() {
exit(1)
} }
} else { } else {
let _ = StdoutViewer::new(view_header, view_content) let _ = StdoutViewer::new(view_header, view_content)