pub struct Scope { /* private fields */ }Expand description
Variable scope with nested frames and last-result tracking.
Variables are looked up from innermost to outermost frame.
The ? variable always refers to the last command result.
The frames field is wrapped in Arc for copy-on-write (COW) semantics.
Cloning a Scope is O(1) — just bumps the Arc refcount. Mutations use
Arc::make_mut to clone the inner data only when shared. This matters
because execute_pipeline snapshots the scope into ExecContext (clone)
and syncs it back (clone) on every command.
Implementations§
Source§impl Scope
impl Scope
Sourcepub fn push_frame(&mut self)
pub fn push_frame(&mut self)
Push a new scope frame (for entering a loop, tool call, etc.)
Sourcepub fn pop_frame(&mut self)
pub fn pop_frame(&mut self)
Pop the innermost scope frame.
Panics if attempting to pop the last frame.
Sourcepub fn set(&mut self, name: impl Into<String>, value: Value)
pub fn set(&mut self, name: impl Into<String>, value: Value)
Set a variable in the current (innermost) frame.
Use this for local variable declarations.
Sourcepub fn set_global(&mut self, name: impl Into<String>, value: Value)
pub fn set_global(&mut self, name: impl Into<String>, value: Value)
Set a variable with global semantics (shell default).
If the variable exists in any frame, update it there. Otherwise, create it in the outermost (root) frame. Use this for non-local variable assignments.
Sourcepub fn get(&self, name: &str) -> Option<&Value>
pub fn get(&self, name: &str) -> Option<&Value>
Get a variable by name, searching from innermost to outermost frame.
Sourcepub fn remove(&mut self, name: &str) -> Option<Value>
pub fn remove(&mut self, name: &str) -> Option<Value>
Remove a variable, searching from innermost to outermost frame.
Returns the removed value if found, None otherwise.
Sourcepub fn set_last_result(&mut self, result: ExecResult)
pub fn set_last_result(&mut self, result: ExecResult)
Set the last command result (accessible via $?).
Sourcepub fn last_result(&self) -> &ExecResult
pub fn last_result(&self) -> &ExecResult
Get the last command result.
Sourcepub fn set_positional(
&mut self,
script_name: impl Into<String>,
args: Vec<String>,
)
pub fn set_positional( &mut self, script_name: impl Into<String>, args: Vec<String>, )
Set the positional parameters ($0, $1-$9, $@, $#).
The script_name becomes $0, and args become $1, $2, etc.
Sourcepub fn save_positional(&self) -> (String, Vec<String>)
pub fn save_positional(&self) -> (String, Vec<String>)
Save current positional parameters for later restoration.
Returns (script_name, args) tuple that can be passed to set_positional.
Sourcepub fn get_positional(&self, n: usize) -> Option<&str>
pub fn get_positional(&self, n: usize) -> Option<&str>
Get a positional parameter by index ($0-$9).
$0 returns the script name, $1-$9 return arguments.
Sourcepub fn error_exit_enabled(&self) -> bool
pub fn error_exit_enabled(&self) -> bool
Check if error-exit mode is enabled (set -e).
Sourcepub fn set_error_exit(&mut self, enabled: bool)
pub fn set_error_exit(&mut self, enabled: bool)
Set error-exit mode (set -e / set +e).
Sourcepub fn export(&mut self, name: impl Into<String>)
pub fn export(&mut self, name: impl Into<String>)
Mark a variable as exported (visible to child processes).
The variable doesn’t need to exist yet; it will be exported when set.
Sourcepub fn is_exported(&self, name: &str) -> bool
pub fn is_exported(&self, name: &str) -> bool
Check if a variable is marked for export.
Sourcepub fn set_exported(&mut self, name: impl Into<String>, value: Value)
pub fn set_exported(&mut self, name: impl Into<String>, value: Value)
Set a variable and mark it as exported.
Sourcepub fn exported_vars(&self) -> Vec<(String, Value)>
pub fn exported_vars(&self) -> Vec<(String, Value)>
Get all exported variables with their values.
Only returns variables that exist and are marked for export.
Sourcepub fn exported_names(&self) -> Vec<&str>
pub fn exported_names(&self) -> Vec<&str>
Get all exported variable names.
Sourcepub fn resolve_path(&self, path: &VarPath) -> Option<Value>
pub fn resolve_path(&self, path: &VarPath) -> Option<Value>
Resolve a variable path like ${VAR} or ${?.field}.
Returns None if the path cannot be resolved.
Field access is only supported for the special $? variable.