diff --git a/README.md b/README.md index 6ce0ad15..57f3c463 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/api.rs b/src/api.rs index cf03125f..919e6460 100644 --- a/src/api.rs +++ b/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 { - let mut f = File::open(filename) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))?; + fn read_file(path: PathBuf) -> Result { + 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 { - Self::read_file(filename) + pub fn compile_file(&self, path: PathBuf) -> Result { + Self::read_file(path) .and_then(|contents| self.compile(&contents).map_err(|err| err.into())) } /// Evaluate a file. - pub fn eval_file(&mut self, filename: &str) -> Result { - Self::read_file(filename).and_then(|contents| self.eval::(&contents)) + pub fn eval_file(&mut self, path: PathBuf) -> Result { + Self::read_file(path).and_then(|contents| self.eval::(&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). diff --git a/src/lib.rs b/src/lib.rs index cc043e7e..0c4bb048 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ //! //! engine.register_fn("compute_something", compute_something); //! -//! assert_eq!(engine.eval_file::("my_script.rhai")?, true); +//! assert_eq!(engine.eval_file::("my_script.rhai".into())?, true); //! //! Ok(()) //! } diff --git a/src/result.rs b/src/result.rs index e0bb3734..c37a7a4a 100644 --- a/src/result.rs +++ b/src/result.rs @@ -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!(