Variables

Struct Variables 

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

Manages PowerShell variables across different scopes.

This structure handles variable storage, retrieval, and scope management for PowerShell scripts. It supports loading variables from environment variables, INI files, and manual assignment.

§Examples

use ps_parser::{Variables, PowerShellSession};
use std::path::Path;

// Load environment variables
let env_vars = Variables::env();
let mut session = PowerShellSession::new().with_variables(env_vars);

// Load from INI string
let ini_vars = Variables::from_ini_string("[global]\nname = John Doe\n[local]\nlocal_var = \"local_value\"").unwrap();
let mut session2 = PowerShellSession::new().with_variables(ini_vars);

// Create empty and add manually
let mut vars = Variables::new();
// ... add variables manually

Implementations§

Source§

impl Variables

Source

pub fn set_status(&mut self, b: bool)

Source

pub fn status(&mut self) -> bool

Source

pub fn load_from_file(&mut self, path: &Path) -> Result<(), Box<dyn Error>>

Source

pub fn load_from_string( &mut self, ini_string: &str, ) -> Result<(), Box<dyn Error>>

Source

pub fn new() -> Variables

Creates a new empty Variables container.

§Arguments
  • initializes the container with PowerShell built-in variables like $true, $false, $null, and $?. If false,
§Returns

A new Variables instance.

§Examples
use ps_parser::Variables;

// Create with built-in variables
let vars_with_builtins = Variables::new();

// Create empty
let empty_vars = Variables::new();
Source

pub fn force_eval() -> Self

Creates a new Variables container with forced evaluation enabled.

This constructor creates a Variables instance that will return Val::Null for undefined variables instead of returning None. This is useful for PowerShell script evaluation where undefined variables should be treated as $null rather than causing errors.

§Returns

A new Variables instance with forced evaluation enabled and built-in variables initialized.

§Examples
use ps_parser::{Variables, PowerShellSession};

// Create with forced evaluation
let vars = Variables::force_eval();
let mut session = PowerShellSession::new().with_variables(vars);

// Undefined variables will evaluate to $null instead of causing errors
let result = session.safe_eval("$undefined_variable").unwrap();
assert_eq!(result, "");  // $null displays as empty string
§Behavior Difference
  • Variables::new(): Returns None for undefined variables
  • Variables::force_eval(): Returns Val::Null for undefined variables

This is particularly useful when parsing PowerShell scripts that may reference variables that haven’t been explicitly defined, allowing the script to continue execution rather than failing.

Source

pub fn env() -> Variables

Loads all environment variables into a Variables container.

This method reads all environment variables from the system and stores them in the env scope, making them accessible as $env:VARIABLE_NAME in PowerShell scripts.

§Returns

A new Variables instance containing all environment variables.

§Examples
use ps_parser::{Variables, PowerShellSession};

let env_vars = Variables::env();
let mut session = PowerShellSession::new().with_variables(env_vars);

// Access environment variables
let path = session.safe_eval("$env:PATH").unwrap();
let username = session.safe_eval("$env:USERNAME").unwrap();
Source

pub fn from_ini_string(ini_string: &str) -> Result<Self, Box<dyn Error>>

Loads variables from an INI configuration file.

This method parses an INI file and loads its key-value pairs as PowerShell variables. Variables are organized by INI sections, with the [global] section creating global variables and other sections creating scoped variables.

§Arguments
  • path - A reference to the path of the INI file to load.
§Returns
  • Result<Variables, VariableError> - A Variables instance with the loaded data, or an error if the file cannot be read or parsed.
§Examples
use ps_parser::{Variables, PowerShellSession};
use std::path::Path;

// Load from INI file
let variables = Variables::from_ini_string("[global]\nname = John Doe\n[local]\nlocal_var = \"local_value\"").unwrap();
let mut session = PowerShellSession::new().with_variables(variables);

// Access loaded variables
let name = session.safe_eval("$global:name").unwrap();
let local_var = session.safe_eval("$local:local_var").unwrap();
§INI Format
# Global variables (accessible as $global:key)
[global]
name = John Doe
version = 1.0

# Local scope variables (accessible as $local:key)
[local]
temp_dir = /tmp
debug = true
Source

pub fn from_ini_file(path: &Path) -> Result<Self, Box<dyn Error>>

Create a new Variables instance with variables loaded from an INI file

Trait Implementations§

Source§

impl Clone for Variables

Source§

fn clone(&self) -> Variables

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Variables

Source§

fn default() -> Variables

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T