REFACTOR: Don't presume io::Result for the converter Result.

This commit is contained in:
Jeremy Wall 2018-06-18 22:07:18 -05:00
parent 5d32dc83a4
commit db9617bd6f
6 changed files with 25 additions and 34 deletions

View File

@ -13,13 +13,12 @@
// limitations under the License.
//! An environment variable converter.
use std::io::Result;
use std::io::Write;
use std::rc::Rc;
use ast::Positioned;
use build::Val;
use convert::traits::Converter;
use convert::traits::{Converter, Result};
/// EnvConverter implements the conversion logic for converting a Val into a set of environment variables.
pub struct EnvConverter {}
@ -29,11 +28,7 @@ impl EnvConverter {
EnvConverter {}
}
fn convert_tuple(
&self,
flds: &Vec<(Positioned<String>, Rc<Val>)>,
w: &mut Write,
) -> Result<()> {
fn convert_tuple(&self, flds: &Vec<(Positioned<String>, Rc<Val>)>, w: &mut Write) -> Result {
for &(ref name, ref val) in flds.iter() {
if val.is_tuple() {
eprintln!("Skipping embedded tuple...");
@ -49,12 +44,12 @@ impl EnvConverter {
Ok(())
}
fn convert_list(&self, _items: &Vec<Rc<Val>>, _w: &mut Write) -> Result<()> {
fn convert_list(&self, _items: &Vec<Rc<Val>>, _w: &mut Write) -> Result {
eprintln!("Skipping List...");
Ok(())
}
fn write(&self, v: &Val, w: &mut Write) -> Result<()> {
fn write(&self, v: &Val, w: &mut Write) -> Result {
match v {
&Val::Empty => {
// Empty is a noop.
@ -88,7 +83,7 @@ impl EnvConverter {
}
impl Converter for EnvConverter {
fn convert(&self, v: Rc<Val>, mut w: Box<Write>) -> Result<()> {
fn convert(&self, v: Rc<Val>, mut w: Box<Write>) -> Result {
self.write(&v, &mut w)
}
}

View File

@ -13,12 +13,11 @@
// limitations under the License.
//! Contains code for converting a UCG Val into the command line flag output target.
use std::io::Result;
use std::io::Write;
use std::rc::Rc;
use build::Val;
use convert::traits::Converter;
use convert::traits::{Converter, Result};
/// FlagConverter implements the conversion logic for converting a Val into a set of command line flags.
pub struct FlagConverter {}
@ -28,7 +27,7 @@ impl FlagConverter {
FlagConverter {}
}
fn write_flag_name(&self, pfx: &str, name: &str, w: &mut Write) -> Result<()> {
fn write_flag_name(&self, pfx: &str, name: &str, w: &mut Write) -> Result {
if name.chars().count() > 1 || pfx.chars().count() > 0 {
try!(write!(w, "--{}{} ", pfx, name));
} else {
@ -37,13 +36,7 @@ impl FlagConverter {
return Ok(());
}
fn write_list_flag(
&self,
pfx: &str,
name: &str,
def: &Vec<Rc<Val>>,
w: &mut Write,
) -> Result<()> {
fn write_list_flag(&self, pfx: &str, name: &str, def: &Vec<Rc<Val>>, w: &mut Write) -> Result {
// first of all we need to make sure that each &Val is only a primitive type.
for v in def.iter() {
let vref = v.as_ref();
@ -60,7 +53,7 @@ impl FlagConverter {
return Ok(());
}
fn write(&self, pfx: &str, v: &Val, w: &mut Write) -> Result<()> {
fn write(&self, pfx: &str, v: &Val, w: &mut Write) -> Result {
match v {
&Val::Empty => {
// Empty is a noop.
@ -110,7 +103,7 @@ impl FlagConverter {
}
impl Converter for FlagConverter {
fn convert(&self, v: Rc<Val>, mut w: Box<Write>) -> Result<()> {
fn convert(&self, v: Rc<Val>, mut w: Box<Write>) -> Result {
self.write("", &v, &mut w)
}
}

View File

@ -8,7 +8,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! Flags contains code for converting a UCG Val into the json output target.
use std::io::Result;
use std;
use std::io::Write;
use std::rc::Rc;
@ -16,7 +16,7 @@ use serde_json;
use ast;
use build::Val;
use convert::traits::Converter;
use convert::traits::{Converter, Result};
/// JsonConverter implements the logic for converting a Val into the json output format.
pub struct JsonConverter {}
@ -26,7 +26,7 @@ impl JsonConverter {
JsonConverter {}
}
fn convert_list(&self, items: &Vec<Rc<Val>>) -> Result<serde_json::Value> {
fn convert_list(&self, items: &Vec<Rc<Val>>) -> std::io::Result<serde_json::Value> {
let mut v = Vec::new();
for val in items.iter() {
v.push(try!(self.convert_value(val)));
@ -37,7 +37,7 @@ impl JsonConverter {
fn convert_tuple(
&self,
items: &Vec<(ast::Positioned<String>, Rc<Val>)>,
) -> Result<serde_json::Value> {
) -> std::io::Result<serde_json::Value> {
let mut mp = serde_json::Map::new();
for &(ref k, ref v) in items.iter() {
mp.entry(k.val.clone())
@ -46,7 +46,7 @@ impl JsonConverter {
Ok(serde_json::Value::Object(mp))
}
fn convert_value(&self, v: &Val) -> Result<serde_json::Value> {
fn convert_value(&self, v: &Val) -> std::io::Result<serde_json::Value> {
let jsn_val = match v {
&Val::Boolean(b) => serde_json::Value::Bool(b),
&Val::Empty => serde_json::Value::Null,
@ -77,7 +77,7 @@ impl JsonConverter {
Ok(jsn_val)
}
fn write(&self, v: &Val, w: &mut Write) -> Result<()> {
fn write(&self, v: &Val, w: &mut Write) -> Result {
let jsn_val = try!(self.convert_value(v));
try!(serde_json::to_writer(w, &jsn_val));
Ok(())
@ -85,7 +85,7 @@ impl JsonConverter {
}
impl Converter for JsonConverter {
fn convert(&self, v: Rc<Val>, mut w: Box<Write>) -> Result<()> {
fn convert(&self, v: Rc<Val>, mut w: Box<Write>) -> Result {
self.write(&v, &mut w)
}
}

View File

@ -18,7 +18,6 @@ pub mod flags;
pub mod json;
pub mod traits;
use std::io;
use std::io::Write;
use std::rc::Rc;
@ -54,7 +53,7 @@ impl ConverterRunner {
}
/// convert runs the Converter on a Val and writes the output to the provided writer.
pub fn convert(&self, v: Rc<Val>, w: Box<Write>) -> io::Result<()> {
pub fn convert(&self, v: Rc<Val>, w: Box<Write>) -> traits::Result {
self.converter.convert(v, w)
}
}

View File

@ -13,14 +13,17 @@
// limitations under the License.
//! The traits used by the ucg compiler for converting Val intermediate format into the output formats..
use std::io::Result;
use std::error::Error;
use std::io::Write;
use std::rc::Rc;
use std::result;
use build::Val;
pub type Result = result::Result<(), Box<Error>>;
/// The trait that Converters from Val to different output formats for the
/// final conversion stage of the ucg compiler.
pub trait Converter {
fn convert(&self, vs: Rc<Val>, w: Box<Write>) -> Result<()>;
fn convert(&self, vs: Rc<Val>, w: Box<Write>) -> Result;
}

View File

@ -23,6 +23,7 @@ use std::rc::Rc;
use ucglib::build;
use ucglib::build::Val;
use ucglib::convert::traits;
use ucglib::convert::ConverterRunner;
// TODO(jwall): List the target output types automatically.
@ -46,7 +47,7 @@ fn do_flags<'a>() -> clap::ArgMatches<'a> {
).get_matches()
}
fn run_converter(c: ConverterRunner, v: Rc<Val>, f: Option<&str>) -> io::Result<()> {
fn run_converter(c: ConverterRunner, v: Rc<Val>, f: Option<&str>) -> traits::Result {
let file: Box<std::io::Write> = match f {
Some(f) => Box::new(try!(File::create(f))),
None => Box::new(io::stdout()),