Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace PathBuf to AsRef<Path> in the Engine file API #693

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/event_handler_js/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn main() {
pub fn main() {
use rhai::{CallFnOptions, Dynamic, Engine, Map, Scope, AST};
use std::io::{stdin, stdout, Write};
use std::path::Path;

const SCRIPT_FILE: &str = "event_handler_js/script.rhai";

Expand Down Expand Up @@ -80,7 +81,7 @@ pub fn main() {
// Compile the handler script.
println!("> Loading script file: {path}");

let ast = match engine.compile_file_with_scope(&mut scope, path.into()) {
let ast = match engine.compile_file_with_scope(&mut scope, path) {
Ok(ast) => ast,
Err(err) => {
eprintln!("! Error: {err}");
Expand Down
3 changes: 2 additions & 1 deletion examples/event_handler_main/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn main() {
pub fn main() {
use rhai::{CallFnOptions, Dynamic, Engine, Scope, AST};
use std::io::{stdin, stdout, Write};
use std::path::Path;

const SCRIPT_FILE: &str = "event_handler_main/script.rhai";

Expand Down Expand Up @@ -69,7 +70,7 @@ pub fn main() {
// Compile the handler script.
println!("> Loading script file: {path}");

let ast = match engine.compile_file_with_scope(&mut scope, path.into()) {
let ast = match engine.compile_file_with_scope(&mut scope, &Path::new(path)) {
Ok(ast) => ast,
Err(err) => {
eprintln!("! Error: {}", err);
Expand Down
3 changes: 2 additions & 1 deletion examples/event_handler_map/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn main() {
pub fn main() {
use rhai::{Dynamic, Engine, Map, Scope, AST};
use std::io::{stdin, stdout, Write};
use std::path::Path;

const SCRIPT_FILE: &str = "event_handler_map/script.rhai";

Expand Down Expand Up @@ -83,7 +84,7 @@ pub fn main() {
// Compile the handler script.
println!("> Loading script file: {path}");

let ast = match engine.compile_file_with_scope(&mut scope, path.into()) {
let ast = match engine.compile_file_with_scope(&mut scope, &Path::new(path)) {
Ok(ast) => ast,
Err(err) => {
eprintln!("! Error: {err}");
Expand Down
31 changes: 14 additions & 17 deletions src/api/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::prelude::v1::*;
use std::{
fs::File,
io::Read,
path::{Path, PathBuf},
path::Path,
};

impl Engine {
Expand Down Expand Up @@ -57,8 +57,7 @@ impl Engine {
/// let engine = Engine::new();
///
/// // Compile a script file to an AST and store it for later evaluation.
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// let ast = engine.compile_file("script.rhai".into())?;
/// let ast = engine.compile_file("script.rhai")?;
///
/// for _ in 0..42 {
/// engine.eval_ast::<i64>(&ast)?;
Expand All @@ -67,7 +66,7 @@ impl Engine {
/// # }
/// ```
#[inline(always)]
pub fn compile_file(&self, path: PathBuf) -> RhaiResultOf<AST> {
pub fn compile_file(&self, path: impl AsRef<Path>) -> RhaiResultOf<AST> {
self.compile_file_with_scope(&Scope::new(), path)
}
/// Compile a script file into an [`AST`] using own scope, which can be used later for evaluation.
Expand Down Expand Up @@ -96,19 +95,18 @@ impl Engine {
/// scope.push_constant("x", 42_i64); // 'x' is a constant
///
/// // Compile a script to an AST and store it for later evaluation.
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// let ast = engine.compile_file_with_scope(&mut scope, "script.rhai".into())?;
/// let ast = engine.compile_file_with_scope(&mut scope, "script.rhai")?;
///
/// let result = engine.eval_ast::<i64>(&ast)?;
/// # }
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn compile_file_with_scope(&self, scope: &Scope, path: PathBuf) -> RhaiResultOf<AST> {
pub fn compile_file_with_scope(&self, scope: &Scope, path: impl AsRef<Path>) -> RhaiResultOf<AST> {
Self::read_file(&path).and_then(|contents| {
let mut ast = self.compile_with_scope(scope, contents)?;
ast.set_source(path.to_string_lossy().as_ref());
ast.set_source(path.as_ref().to_string_lossy().as_ref());
Ok(ast)
})
}
Expand All @@ -125,12 +123,12 @@ impl Engine {
/// let engine = Engine::new();
///
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// let result = engine.eval_file::<i64>("script.rhai".into())?;
/// let result = engine.eval_file::<i64>("script.rhai")?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn eval_file<T: Variant + Clone>(&self, path: PathBuf) -> RhaiResultOf<T> {
pub fn eval_file<T: Variant + Clone>(&self, path: impl AsRef<Path>) -> RhaiResultOf<T> {
Self::read_file(path).and_then(|contents| self.eval::<T>(&contents))
}
/// Evaluate a script file with own scope, returning the result value or an error.
Expand All @@ -157,15 +155,15 @@ impl Engine {
/// scope.push("x", 42_i64);
///
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// let result = engine.eval_file_with_scope::<i64>(&mut scope, "script.rhai".into())?;
/// let result = engine.eval_file_with_scope::<i64>(&mut scope, "script.rhai")?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn eval_file_with_scope<T: Variant + Clone>(
&self,
scope: &mut Scope,
path: PathBuf,
path: impl AsRef<Path>,
) -> RhaiResultOf<T> {
Self::read_file(path).and_then(|contents| self.eval_with_scope(scope, &contents))
}
Expand All @@ -182,12 +180,12 @@ impl Engine {
/// let engine = Engine::new();
///
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// engine.run_file("script.rhai".into())?;
/// engine.run_file("script.rhai")?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn run_file(&self, path: PathBuf) -> RhaiResultOf<()> {
pub fn run_file(&self, path: impl AsRef<Path>) -> RhaiResultOf<()> {
Self::read_file(path).and_then(|contents| self.run(&contents))
}
/// Evaluate a file with own scope.
Expand All @@ -213,13 +211,12 @@ impl Engine {
/// let mut scope = Scope::new();
/// scope.push("x", 42_i64);
///
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// engine.run_file_with_scope(&mut scope, "script.rhai".into())?;
/// engine.run_file_with_scope(&mut scope, "script.rhai")?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn run_file_with_scope(&self, scope: &mut Scope, path: PathBuf) -> RhaiResultOf<()> {
pub fn run_file_with_scope(&self, scope: &mut Scope, path: impl AsRef<Path>) -> RhaiResultOf<()> {
Self::read_file(path).and_then(|contents| self.run_with_scope(scope, &contents))
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//!
//! ```no_run
//! use rhai::{Engine, EvalAltResult};
//! use std::path::Path;
//!
//! fn main() -> Result<(), Box<EvalAltResult>>
//! {
Expand All @@ -44,7 +45,7 @@
//! # #[cfg(not(target_family = "wasm"))]
//! #
//! // Evaluate the script, expecting a 'bool' result
//! let result: bool = engine.eval_file("my_script.rhai".into())?;
//! let result: bool = engine.eval_file(Path::new("my_script.rhai"))?;
//!
//! assert_eq!(result, true);
//!
Expand Down
8 changes: 8 additions & 0 deletions src/types/immutable_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
iter::FromIterator,
ops::{Add, AddAssign, Deref, Sub, SubAssign},
str::FromStr,
path::Path,
};

/// The system immutable string type.
Expand Down Expand Up @@ -142,6 +143,13 @@ impl From<ImmutableString> for SmartString {
std::mem::take(shared_make_mut(&mut value.0))
}
}
impl From<&Path> for ImmutableString {
#[inline(always)]
fn from(value: &Path) -> Self {
let value: SmartString = value.to_str().expect("UTF-8 encoding is expected").into();
Self(value.into())
}
}

impl FromStr for ImmutableString {
type Err = ();
Expand Down