REFACTOR: root is actually always a file. Lets call it as such.

Also have a helper function to get the current build_dir.
This commit is contained in:
Jeremy Wall 2018-11-25 13:02:49 -06:00
parent 9524cd9d25
commit b797f099b4

View File

@ -94,7 +94,7 @@ pub struct AssertCollector {
/// Builder handles building ucg code for a single file.. /// Builder handles building ucg code for a single file..
pub struct Builder<'a> { pub struct Builder<'a> {
root: PathBuf, file: PathBuf,
curr_file: Option<&'a str>, curr_file: Option<&'a str>,
validate_mode: bool, validate_mode: bool,
pub assert_collector: AssertCollector, pub assert_collector: AssertCollector,
@ -171,7 +171,7 @@ impl<'a> Builder<'a> {
format!( format!(
"Unable to find {} in file: {}", "Unable to find {} in file: {}",
s.val, s.val,
self.root.to_string_lossy() self.file.to_string_lossy()
), ),
error::ErrorType::NoSuchSymbol, error::ErrorType::NoSuchSymbol,
v.pos().clone(), v.pos().clone(),
@ -213,7 +213,7 @@ impl<'a> Builder<'a> {
env: Rc<Val>, env: Rc<Val>,
) -> Self { ) -> Self {
Builder { Builder {
root: root.into(), file: root.into(),
curr_file: None, curr_file: None,
validate_mode: false, validate_mode: false,
assert_collector: AssertCollector { assert_collector: AssertCollector {
@ -305,7 +305,7 @@ impl<'a> Builder<'a> {
fn build_import(&mut self, def: &ImportDef) -> Result<Rc<Val>, Box<Error>> { fn build_import(&mut self, def: &ImportDef) -> Result<Rc<Val>, Box<Error>> {
let sym = &def.name; let sym = &def.name;
let mut normalized = self.root.clone(); let mut normalized = self.file.clone();
let import_path = PathBuf::from(&def.path.fragment); let import_path = PathBuf::from(&def.path.fragment);
if import_path.is_relative() { if import_path.is_relative() {
normalized.push(&def.path.fragment); normalized.push(&def.path.fragment);
@ -352,7 +352,7 @@ impl<'a> Builder<'a> {
for {:?} already \ for {:?} already \
exists in file: {}", exists in file: {}",
e.key(), e.key(),
self.root.to_string_lossy(), self.file.to_string_lossy(),
), ),
error::ErrorType::DuplicateBinding, error::ErrorType::DuplicateBinding,
def.name.pos.clone(), def.name.pos.clone(),
@ -432,7 +432,7 @@ impl<'a> Builder<'a> {
path [{}] in file: {}", path [{}] in file: {}",
next.1, next.1,
sl, sl,
self.root.to_string_lossy(), self.file.to_string_lossy(),
), ),
error::ErrorType::NoSuchSymbol, error::ErrorType::NoSuchSymbol,
next.0.clone(), next.0.clone(),
@ -459,7 +459,7 @@ impl<'a> Builder<'a> {
path [{}] in file: {}", path [{}] in file: {}",
next.1, next.1,
sl, sl,
self.root.to_string_lossy(), self.file.to_string_lossy(),
), ),
error::ErrorType::NoSuchSymbol, error::ErrorType::NoSuchSymbol,
next.0.clone(), next.0.clone(),
@ -664,7 +664,7 @@ impl<'a> Builder<'a> {
) -> Result<Rc<Val>, Box<Error>> { ) -> Result<Rc<Val>, Box<Error>> {
Ok(Rc::new(Val::Boolean(try!(left.equal( Ok(Rc::new(Val::Boolean(try!(left.equal(
right.as_ref(), right.as_ref(),
&self.root.to_string_lossy(), &self.file.to_string_lossy(),
pos.clone() pos.clone()
))))) )))))
} }
@ -677,7 +677,7 @@ impl<'a> Builder<'a> {
) -> Result<Rc<Val>, Box<Error>> { ) -> Result<Rc<Val>, Box<Error>> {
Ok(Rc::new(Val::Boolean(!try!(left.equal( Ok(Rc::new(Val::Boolean(!try!(left.equal(
right.as_ref(), right.as_ref(),
&self.root.to_string_lossy(), &self.file.to_string_lossy(),
pos.clone() pos.clone()
))))) )))))
} }
@ -925,7 +925,7 @@ impl<'a> Builder<'a> {
let maybe_tpl = mod_def.clone().arg_tuple.unwrap().clone(); let maybe_tpl = mod_def.clone().arg_tuple.unwrap().clone();
if let &Val::Tuple(ref src_fields) = maybe_tpl.as_ref() { if let &Val::Tuple(ref src_fields) = maybe_tpl.as_ref() {
// 1. First we create a builder. // 1. First we create a builder.
let mut b = Self::new(self.root.clone(), self.assets.clone()); let mut b = Self::new(self.file.clone(), self.assets.clone());
b.is_module = true; b.is_module = true;
// 2. We construct an argument tuple by copying from the defs // 2. We construct an argument tuple by copying from the defs
// argset. // argset.
@ -1000,7 +1000,7 @@ impl<'a> Builder<'a> {
argvals.push(try!(self.eval_expr(arg))); argvals.push(try!(self.eval_expr(arg)));
} }
let fields = try!(m.eval( let fields = try!(m.eval(
self.root.clone(), self.file.clone(),
self.assets.clone(), self.assets.clone(),
self.env.clone(), self.env.clone(),
argvals argvals
@ -1030,17 +1030,21 @@ impl<'a> Builder<'a> {
} }
} }
fn file_dir(&self) -> PathBuf {
return if self.file.is_file() {
// Only use the dirname portion if the root is a file.
self.file.parent().unwrap().to_path_buf()
} else {
// otherwise use clone of the root..
self.file.clone()
};
}
fn eval_module_def(&mut self, def: &ModuleDef) -> Result<Rc<Val>, Box<Error>> { fn eval_module_def(&mut self, def: &ModuleDef) -> Result<Rc<Val>, Box<Error>> {
let root = self.file_dir();
// Always work on a copy. The original should not be modified. // Always work on a copy. The original should not be modified.
let mut def = def.clone(); let mut def = def.clone();
// First we rewrite the imports to be absolute paths. // First we rewrite the imports to be absolute paths.
let root = if self.root.is_file() {
// Only use the dirname portion if the root is a file.
self.root.parent().unwrap().to_path_buf()
} else {
// otherwise use clone of the root..
self.root.clone()
};
def.imports_to_absolute(root); def.imports_to_absolute(root);
// Then we create our tuple default. // Then we create our tuple default.
def.arg_tuple = Some(try!(self.tuple_to_val(&def.arg_set))); def.arg_tuple = Some(try!(self.tuple_to_val(&def.arg_set)));
@ -1096,7 +1100,7 @@ impl<'a> Builder<'a> {
for item in l.iter() { for item in l.iter() {
let argvals = vec![item.clone()]; let argvals = vec![item.clone()];
let fields = try!(macdef.eval( let fields = try!(macdef.eval(
self.root.clone(), self.file.clone(),
self.assets.clone(), self.assets.clone(),
self.env.clone(), self.env.clone(),
argvals argvals