mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-25 18:49:50 -04:00
REFACTOR: Don't presume io::Result for the converter Result.
This commit is contained in:
parent
5d32dc83a4
commit
db9617bd6f
@ -13,13 +13,12 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
//! An environment variable converter.
|
//! An environment variable converter.
|
||||||
use std::io::Result;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use ast::Positioned;
|
use ast::Positioned;
|
||||||
use build::Val;
|
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.
|
/// EnvConverter implements the conversion logic for converting a Val into a set of environment variables.
|
||||||
pub struct EnvConverter {}
|
pub struct EnvConverter {}
|
||||||
@ -29,11 +28,7 @@ impl EnvConverter {
|
|||||||
EnvConverter {}
|
EnvConverter {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_tuple(
|
fn convert_tuple(&self, flds: &Vec<(Positioned<String>, Rc<Val>)>, w: &mut Write) -> Result {
|
||||||
&self,
|
|
||||||
flds: &Vec<(Positioned<String>, Rc<Val>)>,
|
|
||||||
w: &mut Write,
|
|
||||||
) -> Result<()> {
|
|
||||||
for &(ref name, ref val) in flds.iter() {
|
for &(ref name, ref val) in flds.iter() {
|
||||||
if val.is_tuple() {
|
if val.is_tuple() {
|
||||||
eprintln!("Skipping embedded tuple...");
|
eprintln!("Skipping embedded tuple...");
|
||||||
@ -49,12 +44,12 @@ impl EnvConverter {
|
|||||||
Ok(())
|
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...");
|
eprintln!("Skipping List...");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&self, v: &Val, w: &mut Write) -> Result<()> {
|
fn write(&self, v: &Val, w: &mut Write) -> Result {
|
||||||
match v {
|
match v {
|
||||||
&Val::Empty => {
|
&Val::Empty => {
|
||||||
// Empty is a noop.
|
// Empty is a noop.
|
||||||
@ -88,7 +83,7 @@ impl EnvConverter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Converter for 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)
|
self.write(&v, &mut w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,12 +13,11 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
//! Contains code for converting a UCG Val into the command line flag output target.
|
//! Contains code for converting a UCG Val into the command line flag output target.
|
||||||
use std::io::Result;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use build::Val;
|
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.
|
/// FlagConverter implements the conversion logic for converting a Val into a set of command line flags.
|
||||||
pub struct FlagConverter {}
|
pub struct FlagConverter {}
|
||||||
@ -28,7 +27,7 @@ impl FlagConverter {
|
|||||||
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 {
|
if name.chars().count() > 1 || pfx.chars().count() > 0 {
|
||||||
try!(write!(w, "--{}{} ", pfx, name));
|
try!(write!(w, "--{}{} ", pfx, name));
|
||||||
} else {
|
} else {
|
||||||
@ -37,13 +36,7 @@ impl FlagConverter {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_list_flag(
|
fn write_list_flag(&self, pfx: &str, name: &str, def: &Vec<Rc<Val>>, w: &mut Write) -> Result {
|
||||||
&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.
|
// first of all we need to make sure that each &Val is only a primitive type.
|
||||||
for v in def.iter() {
|
for v in def.iter() {
|
||||||
let vref = v.as_ref();
|
let vref = v.as_ref();
|
||||||
@ -60,7 +53,7 @@ impl FlagConverter {
|
|||||||
return Ok(());
|
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 {
|
match v {
|
||||||
&Val::Empty => {
|
&Val::Empty => {
|
||||||
// Empty is a noop.
|
// Empty is a noop.
|
||||||
@ -110,7 +103,7 @@ impl FlagConverter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Converter for 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)
|
self.write("", &v, &mut w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
//! Flags contains code for converting a UCG Val into the json output target.
|
//! 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::io::Write;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ use serde_json;
|
|||||||
|
|
||||||
use ast;
|
use ast;
|
||||||
use build::Val;
|
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.
|
/// JsonConverter implements the logic for converting a Val into the json output format.
|
||||||
pub struct JsonConverter {}
|
pub struct JsonConverter {}
|
||||||
@ -26,7 +26,7 @@ impl JsonConverter {
|
|||||||
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();
|
let mut v = Vec::new();
|
||||||
for val in items.iter() {
|
for val in items.iter() {
|
||||||
v.push(try!(self.convert_value(val)));
|
v.push(try!(self.convert_value(val)));
|
||||||
@ -37,7 +37,7 @@ impl JsonConverter {
|
|||||||
fn convert_tuple(
|
fn convert_tuple(
|
||||||
&self,
|
&self,
|
||||||
items: &Vec<(ast::Positioned<String>, Rc<Val>)>,
|
items: &Vec<(ast::Positioned<String>, Rc<Val>)>,
|
||||||
) -> Result<serde_json::Value> {
|
) -> std::io::Result<serde_json::Value> {
|
||||||
let mut mp = serde_json::Map::new();
|
let mut mp = serde_json::Map::new();
|
||||||
for &(ref k, ref v) in items.iter() {
|
for &(ref k, ref v) in items.iter() {
|
||||||
mp.entry(k.val.clone())
|
mp.entry(k.val.clone())
|
||||||
@ -46,7 +46,7 @@ impl JsonConverter {
|
|||||||
Ok(serde_json::Value::Object(mp))
|
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 {
|
let jsn_val = match v {
|
||||||
&Val::Boolean(b) => serde_json::Value::Bool(b),
|
&Val::Boolean(b) => serde_json::Value::Bool(b),
|
||||||
&Val::Empty => serde_json::Value::Null,
|
&Val::Empty => serde_json::Value::Null,
|
||||||
@ -77,7 +77,7 @@ impl JsonConverter {
|
|||||||
Ok(jsn_val)
|
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));
|
let jsn_val = try!(self.convert_value(v));
|
||||||
try!(serde_json::to_writer(w, &jsn_val));
|
try!(serde_json::to_writer(w, &jsn_val));
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -85,7 +85,7 @@ impl JsonConverter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Converter for 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)
|
self.write(&v, &mut w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ pub mod flags;
|
|||||||
pub mod json;
|
pub mod json;
|
||||||
pub mod traits;
|
pub mod traits;
|
||||||
|
|
||||||
use std::io;
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::rc::Rc;
|
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.
|
/// 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)
|
self.converter.convert(v, w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,14 +13,17 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
//! The traits used by the ucg compiler for converting Val intermediate format into the output formats..
|
//! 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::io::Write;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::result;
|
||||||
|
|
||||||
use build::Val;
|
use build::Val;
|
||||||
|
|
||||||
|
pub type Result = result::Result<(), Box<Error>>;
|
||||||
|
|
||||||
/// The trait that Converters from Val to different output formats for the
|
/// The trait that Converters from Val to different output formats for the
|
||||||
/// final conversion stage of the ucg compiler.
|
/// final conversion stage of the ucg compiler.
|
||||||
pub trait Converter {
|
pub trait Converter {
|
||||||
fn convert(&self, vs: Rc<Val>, w: Box<Write>) -> Result<()>;
|
fn convert(&self, vs: Rc<Val>, w: Box<Write>) -> Result;
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
use ucglib::build;
|
use ucglib::build;
|
||||||
use ucglib::build::Val;
|
use ucglib::build::Val;
|
||||||
|
use ucglib::convert::traits;
|
||||||
use ucglib::convert::ConverterRunner;
|
use ucglib::convert::ConverterRunner;
|
||||||
|
|
||||||
// TODO(jwall): List the target output types automatically.
|
// TODO(jwall): List the target output types automatically.
|
||||||
@ -46,7 +47,7 @@ fn do_flags<'a>() -> clap::ArgMatches<'a> {
|
|||||||
).get_matches()
|
).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 {
|
let file: Box<std::io::Write> = match f {
|
||||||
Some(f) => Box::new(try!(File::create(f))),
|
Some(f) => Box::new(try!(File::create(f))),
|
||||||
None => Box::new(io::stdout()),
|
None => Box::new(io::stdout()),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user