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
impl Variables
pub fn set_status(&mut self, b: bool)
pub fn status(&mut self) -> bool
pub fn load_from_file(&mut self, path: &Path) -> Result<(), Box<dyn Error>>
pub fn load_from_string( &mut self, ini_string: &str, ) -> Result<(), Box<dyn Error>>
Sourcepub fn new() -> Variables
pub fn new() -> Variables
Creates a new empty Variables container.
§Arguments
- initializes the container with PowerShell built-in variables like
$true
,$false
,$null
, and$?
. Iffalse
,
§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();
Sourcepub fn force_eval() -> Self
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()
: ReturnsNone
for undefined variablesVariables::force_eval()
: ReturnsVal::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.
Sourcepub fn env() -> Variables
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();
Sourcepub fn from_ini_string(ini_string: &str) -> Result<Self, Box<dyn Error>>
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
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Variables
impl !RefUnwindSafe for Variables
impl !Send for Variables
impl !Sync for Variables
impl Unpin for Variables
impl !UnwindSafe for Variables
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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