Added no_std support
This commit is contained in:
17
src/api.rs
17
src/api.rs
@@ -12,13 +12,15 @@ use crate::scope::Scope;
|
||||
#[cfg(not(feature = "no_optimize"))]
|
||||
use crate::optimize::optimize_ast;
|
||||
|
||||
use std::{
|
||||
use crate::stdlib::{
|
||||
any::{type_name, TypeId},
|
||||
fs::File,
|
||||
io::prelude::*,
|
||||
path::PathBuf,
|
||||
boxed::Box,
|
||||
string::{String, ToString},
|
||||
sync::Arc,
|
||||
vec::Vec,
|
||||
};
|
||||
#[cfg(not(feature = "no_stdlib"))]
|
||||
use crate::stdlib::{fs::File, io::prelude::*, path::PathBuf};
|
||||
|
||||
impl<'e> Engine<'e> {
|
||||
pub(crate) fn register_fn_raw(
|
||||
@@ -115,6 +117,7 @@ impl<'e> Engine<'e> {
|
||||
parse(&mut tokens_stream.peekable(), self, scope)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_stdlib"))]
|
||||
fn read_file(path: PathBuf) -> Result<String, EvalAltResult> {
|
||||
let mut f = File::open(path.clone())
|
||||
.map_err(|err| EvalAltResult::ErrorReadingScriptFile(path.clone(), err))?;
|
||||
@@ -127,12 +130,14 @@ impl<'e> Engine<'e> {
|
||||
}
|
||||
|
||||
/// Compile a file into an AST.
|
||||
#[cfg(not(feature = "no_stdlib"))]
|
||||
pub fn compile_file(&self, path: PathBuf) -> Result<AST, EvalAltResult> {
|
||||
self.compile_file_with_scope(&Scope::new(), path)
|
||||
}
|
||||
|
||||
/// Compile a file into an AST using own scope.
|
||||
/// The scope is useful for passing constants into the script for optimization.
|
||||
#[cfg(not(feature = "no_stdlib"))]
|
||||
pub fn compile_file_with_scope(
|
||||
&self,
|
||||
scope: &Scope,
|
||||
@@ -145,11 +150,13 @@ impl<'e> Engine<'e> {
|
||||
}
|
||||
|
||||
/// Evaluate a file.
|
||||
#[cfg(not(feature = "no_stdlib"))]
|
||||
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 file with own scope.
|
||||
#[cfg(not(feature = "no_stdlib"))]
|
||||
pub fn eval_file_with_scope<T: Any + Clone>(
|
||||
&mut self,
|
||||
scope: &mut Scope,
|
||||
@@ -234,6 +241,7 @@ impl<'e> Engine<'e> {
|
||||
///
|
||||
/// Note - if `retain_functions` is set to `true`, functions defined by previous scripts are _retained_
|
||||
/// and not cleared from run to run.
|
||||
#[cfg(not(feature = "no_stdlib"))]
|
||||
pub fn consume_file(
|
||||
&mut self,
|
||||
retain_functions: bool,
|
||||
@@ -247,6 +255,7 @@ impl<'e> Engine<'e> {
|
||||
///
|
||||
/// Note - if `retain_functions` is set to `true`, functions defined by previous scripts are _retained_
|
||||
/// and not cleared from run to run.
|
||||
#[cfg(not(feature = "no_stdlib"))]
|
||||
pub fn consume_file_with_scope(
|
||||
&mut self,
|
||||
scope: &mut Scope,
|
||||
|
Reference in New Issue
Block a user