mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
REFACTOR: Pass file into the constructor for builders.
This commit is contained in:
parent
02cc210eb2
commit
960dbef591
@ -93,9 +93,8 @@ pub struct AssertCollector {
|
||||
}
|
||||
|
||||
/// Builder handles building ucg code for a single file.
|
||||
pub struct Builder<'a> {
|
||||
pub struct Builder {
|
||||
file: PathBuf,
|
||||
curr_file: Option<&'a str>,
|
||||
validate_mode: bool,
|
||||
pub assert_collector: AssertCollector,
|
||||
strict: bool,
|
||||
@ -138,10 +137,10 @@ macro_rules! eval_binary_expr {
|
||||
};
|
||||
}
|
||||
|
||||
impl<'a> Builder<'a> {
|
||||
impl Builder {
|
||||
/// Constructs a new Builder.
|
||||
pub fn new<P: Into<PathBuf>>(root: P, cache: Rc<RefCell<assets::Cache>>) -> Self {
|
||||
Self::new_with_scope(root, cache, HashMap::new())
|
||||
pub fn new<P: Into<PathBuf>>(file: P, cache: Rc<RefCell<assets::Cache>>) -> Self {
|
||||
Self::new_with_scope(file, cache, HashMap::new())
|
||||
}
|
||||
|
||||
/// Constructs a new Builder with a provided scope.
|
||||
@ -162,7 +161,6 @@ impl<'a> Builder<'a> {
|
||||
) -> Self {
|
||||
Builder {
|
||||
file: root.into(),
|
||||
curr_file: None,
|
||||
validate_mode: false,
|
||||
assert_collector: AssertCollector {
|
||||
success: true,
|
||||
@ -281,9 +279,8 @@ impl<'a> Builder<'a> {
|
||||
}
|
||||
|
||||
/// Builds a ucg file at the named path.
|
||||
pub fn build_file(&mut self, name: &'a str) -> BuildResult {
|
||||
self.curr_file = Some(name);
|
||||
let mut f = try!(File::open(name));
|
||||
pub fn build_file(&mut self) -> BuildResult {
|
||||
let mut f = try!(File::open(&self.file));
|
||||
let mut s = String::new();
|
||||
try!(f.read_to_string(&mut s));
|
||||
let eval_result = self.eval_string(&s);
|
||||
@ -294,7 +291,11 @@ impl<'a> Builder<'a> {
|
||||
}
|
||||
Err(e) => {
|
||||
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))
|
||||
}
|
||||
@ -322,10 +323,10 @@ impl<'a> Builder<'a> {
|
||||
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);
|
||||
if import_path.is_relative() {
|
||||
normalized.push(&def.path.fragment);
|
||||
normalized.push(&import_path);
|
||||
} else {
|
||||
normalized = import_path;
|
||||
}
|
||||
@ -339,8 +340,7 @@ impl<'a> Builder<'a> {
|
||||
Some(v) => v.clone(),
|
||||
None => {
|
||||
let mut b = Self::new(normalized.clone(), self.assets.clone());
|
||||
let filepath = normalized.to_str().unwrap().clone();
|
||||
try!(b.build_file(filepath));
|
||||
try!(b.build_file());
|
||||
b.get_outputs_as_val()
|
||||
}
|
||||
};
|
||||
|
15
src/main.rs
15
src/main.rs
@ -78,16 +78,16 @@ fn build_file(
|
||||
strict: bool,
|
||||
cache: Rc<RefCell<Cache>>,
|
||||
) -> Result<build::Builder, Box<Error>> {
|
||||
let mut root = PathBuf::from(file);
|
||||
if root.is_relative() {
|
||||
root = std::env::current_dir().unwrap().join(root);
|
||||
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(root.parent().unwrap(), cache);
|
||||
let mut builder = build::Builder::new(file_path_buf, cache);
|
||||
builder.set_strict(strict);
|
||||
if validate {
|
||||
builder.enable_validate_mode();
|
||||
}
|
||||
try!(builder.build_file(file));
|
||||
try!(builder.build_file());
|
||||
if validate {
|
||||
println!("{}", builder.assert_collector.summary);
|
||||
}
|
||||
@ -226,13 +226,12 @@ fn inspect_command(
|
||||
let file = matches.value_of("INPUT").unwrap();
|
||||
let sym = matches.value_of("sym");
|
||||
let target = matches.value_of("target").unwrap();
|
||||
let root = PathBuf::from(file);
|
||||
let mut builder = build::Builder::new(root.parent().unwrap(), cache);
|
||||
let mut builder = build::Builder::new(file, cache);
|
||||
builder.set_strict(strict);
|
||||
match registry.get_converter(target) {
|
||||
Some(converter) => {
|
||||
// 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() {
|
||||
eprintln!("{:?}", result.err().unwrap());
|
||||
process::exit(1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user