From 8c1c37666d8856cbd3e26d86f447e91d93b0b5a0 Mon Sep 17 00:00:00 2001 From: Jarkko Kuukkanen Date: Fri, 13 Mar 2020 10:17:55 +0200 Subject: [PATCH 1/2] Make every file evaluation use PathBuf instead of str --- src/api.rs | 25 +++++++++++++------------ src/lib.rs | 2 +- src/result.rs | 6 +++--- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/api.rs b/src/api.rs index 5e89c5ca..29ef1472 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, }; @@ -105,26 +106,26 @@ impl<'e> Engine<'e> { } /// Compile a file into an AST. - pub fn compile_file(&self, filename: &str) -> Result { - let mut f = File::open(filename) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))?; + pub fn compile_file(&self, filename: PathBuf) -> Result { + let mut f = File::open(filename.clone()) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?; let mut contents = String::new(); f.read_to_string(&mut contents) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err)) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err)) .and_then(|_| self.compile(&contents).map_err(EvalAltResult::ErrorParsing)) } /// Evaluate a file. - pub fn eval_file(&mut self, filename: &str) -> Result { - let mut f = File::open(filename) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))?; + pub fn eval_file(&mut self, filename: PathBuf) -> Result { + let mut f = File::open(filename.clone()) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?; let mut contents = String::new(); f.read_to_string(&mut contents) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err)) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err)) .and_then(|_| self.eval::(&contents)) } @@ -205,14 +206,14 @@ impl<'e> Engine<'e> { /// 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. - pub fn consume_file(&mut self, filename: &str) -> Result<(), EvalAltResult> { - let mut f = File::open(filename) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err))?; + pub fn consume_file(&mut self, filename: PathBuf) -> Result<(), EvalAltResult> { + let mut f = File::open(filename.clone()) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?; let mut contents = String::new(); f.read_to_string(&mut contents) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.into(), err)) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err)) .and_then(|_| self.consume(&contents)) } diff --git a/src/lib.rs b/src/lib.rs index 56df1ae2..1472657f 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 224c8232..a90abce1 100644 --- a/src/result.rs +++ b/src/result.rs @@ -3,7 +3,7 @@ use crate::any::Dynamic; use crate::error::ParseError; use crate::parser::Position; -use std::{error::Error, fmt}; +use std::{error::Error, fmt, path::PathBuf}; /// Evaluation result. /// @@ -44,7 +44,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,7 +125,7 @@ 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) + write!(f, "{} '{}': {}", desc, filename.display(), err) } Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p), Self::ErrorFunctionArgsMismatch(fun, need, n, pos) => write!( From 6c72e3c48bbf8f200fefc0b3682f9769fadfc90c Mon Sep 17 00:00:00 2001 From: Jarkko Kuukkanen Date: Fri, 13 Mar 2020 12:07:51 +0200 Subject: [PATCH 2/2] Change filename to path for clarity --- README.md | 2 +- src/api.rs | 24 ++++++++++++------------ src/result.rs | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 71f33028..16e40b54 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ use rhai::Engine; 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. diff --git a/src/api.rs b/src/api.rs index 29ef1472..d3fc6402 100644 --- a/src/api.rs +++ b/src/api.rs @@ -106,26 +106,26 @@ impl<'e> Engine<'e> { } /// Compile a file into an AST. - pub fn compile_file(&self, filename: PathBuf) -> Result { - let mut f = File::open(filename.clone()) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?; + pub fn compile_file(&self, path: PathBuf) -> Result { + let mut f = File::open(path.clone()) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?; let mut contents = String::new(); 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)) } /// Evaluate a file. - pub fn eval_file(&mut self, filename: PathBuf) -> Result { - let mut f = File::open(filename.clone()) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?; + pub fn eval_file(&mut self, path: PathBuf) -> Result { + let mut f = File::open(path.clone()) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?; let mut contents = String::new(); 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::(&contents)) } @@ -206,14 +206,14 @@ impl<'e> Engine<'e> { /// 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. - pub fn consume_file(&mut self, filename: PathBuf) -> Result<(), EvalAltResult> { - let mut f = File::open(filename.clone()) - .map_err(|err| EvalAltResult::ErrorReadingScriptFile(filename.clone(), err))?; + pub fn consume_file(&mut self, path: PathBuf) -> Result<(), EvalAltResult> { + let mut f = File::open(path.clone()) + .map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?; let mut contents = String::new(); 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)) } diff --git a/src/result.rs b/src/result.rs index a90abce1..16569a48 100644 --- a/src/result.rs +++ b/src/result.rs @@ -124,8 +124,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.display(), err) + Self::ErrorReadingScriptFile(path, err) => { + write!(f, "{} '{}': {}", desc, path.display(), err) } Self::ErrorParsing(p) => write!(f, "Syntax error: {}", p), Self::ErrorFunctionArgsMismatch(fun, need, n, pos) => write!(