Skip to main content

Interpreter

Struct Interpreter 

Source
pub struct Interpreter { /* private fields */ }
Expand description

The FiddlerScript interpreter.

Implementations§

Source§

impl Interpreter

Source

pub fn new() -> Self

Create a new interpreter with default built-in functions.

The interpreter is initialized with all current OS environment variables available as script variables.

Examples found in repository?
examples/run.rs (line 130)
129fn run_example(source: &str) {
130    let mut interpreter = Interpreter::new();
131    match interpreter.run(source) {
132        Ok(_) => {}
133        Err(e) => eprintln!("Error: {}", e),
134    }
135}
Source

pub fn with_builtins( builtins: HashMap<String, fn(Vec<Value>) -> Result<Value, RuntimeError>>, ) -> Self

Create a new interpreter with custom built-in functions.

The interpreter is initialized with all current OS environment variables available as script variables.

Source

pub fn new_without_env() -> Self

Create a new interpreter without loading environment variables.

Source

pub fn parse_only(&self, source: &str) -> Result<Program, FiddlerError>

Parse FiddlerScript source without executing it.

Identical to crate::parse but provided as a method for API symmetry with Interpreter::run.

Source

pub fn run(&mut self, source: &str) -> Result<Value, FiddlerError>

Run FiddlerScript source code.

Examples found in repository?
examples/run.rs (line 131)
129fn run_example(source: &str) {
130    let mut interpreter = Interpreter::new();
131    match interpreter.run(source) {
132        Ok(_) => {}
133        Err(e) => eprintln!("Error: {}", e),
134    }
135}
Source

pub fn execute(&mut self, program: &Program) -> Result<Value, RuntimeError>

Execute a parsed program.

Source

pub fn output(&self) -> &[String]

Get captured output (for testing).

Source

pub fn clear_output(&mut self)

Clear captured output.

Source

pub fn is_output_truncated(&self) -> bool

Check if captured output was truncated.

Source

pub fn set_variable_value(&mut self, name: impl Into<String>, value: Value)

Set a variable by name in the global scope.

This allows external code to inject values into the interpreter before running a script.

§Arguments
  • name - The variable name
  • value - The value to set
Source

pub fn set_variable_bytes(&mut self, name: impl Into<String>, bytes: Vec<u8>)

Set a variable from bytes.

Convenience method to set a variable with byte data.

§Arguments
  • name - The variable name
  • bytes - The byte data
Source

pub fn set_variable_string( &mut self, name: impl Into<String>, value: impl Into<String>, )

Set a variable from a string.

Convenience method to set a variable with string data.

§Arguments
  • name - The variable name
  • value - The string value
Source

pub fn set_variable_int(&mut self, name: impl Into<String>, value: i64)

Set a variable from an integer.

Convenience method to set a variable with an integer value.

§Arguments
  • name - The variable name
  • value - The integer value
Source

pub fn set_variable_array( &mut self, name: impl Into<String>, values: Vec<Value>, )

Set a variable as an array.

Convenience method to set a variable with an array value.

§Arguments
  • name - The variable name
  • values - The array values
Source

pub fn set_variable_dict( &mut self, name: impl Into<String>, values: IndexMap<String, Value>, )

Set a variable as a dictionary.

Convenience method to set a variable with a dictionary value.

§Arguments
  • name - The variable name
  • values - The dictionary values
Source

pub fn get_value(&self, name: &str) -> Option<Value>

Get a variable by name, returning the Value type.

Searches all scopes from innermost to outermost.

§Arguments
  • name - The variable name
§Returns
  • Some(Value) if the variable exists
  • None if the variable is not defined
Source

pub fn get_bytes(&self, name: &str) -> Option<Vec<u8>>

Get a variable by name as bytes.

Converts the variable value to its byte representation.

§Arguments
  • name - The variable name
§Returns
  • Some(Vec<u8>) if the variable exists
  • None if the variable is not defined
Source

pub fn has_variable(&self, name: &str) -> bool

Check if a variable exists.

§Arguments
  • name - The variable name
§Returns
  • true if the variable exists in any scope
  • false otherwise

Trait Implementations§

Source§

impl Default for Interpreter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.