Merge branch 'master' into master
This commit is contained in:
commit
c7a6777b70
@ -167,7 +167,7 @@ use rhai::Engine;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
||||
let ast = engine.compile_file("hello_world.rhai")?;
|
||||
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.
|
||||
|
26
src/api.rs
26
src/api.rs
@ -12,6 +12,7 @@ use std::{
|
||||
any::{type_name, TypeId},
|
||||
fs::File,
|
||||
io::prelude::*,
|
||||
path::PathBuf,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
@ -104,9 +105,9 @@ impl<'e> Engine<'e> {
|
||||
parse(&mut tokens_stream.peekable(), self.optimize)
|
||||
}
|
||||
|
||||
fn read_file(filename: &str) -> Result<String, EvalAltResult> {
|
||||
let mut f = File::open(filename)
|
||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))?;
|
||||
fn read_file(path: PathBuf) -> Result<String, EvalAltResult> {
|
||||
let mut f = File::open(path.clone())
|
||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?;
|
||||
|
||||
let mut contents = String::new();
|
||||
|
||||
@ -116,14 +117,14 @@ impl<'e> Engine<'e> {
|
||||
}
|
||||
|
||||
/// Compile a file into an AST.
|
||||
pub fn compile_file(&self, filename: &str) -> Result<AST, EvalAltResult> {
|
||||
Self::read_file(filename)
|
||||
pub fn compile_file(&self, path: PathBuf) -> Result<AST, EvalAltResult> {
|
||||
Self::read_file(path)
|
||||
.and_then(|contents| self.compile(&contents).map_err(|err| err.into()))
|
||||
}
|
||||
|
||||
/// Evaluate a file.
|
||||
pub fn eval_file<T: Any + Clone>(&mut self, filename: &str) -> Result<T, EvalAltResult> {
|
||||
Self::read_file(filename).and_then(|contents| self.eval::<T>(&contents))
|
||||
pub fn eval_file<T: Any + Clone>(&mut self, path: PathBuf) -> Result<T, EvalAltResult> {
|
||||
Self::read_file(path).and_then(|contents| self.eval::<T>(&contents))
|
||||
}
|
||||
|
||||
/// Evaluate a string.
|
||||
@ -206,17 +207,10 @@ impl<'e> Engine<'e> {
|
||||
/// and not cleared from run to run.
|
||||
pub fn consume_file(
|
||||
&mut self,
|
||||
filename: &str,
|
||||
path: PathBuf,
|
||||
retain_functions: bool,
|
||||
) -> Result<(), EvalAltResult> {
|
||||
let mut f = File::open(filename)
|
||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))?;
|
||||
|
||||
let mut contents = String::new();
|
||||
|
||||
f.read_to_string(&mut contents)
|
||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))
|
||||
.and_then(|_| self.consume(&contents, retain_functions))
|
||||
Self::read_file(path).and_then(|_| self.consume(&contents, retain_functions))
|
||||
}
|
||||
|
||||
/// Evaluate a string, but throw away the result and only return error (if any).
|
||||
|
@ -29,7 +29,7 @@
|
||||
//!
|
||||
//! engine.register_fn("compute_something", compute_something);
|
||||
//!
|
||||
//! assert_eq!(engine.eval_file::<bool>("my_script.rhai")?, true);
|
||||
//! assert_eq!(engine.eval_file::<bool>("my_script.rhai".into())?, true);
|
||||
//!
|
||||
//! Ok(())
|
||||
//! }
|
||||
|
@ -4,7 +4,7 @@ use crate::any::Dynamic;
|
||||
use crate::error::ParseError;
|
||||
use crate::parser::{Position, INT};
|
||||
|
||||
use std::{error::Error, fmt};
|
||||
use std::{error::Error, fmt, path::PathBuf};
|
||||
|
||||
/// Evaluation result.
|
||||
///
|
||||
@ -45,7 +45,7 @@ pub enum EvalAltResult {
|
||||
/// Wrapped value is the type of the actual result.
|
||||
ErrorMismatchOutputType(String, Position),
|
||||
/// Error reading from a script file. Wrapped value is the path of the script file.
|
||||
ErrorReadingScriptFile(String, std::io::Error),
|
||||
ErrorReadingScriptFile(PathBuf, std::io::Error),
|
||||
/// Inappropriate member access.
|
||||
ErrorDotExpr(String, Position),
|
||||
/// Arithmetic error encountered. Wrapped value is the error message.
|
||||
@ -125,8 +125,8 @@ impl fmt::Display for EvalAltResult {
|
||||
}
|
||||
Self::LoopBreak => write!(f, "{}", desc),
|
||||
Self::Return(_, pos) => write!(f, "{} ({})", desc, pos),
|
||||
Self::ErrorReadingScriptFile(filename, err) => {
|
||||
write!(f, "{} '{}': {}", desc, filename, err)
|
||||
Self::ErrorReadingScriptFile(path, err) => {
|
||||
write!(f, "{} '{}': {}", desc, path.display(), err)
|
||||
}
|
||||
Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p),
|
||||
Self::ErrorFunctionArgsMismatch(fun, 0, n, pos) => write!(
|
||||
|
Loading…
Reference in New Issue
Block a user