REFACTOR: Pass file into the constructor for builders.

This commit is contained in:
Jeremy Wall 2018-11-28 20:11:34 -06:00
parent 02cc210eb2
commit 960dbef591
2 changed files with 21 additions and 22 deletions

View File

@ -93,9 +93,8 @@ 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 {
file: PathBuf, file: PathBuf,
curr_file: Option<&'a str>,
validate_mode: bool, validate_mode: bool,
pub assert_collector: AssertCollector, pub assert_collector: AssertCollector,
strict: bool, strict: bool,
@ -138,10 +137,10 @@ macro_rules! eval_binary_expr {
}; };
} }
impl<'a> Builder<'a> { impl Builder {
/// Constructs a new Builder. /// Constructs a new Builder.
pub fn new<P: Into<PathBuf>>(root: P, cache: Rc<RefCell<assets::Cache>>) -> Self { pub fn new<P: Into<PathBuf>>(file: P, cache: Rc<RefCell<assets::Cache>>) -> Self {
Self::new_with_scope(root, cache, HashMap::new()) Self::new_with_scope(file, cache, HashMap::new())
} }
/// Constructs a new Builder with a provided scope. /// Constructs a new Builder with a provided scope.
@ -162,7 +161,6 @@ impl<'a> Builder<'a> {
) -> Self { ) -> Self {
Builder { Builder {
file: root.into(), file: root.into(),
curr_file: None,
validate_mode: false, validate_mode: false,
assert_collector: AssertCollector { assert_collector: AssertCollector {
success: true, success: true,
@ -281,9 +279,8 @@ impl<'a> Builder<'a> {
} }
/// Builds a ucg file at the named path. /// Builds a ucg file at the named path.
pub fn build_file(&mut self, name: &'a str) -> BuildResult { pub fn build_file(&mut self) -> BuildResult {
self.curr_file = Some(name); let mut f = try!(File::open(&self.file));
let mut f = try!(File::open(name));
let mut s = String::new(); let mut s = String::new();
try!(f.read_to_string(&mut s)); try!(f.read_to_string(&mut s));
let eval_result = self.eval_string(&s); let eval_result = self.eval_string(&s);
@ -294,7 +291,11 @@ impl<'a> Builder<'a> {
} }
Err(e) => { Err(e) => {
let err = simple_error::SimpleError::new( let err = simple_error::SimpleError::new(
format!("Error building file: {}\n{}", name, e.as_ref()).as_ref(), format!(
"Error building file: {}\n{}",
self.file.to_string_lossy(),
e.as_ref()
).as_ref(),
); );
Err(Box::new(err)) Err(Box::new(err))
} }
@ -322,10 +323,10 @@ impl<'a> Builder<'a> {
sym.pos.clone(), sym.pos.clone(),
))); )));
} }
let mut normalized = self.file.clone(); let mut normalized = self.file.parent().unwrap().to_path_buf();
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(&import_path);
} else { } else {
normalized = import_path; normalized = import_path;
} }
@ -339,8 +340,7 @@ impl<'a> Builder<'a> {
Some(v) => v.clone(), Some(v) => v.clone(),
None => { None => {
let mut b = Self::new(normalized.clone(), self.assets.clone()); let mut b = Self::new(normalized.clone(), self.assets.clone());
let filepath = normalized.to_str().unwrap().clone(); try!(b.build_file());
try!(b.build_file(filepath));
b.get_outputs_as_val() b.get_outputs_as_val()
} }
}; };

View File

@ -78,16 +78,16 @@ fn build_file(
strict: bool, strict: bool,
cache: Rc<RefCell<Cache>>, cache: Rc<RefCell<Cache>>,
) -> Result<build::Builder, Box<Error>> { ) -> Result<build::Builder, Box<Error>> {
let mut root = PathBuf::from(file); let mut file_path_buf = PathBuf::from(file);
if root.is_relative() { if file_path_buf.is_relative() {
root = std::env::current_dir().unwrap().join(root); file_path_buf = std::env::current_dir().unwrap().join(file_path_buf);
} }
let mut builder = build::Builder::new(root.parent().unwrap(), cache); let mut builder = build::Builder::new(file_path_buf, cache);
builder.set_strict(strict); builder.set_strict(strict);
if validate { if validate {
builder.enable_validate_mode(); builder.enable_validate_mode();
} }
try!(builder.build_file(file)); try!(builder.build_file());
if validate { if validate {
println!("{}", builder.assert_collector.summary); println!("{}", builder.assert_collector.summary);
} }
@ -226,13 +226,12 @@ fn inspect_command(
let file = matches.value_of("INPUT").unwrap(); let file = matches.value_of("INPUT").unwrap();
let sym = matches.value_of("sym"); let sym = matches.value_of("sym");
let target = matches.value_of("target").unwrap(); let target = matches.value_of("target").unwrap();
let root = PathBuf::from(file); let mut builder = build::Builder::new(file, cache);
let mut builder = build::Builder::new(root.parent().unwrap(), cache);
builder.set_strict(strict); builder.set_strict(strict);
match registry.get_converter(target) { match registry.get_converter(target) {
Some(converter) => { Some(converter) => {
// TODO(jwall): We should warn if this is a test file. // TODO(jwall): We should warn if this is a test file.
let result = builder.build_file(file); let result = builder.build_file();
if !result.is_ok() { if !result.is_ok() {
eprintln!("{:?}", result.err().unwrap()); eprintln!("{:?}", result.err().unwrap());
process::exit(1); process::exit(1);