rhai/src/api/files.rs

256 lines
8.1 KiB
Rust
Raw Normal View History

2021-11-20 07:57:21 +01:00
//! Module that defines the public file-based API of [`Engine`].
#![cfg(not(feature = "no_std"))]
2022-01-12 01:12:28 +01:00
#![cfg(not(target_family = "wasm"))]
2021-11-20 07:57:21 +01:00
use crate::types::dynamic::Variant;
2021-12-27 05:27:31 +01:00
use crate::{Engine, RhaiResultOf, Scope, AST, ERR};
2021-11-20 07:57:21 +01:00
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
2022-04-13 04:35:10 +02:00
use std::{fs::File, io::Read, path::PathBuf};
2021-11-20 07:57:21 +01:00
impl Engine {
/// Read the contents of a file into a string.
2022-04-13 04:35:10 +02:00
fn read_file(path: PathBuf) -> RhaiResultOf<String> {
let mut f = File::open(path.clone()).map_err(|err| {
2021-12-27 05:27:31 +01:00
ERR::ErrorSystem(
2021-11-20 07:57:21 +01:00
format!("Cannot open script file '{}'", path.to_string_lossy()),
err.into(),
)
})?;
let mut contents = String::new();
f.read_to_string(&mut contents).map_err(|err| {
2021-12-27 05:27:31 +01:00
ERR::ErrorSystem(
2021-11-20 07:57:21 +01:00
format!("Cannot read script file '{}'", path.to_string_lossy()),
err.into(),
)
})?;
if contents.starts_with("#!") {
// Remove shebang
if let Some(n) = contents.find('\n') {
contents.drain(0..n).count();
} else {
contents.clear();
}
};
Ok(contents)
}
/// Compile a script file into an [`AST`], which can be used later for evaluation.
///
/// Not available under `no_std` or `WASM`.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::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())?;
///
/// for _ in 0..42 {
/// engine.eval_ast::<i64>(&ast)?;
/// }
/// # Ok(())
/// # }
/// ```
#[inline(always)]
2022-04-13 04:35:10 +02:00
pub fn compile_file(&self, path: PathBuf) -> RhaiResultOf<AST> {
2021-11-20 07:57:21 +01:00
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.
///
/// Not available under `no_std` or `WASM`.
///
/// ## Constants Propagation
///
2021-11-20 14:29:36 +01:00
/// If not [`OptimizationLevel::None`][crate::OptimizationLevel::None], constants defined within
2022-02-24 03:36:20 +01:00
/// the scope are propagated throughout the script _including_ functions.
///
/// This allows functions to be optimized based on dynamic global constants.
2021-11-20 07:57:21 +01:00
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// # #[cfg(not(feature = "no_optimize"))]
/// # {
/// use rhai::{Engine, Scope, OptimizationLevel};
///
/// let mut engine = Engine::new();
///
/// // Create initialized scope
/// let mut scope = Scope::new();
/// 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 result = engine.eval_ast::<i64>(&ast)?;
/// # }
/// # Ok(())
/// # }
/// ```
#[inline]
2022-04-13 04:35:10 +02:00
pub fn compile_file_with_scope(&self, scope: &Scope, path: PathBuf) -> RhaiResultOf<AST> {
2021-11-20 07:57:21 +01:00
Self::read_file(path).and_then(|contents| Ok(self.compile_with_scope(scope, &contents)?))
}
2022-08-08 03:10:15 +02:00
/// Evaluate a script file, returning the result value or an error.
2021-11-20 07:57:21 +01:00
///
/// Not available under `no_std` or `WASM`.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::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())?;
/// # Ok(())
/// # }
/// ```
#[inline]
2022-04-13 04:35:10 +02:00
pub fn eval_file<T: Variant + Clone>(&self, path: PathBuf) -> RhaiResultOf<T> {
2021-11-20 07:57:21 +01:00
Self::read_file(path).and_then(|contents| self.eval::<T>(&contents))
}
2022-08-08 03:10:15 +02:00
/// Evaluate a script file with own scope, returning the result value or an error.
2021-11-20 07:57:21 +01:00
///
/// Not available under `no_std` or `WASM`.
///
/// ## Constants Propagation
///
2021-11-20 14:29:36 +01:00
/// If not [`OptimizationLevel::None`][crate::OptimizationLevel::None], constants defined within
2022-02-24 03:36:20 +01:00
/// the scope are propagated throughout the script _including_ functions.
///
/// This allows functions to be optimized based on dynamic global constants.
2021-11-20 07:57:21 +01:00
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Scope};
///
/// let engine = Engine::new();
///
/// // Create initialized scope
/// let mut scope = Scope::new();
/// 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())?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn eval_file_with_scope<T: Variant + Clone>(
&self,
scope: &mut Scope,
2022-04-13 04:35:10 +02:00
path: PathBuf,
2021-12-25 16:49:14 +01:00
) -> RhaiResultOf<T> {
2021-11-27 16:04:45 +01:00
Self::read_file(path).and_then(|contents| self.eval_with_scope(scope, &contents))
2021-11-20 07:57:21 +01:00
}
2022-08-08 03:10:15 +02:00
/// Evaluate a file.
2021-11-20 07:57:21 +01:00
///
/// Not available under `no_std` or `WASM`.
2022-08-08 03:10:15 +02:00
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::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())?;
/// # Ok(())
/// # }
/// ```
2021-11-20 07:57:21 +01:00
#[inline]
2022-04-13 04:35:10 +02:00
pub fn run_file(&self, path: PathBuf) -> RhaiResultOf<()> {
2021-11-20 07:57:21 +01:00
Self::read_file(path).and_then(|contents| self.run(&contents))
}
2022-08-08 03:10:15 +02:00
/// Evaluate a file with own scope.
2021-11-20 07:57:21 +01:00
///
/// Not available under `no_std` or `WASM`.
///
/// ## Constants Propagation
///
2021-11-20 14:29:36 +01:00
/// If not [`OptimizationLevel::None`][crate::OptimizationLevel::None], constants defined within
2022-02-24 03:36:20 +01:00
/// the scope are propagated throughout the script _including_ functions.
///
/// This allows functions to be optimized based on dynamic global constants.
2022-08-08 03:10:15 +02:00
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::{Engine, Scope};
///
/// let engine = Engine::new();
///
/// // Create initialized scope
/// 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())?;
/// # Ok(())
/// # }
/// ```
2021-11-20 07:57:21 +01:00
#[inline]
2022-04-13 04:35:10 +02:00
pub fn run_file_with_scope(&self, scope: &mut Scope, path: PathBuf) -> RhaiResultOf<()> {
2021-11-20 07:57:21 +01:00
Self::read_file(path).and_then(|contents| self.run_with_scope(scope, &contents))
}
}
2022-08-08 03:10:15 +02:00
/// Evaluate a script file.
///
/// Not available under `no_std` or `WASM`.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// let result: i64 = rhai::eval_file("script.rhai".into())?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn eval_file<T: Variant + Clone>(path: PathBuf) -> RhaiResultOf<T> {
Engine::read_file(path).and_then(|contents| Engine::new().eval::<T>(&contents))
}
/// Evaluate a file.
///
/// Not available under `no_std` or `WASM`.
///
/// # Example
///
/// ```no_run
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
/// use rhai::Engine;
///
/// let engine = Engine::new();
///
/// // Notice that a PathBuf is required which can easily be constructed from a string.
/// rhai::run_file("script.rhai".into())?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn run_file(path: PathBuf) -> RhaiResultOf<()> {
Engine::read_file(path).and_then(|contents| Engine::new().run(&contents))
}