diff --git a/src/convert/env.rs b/src/convert/env.rs index 53ea087..2d87f10 100644 --- a/src/convert/env.rs +++ b/src/convert/env.rs @@ -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, Rc)>, - w: &mut Write, - ) -> Result<()> { + fn convert_tuple(&self, flds: &Vec<(Positioned, Rc)>, 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>, _w: &mut Write) -> Result<()> { + fn convert_list(&self, _items: &Vec>, _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, mut w: Box) -> Result<()> { + fn convert(&self, v: Rc, mut w: Box) -> Result { self.write(&v, &mut w) } } diff --git a/src/convert/flags.rs b/src/convert/flags.rs index d587163..19ce11d 100644 --- a/src/convert/flags.rs +++ b/src/convert/flags.rs @@ -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>, - w: &mut Write, - ) -> Result<()> { + fn write_list_flag(&self, pfx: &str, name: &str, def: &Vec>, 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, mut w: Box) -> Result<()> { + fn convert(&self, v: Rc, mut w: Box) -> Result { self.write("", &v, &mut w) } } diff --git a/src/convert/json.rs b/src/convert/json.rs index b80ed73..831d0c1 100644 --- a/src/convert/json.rs +++ b/src/convert/json.rs @@ -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>) -> Result { + fn convert_list(&self, items: &Vec>) -> std::io::Result { 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, Rc)>, - ) -> Result { + ) -> std::io::Result { 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 { + fn convert_value(&self, v: &Val) -> std::io::Result { 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, mut w: Box) -> Result<()> { + fn convert(&self, v: Rc, mut w: Box) -> Result { self.write(&v, &mut w) } } diff --git a/src/convert/mod.rs b/src/convert/mod.rs index e55003f..9af5199 100644 --- a/src/convert/mod.rs +++ b/src/convert/mod.rs @@ -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, w: Box) -> io::Result<()> { + pub fn convert(&self, v: Rc, w: Box) -> traits::Result { self.converter.convert(v, w) } } diff --git a/src/convert/traits.rs b/src/convert/traits.rs index e0479cb..495720f 100644 --- a/src/convert/traits.rs +++ b/src/convert/traits.rs @@ -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>; + /// 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, w: Box) -> Result<()>; + fn convert(&self, vs: Rc, w: Box) -> Result; } diff --git a/src/main.rs b/src/main.rs index f144b5b..c3b7899 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, f: Option<&str>) -> io::Result<()> { +fn run_converter(c: ConverterRunner, v: Rc, f: Option<&str>) -> traits::Result { let file: Box = match f { Some(f) => Box::new(try!(File::create(f))), None => Box::new(io::stdout()),