Change filename to path for clarity
This commit is contained in:
parent
8c1c37666d
commit
6c72e3c48b
@ -153,7 +153,7 @@ use rhai::Engine;
|
|||||||
|
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
|
|
||||||
let ast = engine.compile_file("hello_world.rhai").unwrap();
|
let ast = engine.compile_file("hello_world.rhai".into()).unwrap();
|
||||||
```
|
```
|
||||||
|
|
||||||
Rhai also allows you to work _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust.
|
Rhai also allows you to work _backwards_ from the other direction - i.e. calling a Rhai-scripted function from Rust.
|
||||||
|
24
src/api.rs
24
src/api.rs
@ -106,26 +106,26 @@ impl<'e> Engine<'e> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a file into an AST.
|
/// Compile a file into an AST.
|
||||||
pub fn compile_file(&self, filename: PathBuf) -> Result<AST, EvalAltResult> {
|
pub fn compile_file(&self, path: PathBuf) -> Result<AST, EvalAltResult> {
|
||||||
let mut f = File::open(filename.clone())
|
let mut f = File::open(path.clone())
|
||||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?;
|
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?;
|
||||||
|
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
|
|
||||||
f.read_to_string(&mut contents)
|
f.read_to_string(&mut contents)
|
||||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))
|
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))
|
||||||
.and_then(|_| self.compile(&contents).map_err(EvalAltResult::ErrorParsing))
|
.and_then(|_| self.compile(&contents).map_err(EvalAltResult::ErrorParsing))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Evaluate a file.
|
/// Evaluate a file.
|
||||||
pub fn eval_file<T: Any + Clone>(&mut self, filename: PathBuf) -> Result<T, EvalAltResult> {
|
pub fn eval_file<T: Any + Clone>(&mut self, path: PathBuf) -> Result<T, EvalAltResult> {
|
||||||
let mut f = File::open(filename.clone())
|
let mut f = File::open(path.clone())
|
||||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?;
|
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?;
|
||||||
|
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
|
|
||||||
f.read_to_string(&mut contents)
|
f.read_to_string(&mut contents)
|
||||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))
|
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))
|
||||||
.and_then(|_| self.eval::<T>(&contents))
|
.and_then(|_| self.eval::<T>(&contents))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,14 +206,14 @@ impl<'e> Engine<'e> {
|
|||||||
|
|
||||||
/// Evaluate a file, but throw away the result and only return error (if any).
|
/// Evaluate a file, but throw away the result and only return error (if any).
|
||||||
/// Useful for when you don't need the result, but still need to keep track of possible errors.
|
/// Useful for when you don't need the result, but still need to keep track of possible errors.
|
||||||
pub fn consume_file(&mut self, filename: PathBuf) -> Result<(), EvalAltResult> {
|
pub fn consume_file(&mut self, path: PathBuf) -> Result<(), EvalAltResult> {
|
||||||
let mut f = File::open(filename.clone())
|
let mut f = File::open(path.clone())
|
||||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?;
|
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?;
|
||||||
|
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
|
|
||||||
f.read_to_string(&mut contents)
|
f.read_to_string(&mut contents)
|
||||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))
|
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))
|
||||||
.and_then(|_| self.consume(&contents))
|
.and_then(|_| self.consume(&contents))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,8 +124,8 @@ impl fmt::Display for EvalAltResult {
|
|||||||
}
|
}
|
||||||
Self::LoopBreak => write!(f, "{}", desc),
|
Self::LoopBreak => write!(f, "{}", desc),
|
||||||
Self::Return(_, pos) => write!(f, "{} ({})", desc, pos),
|
Self::Return(_, pos) => write!(f, "{} ({})", desc, pos),
|
||||||
Self::ErrorReadingScriptFile(filename, err) => {
|
Self::ErrorReadingScriptFile(path, err) => {
|
||||||
write!(f, "{} '{}': {}", desc, filename.display(), err)
|
write!(f, "{} '{}': {}", desc, path.display(), err)
|
||||||
}
|
}
|
||||||
Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p),
|
Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p),
|
||||||
Self::ErrorFunctionArgsMismatch(fun, need, n, pos) => write!(
|
Self::ErrorFunctionArgsMismatch(fun, need, n, pos) => write!(
|
||||||
|
Loading…
Reference in New Issue
Block a user