mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
REFACTOR: Builder => FileBuilder
This commit is contained in:
parent
a997b7b513
commit
05aa57ae7f
@ -16,12 +16,12 @@ use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use super::assets::MemoryCache;
|
||||
use super::Builder;
|
||||
use super::FileBuilder;
|
||||
|
||||
fn assert_build(input: &str) {
|
||||
let i_paths = Vec::new();
|
||||
let cache = MemoryCache::new();
|
||||
let mut b = Builder::new("<Eval>", &i_paths, Rc::new(RefCell::new(cache)));
|
||||
let mut b = FileBuilder::new("<Eval>", &i_paths, Rc::new(RefCell::new(cache)));
|
||||
b.enable_validate_mode();
|
||||
b.eval_string(input).unwrap();
|
||||
if !b.assert_collector.success {
|
||||
|
@ -43,7 +43,7 @@ impl MacroDef {
|
||||
pub fn eval(
|
||||
&self,
|
||||
root: PathBuf,
|
||||
parent_builder: &Builder,
|
||||
parent_builder: &FileBuilder,
|
||||
mut args: Vec<Rc<Val>>,
|
||||
) -> Result<Vec<(PositionedItem<String>, Rc<Val>)>, Box<Error>> {
|
||||
// Error conditions. If the args don't match the length and types of the argdefs then this is
|
||||
@ -93,7 +93,7 @@ pub struct AssertCollector {
|
||||
}
|
||||
|
||||
/// Builder handles building ucg code for a single file.
|
||||
pub struct Builder<'a> {
|
||||
pub struct FileBuilder<'a> {
|
||||
file: PathBuf,
|
||||
import_path: &'a Vec<PathBuf>,
|
||||
validate_mode: bool,
|
||||
@ -139,7 +139,7 @@ macro_rules! eval_binary_expr {
|
||||
};
|
||||
}
|
||||
|
||||
impl<'a> Builder<'a> {
|
||||
impl<'a> FileBuilder<'a> {
|
||||
/// Constructs a new Builder.
|
||||
pub fn new<P: Into<PathBuf>>(
|
||||
file: P,
|
||||
@ -174,7 +174,7 @@ impl<'a> Builder<'a> {
|
||||
env: Rc<Val>,
|
||||
) -> Self {
|
||||
let file = file.into();
|
||||
Builder {
|
||||
FileBuilder {
|
||||
// Our import stack is initialized with ourself.
|
||||
import_stack: vec![file.to_string_lossy().to_string()],
|
||||
file: file,
|
||||
@ -197,7 +197,7 @@ impl<'a> Builder<'a> {
|
||||
}
|
||||
|
||||
pub fn clone_builder<P: Into<PathBuf>>(&self, file: P) -> Self {
|
||||
Builder {
|
||||
FileBuilder {
|
||||
// Our import stack is initialized with ourself.
|
||||
import_stack: self.import_stack.clone(),
|
||||
file: file.into(),
|
||||
|
@ -12,14 +12,14 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
use super::assets::MemoryCache;
|
||||
use super::{Builder, CallDef, MacroDef, SelectDef, Val};
|
||||
use super::{CallDef, FileBuilder, MacroDef, SelectDef, Val};
|
||||
use crate::ast::*;
|
||||
|
||||
use std;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn test_expr_to_val(mut cases: Vec<(Expression, Val)>, mut b: Builder) {
|
||||
fn test_expr_to_val(mut cases: Vec<(Expression, Val)>, mut b: FileBuilder) {
|
||||
for tpl in cases.drain(0..) {
|
||||
assert_eq!(b.eval_expr(&tpl.0).unwrap(), Rc::new(tpl.1));
|
||||
}
|
||||
@ -30,7 +30,7 @@ fn test_expr_to_val(mut cases: Vec<(Expression, Val)>, mut b: Builder) {
|
||||
fn test_eval_div_expr_fail() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
test_expr_to_val(
|
||||
vec![(
|
||||
Expression::Binary(BinaryOpDef {
|
||||
@ -56,7 +56,7 @@ fn test_eval_div_expr_fail() {
|
||||
fn test_eval_mul_expr_fail() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
test_expr_to_val(
|
||||
vec![(
|
||||
Expression::Binary(BinaryOpDef {
|
||||
@ -82,7 +82,7 @@ fn test_eval_mul_expr_fail() {
|
||||
fn test_eval_subtract_expr_fail() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
test_expr_to_val(
|
||||
vec![(
|
||||
Expression::Binary(BinaryOpDef {
|
||||
@ -107,7 +107,7 @@ fn test_eval_subtract_expr_fail() {
|
||||
fn test_eval_add_expr_fail() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
test_expr_to_val(
|
||||
vec![(
|
||||
Expression::Binary(BinaryOpDef {
|
||||
@ -132,7 +132,7 @@ fn test_eval_add_expr_fail() {
|
||||
fn test_eval_simple_lookup_error() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let mut b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let mut b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
b.build_output
|
||||
.entry(value_node!("var1".to_string(), Position::new(1, 0, 0)))
|
||||
.or_insert(Rc::new(Val::Int(1)));
|
||||
@ -149,7 +149,7 @@ fn test_eval_simple_lookup_error() {
|
||||
fn test_expr_copy_no_such_tuple() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
test_expr_to_val(
|
||||
vec![(
|
||||
Expression::Copy(CopyDef {
|
||||
@ -171,7 +171,7 @@ fn test_expr_copy_no_such_tuple() {
|
||||
fn test_expr_copy_not_a_tuple() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let mut b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let mut b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
b.build_output
|
||||
.entry(value_node!("tpl1".to_string(), Position::new(1, 0, 0)))
|
||||
.or_insert(Rc::new(Val::Int(1)));
|
||||
@ -196,7 +196,7 @@ fn test_expr_copy_not_a_tuple() {
|
||||
fn test_expr_copy_field_type_error() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let mut b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let mut b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
b.build_output
|
||||
.entry(value_node!("tpl1".to_string(), Position::new(1, 0, 0)))
|
||||
.or_insert(Rc::new(Val::Tuple(vec![(
|
||||
@ -233,7 +233,7 @@ fn test_expr_copy_field_type_error() {
|
||||
fn test_macro_hermetic() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let mut b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let mut b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
b.build_output
|
||||
.entry(value_node!("arg1".to_string(), Position::new(1, 0, 0)))
|
||||
.or_insert(Rc::new(Val::Str("bar".to_string())));
|
||||
@ -277,7 +277,7 @@ fn test_macro_hermetic() {
|
||||
fn test_select_expr_not_a_string() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let mut b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let mut b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
b.build_output
|
||||
.entry(value_node!("foo".to_string(), Position::new(1, 0, 0)))
|
||||
.or_insert(Rc::new(Val::Int(4)));
|
||||
|
@ -196,7 +196,7 @@ impl Converter for ExecConverter {
|
||||
mod exec_test {
|
||||
use super::*;
|
||||
use crate::build::assets::MemoryCache;
|
||||
use crate::build::Builder;
|
||||
use crate::build::FileBuilder;
|
||||
use crate::convert::traits::Converter;
|
||||
|
||||
use std;
|
||||
@ -207,7 +207,7 @@ mod exec_test {
|
||||
fn convert_just_command_test() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let mut b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let mut b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let conv = ExecConverter::new();
|
||||
b.eval_string(
|
||||
"let script = {
|
||||
@ -229,7 +229,7 @@ mod exec_test {
|
||||
fn convert_command_with_env_test() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let mut b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let mut b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let conv = ExecConverter::new();
|
||||
b.eval_string(
|
||||
"let script = {
|
||||
@ -258,7 +258,7 @@ mod exec_test {
|
||||
fn convert_command_with_arg_test() {
|
||||
let i_paths = Vec::new();
|
||||
let cache = Rc::new(RefCell::new(MemoryCache::new()));
|
||||
let mut b = Builder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let mut b = FileBuilder::new(std::env::current_dir().unwrap(), &i_paths, cache);
|
||||
let conv = ExecConverter::new();
|
||||
b.eval_string(
|
||||
"let script = {
|
||||
|
@ -44,6 +44,6 @@ pub use crate::ast::Expression;
|
||||
pub use crate::ast::Statement;
|
||||
pub use crate::ast::Value;
|
||||
|
||||
pub use crate::build::Builder;
|
||||
pub use crate::build::FileBuilder;
|
||||
pub use crate::build::Val;
|
||||
pub use crate::parse::parse;
|
||||
|
@ -80,12 +80,12 @@ fn build_file<'a>(
|
||||
strict: bool,
|
||||
import_paths: &'a Vec<PathBuf>,
|
||||
cache: Rc<RefCell<Cache>>,
|
||||
) -> Result<build::Builder<'a>, Box<Error>> {
|
||||
) -> Result<build::FileBuilder<'a>, Box<Error>> {
|
||||
let mut file_path_buf = PathBuf::from(file);
|
||||
if file_path_buf.is_relative() {
|
||||
file_path_buf = std::env::current_dir().unwrap().join(file_path_buf);
|
||||
}
|
||||
let mut builder = build::Builder::new(file_path_buf, import_paths, cache);
|
||||
let mut builder = build::FileBuilder::new(file_path_buf, import_paths, cache);
|
||||
builder.set_strict(strict);
|
||||
if validate {
|
||||
builder.enable_validate_mode();
|
||||
@ -243,7 +243,7 @@ fn inspect_command(
|
||||
let file = matches.value_of("INPUT").unwrap();
|
||||
let sym = matches.value_of("sym");
|
||||
let target = matches.value_of("target").unwrap();
|
||||
let mut builder = build::Builder::new(file, import_paths, cache);
|
||||
let mut builder = build::FileBuilder::new(file, import_paths, cache);
|
||||
builder.set_strict(strict);
|
||||
match registry.get_converter(target) {
|
||||
Some(converter) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user