REFACTOR: Rename convert::traits::Result to convert::traits::ConvertResult.

This commit is contained in:
Jeremy Wall 2019-02-19 14:50:55 -06:00
parent fdbbf0fb01
commit a6eab6a459
9 changed files with 40 additions and 34 deletions

View File

@ -18,7 +18,7 @@ use std::rc::Rc;
use crate::ast::PositionedItem;
use crate::build::Val;
use crate::convert::traits::{Converter, Result};
use crate::convert::traits::{ConvertResult, Converter};
/// EnvConverter implements the conversion logic for converting a Val into a
/// set of environment variables.
@ -33,7 +33,7 @@ impl EnvConverter {
&self,
flds: &Vec<(PositionedItem<String>, Rc<Val>)>,
w: &mut Write,
) -> Result {
) -> ConvertResult {
for &(ref name, ref val) in flds.iter() {
if val.is_tuple() {
eprintln!("Skipping embedded tuple...");
@ -49,12 +49,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) -> ConvertResult {
eprintln!("Skipping List...");
Ok(())
}
fn write(&self, v: &Val, w: &mut Write) -> Result {
fn write(&self, v: &Val, w: &mut Write) -> ConvertResult {
match v {
&Val::Empty => {
// Empty is a noop.
@ -96,7 +96,7 @@ impl EnvConverter {
}
impl Converter for EnvConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
self.write(&v, &mut w)
}

View File

@ -21,7 +21,7 @@ use crate::ast::{Position, PositionedItem};
use crate::build::Val;
use crate::build::Val::Tuple;
use crate::convert;
use crate::convert::traits::{Converter, Result};
use crate::convert::traits::{ConvertResult, Converter};
use crate::error::BuildError;
use crate::error::ErrorType;
@ -38,7 +38,7 @@ impl ExecConverter {
}
#[allow(unused_assignments)]
fn write(&self, v: &Val, w: &mut Write) -> Result {
fn write(&self, v: &Val, w: &mut Write) -> ConvertResult {
// We always expect the Val to be a Tuple.
if let &Tuple(ref fields) = v {
// We expect no more than three fields in our exec tuple.
@ -179,7 +179,7 @@ impl ExecConverter {
}
impl Converter for ExecConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
self.write(&v, &mut w)
}

View File

@ -17,7 +17,7 @@ use std::io::Write;
use std::rc::Rc;
use crate::build::Val;
use crate::convert::traits::{Converter, Result};
use crate::convert::traits::{ConvertResult, Converter};
/// FlagConverter implements the conversion logic for converting a Val into a set
/// of command line flags.
@ -28,7 +28,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) -> ConvertResult {
if name.chars().count() > 1 || pfx.chars().count() > 0 {
write!(w, "--{}{} ", pfx, name)?;
} else {
@ -37,7 +37,13 @@ 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,
) -> ConvertResult {
// 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();
@ -54,7 +60,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) -> ConvertResult {
match v {
&Val::Empty => {
// Empty is a noop.
@ -114,7 +120,7 @@ impl FlagConverter {
}
impl Converter for FlagConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
self.write("", &v, &mut w)
}

View File

@ -17,7 +17,7 @@ use serde_json;
use crate::ast;
use crate::build::Val;
use crate::convert::traits::{Converter, ImportResult, Importer, Result};
use crate::convert::traits::{ConvertResult, Converter, ImportResult, Importer};
/// JsonConverter implements the logic for converting a Val into the json output format.
pub struct JsonConverter {}
@ -123,7 +123,7 @@ impl JsonConverter {
})
}
fn write(&self, v: &Val, w: &mut Write) -> Result {
fn write(&self, v: &Val, w: &mut Write) -> ConvertResult {
let jsn_val = self.convert_value(v)?;
serde_json::to_writer_pretty(w, &jsn_val)?;
Ok(())
@ -131,7 +131,7 @@ impl JsonConverter {
}
impl Converter for JsonConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
self.write(&v, &mut w)
}

View File

@ -22,18 +22,18 @@ use toml;
use crate::ast;
use crate::build::Val;
use crate::convert::traits::{Converter, Result};
use crate::convert::traits::{ConvertResult, Converter};
pub struct TomlConverter {}
type ConvertResult = std::result::Result<toml::Value, Box<error::Error>>;
type Result = std::result::Result<toml::Value, Box<error::Error>>;
impl TomlConverter {
pub fn new() -> Self {
TomlConverter {}
}
fn convert_list(&self, items: &Vec<Rc<Val>>) -> ConvertResult {
fn convert_list(&self, items: &Vec<Rc<Val>>) -> Result {
let mut v = Vec::new();
for val in items.iter() {
v.push(self.convert_value(val)?);
@ -41,7 +41,7 @@ impl TomlConverter {
Ok(toml::Value::Array(v))
}
fn convert_tuple(&self, items: &Vec<(ast::PositionedItem<String>, Rc<Val>)>) -> ConvertResult {
fn convert_tuple(&self, items: &Vec<(ast::PositionedItem<String>, Rc<Val>)>) -> Result {
let mut mp = toml::value::Table::new();
for &(ref k, ref v) in items.iter() {
mp.entry(k.val.clone()).or_insert(self.convert_value(v)?);
@ -49,7 +49,7 @@ impl TomlConverter {
Ok(toml::Value::Table(mp))
}
fn convert_env(&self, items: &Vec<(String, String)>) -> ConvertResult {
fn convert_env(&self, items: &Vec<(String, String)>) -> Result {
let mut mp = toml::value::Table::new();
for &(ref k, ref v) in items.iter() {
mp.entry(k.clone())
@ -58,7 +58,7 @@ impl TomlConverter {
Ok(toml::Value::Table(mp))
}
fn convert_value(&self, v: &Val) -> ConvertResult {
fn convert_value(&self, v: &Val) -> Result {
let toml_val = match v {
&Val::Boolean(b) => toml::Value::Boolean(b),
// TODO(jwall): This is an error apparently
@ -84,7 +84,7 @@ impl TomlConverter {
Ok(toml_val)
}
fn write(&self, v: &Val, w: &mut Write) -> Result {
fn write(&self, v: &Val, w: &mut Write) -> ConvertResult {
let toml_val = self.convert_value(v)?;
let toml_bytes = toml::ser::to_string_pretty(&toml_val)?;
write!(w, "{}", toml_bytes)?;
@ -93,7 +93,7 @@ impl TomlConverter {
}
impl Converter for TomlConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
self.write(&v, &mut w)
}

View File

@ -21,14 +21,14 @@ use std::result;
use crate::build::Val;
// TODO Rename to ConvertResult
pub type Result = result::Result<(), Box<dyn Error>>;
pub type ConvertResult = result::Result<(), Box<dyn Error>>;
pub type ImportResult = result::Result<Rc<Val>, Box<dyn 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: &mut Write) -> Result;
fn convert(&self, vs: Rc<Val>, w: &mut Write) -> ConvertResult;
fn file_ext(&self) -> String;
fn description(&self) -> String;
}

View File

@ -17,7 +17,7 @@ use std::error::Error;
use std::io::Write;
use std::rc::Rc;
use super::traits::{Converter, Result};
use super::traits::{ConvertResult, Converter};
use crate::ast::{Position, PositionedItem};
use crate::build::Val;
use crate::error::BuildError;
@ -69,7 +69,7 @@ impl XmlConverter {
}
}
fn write_node<W: std::io::Write>(&self, v: &Val, w: &mut EventWriter<W>) -> Result {
fn write_node<W: std::io::Write>(&self, v: &Val, w: &mut EventWriter<W>) -> ConvertResult {
// First we determine if this is a tag or text node
if let Val::Tuple(ref fs) = v {
let mut name: Option<&str> = None;
@ -168,7 +168,7 @@ impl XmlConverter {
Ok(())
}
fn write(&self, v: &Val, w: &mut Write) -> Result {
fn write(&self, v: &Val, w: &mut Write) -> ConvertResult {
if let Val::Tuple(ref fs) = v {
let mut version: Option<&str> = None;
let mut encoding: Option<&str> = None;
@ -242,7 +242,7 @@ impl XmlConverter {
}
impl Converter for XmlConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
self.write(&v, &mut w)
}

View File

@ -4,7 +4,7 @@ use std::rc::Rc;
use serde_yaml;
use super::traits::{Converter, Result};
use super::traits::{ConvertResult, Converter};
use crate::ast;
use crate::build::Val;
@ -76,7 +76,7 @@ impl YamlConverter {
Ok(yaml_val)
}
fn write(&self, v: &Val, w: &mut Write) -> Result {
fn write(&self, v: &Val, w: &mut Write) -> ConvertResult {
let jsn_val = self.convert_value(v)?;
serde_yaml::to_writer(w, &jsn_val)?;
Ok(())
@ -84,7 +84,7 @@ impl YamlConverter {
}
impl Converter for YamlConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> ConvertResult {
self.write(&v, &mut w)
}

View File

@ -65,7 +65,7 @@ fn do_flags<'a, 'b>() -> clap::App<'a, 'b> {
)
}
fn run_converter(c: &traits::Converter, v: Rc<Val>, f: Option<&str>) -> traits::Result {
fn run_converter(c: &traits::Converter, v: Rc<Val>, f: Option<&str>) -> traits::ConvertResult {
let mut file: Box<std::io::Write> = match f {
Some(f) => {
let mut path_buf = PathBuf::from(f);