use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use crate::ast::{Value, VarPath, VarSegment};
use super::result::ExecResult;
#[derive(Debug, Clone)]
pub struct Scope {
frames: Arc<Vec<HashMap<String, Value>>>,
exported: HashSet<String>,
last_result: ExecResult,
script_name: String,
positional: Vec<String>,
error_exit: bool,
errexit_suppressed: usize,
show_ast: bool,
latch_enabled: bool,
trash_enabled: bool,
trash_max_size: u64,
glob_enabled: bool,
pid: u64,
}
impl Scope {
pub fn new() -> Self {
Self {
frames: Arc::new(vec![HashMap::new()]),
exported: HashSet::new(),
last_result: ExecResult::default(),
script_name: String::new(),
positional: Vec::new(),
error_exit: false,
errexit_suppressed: 0,
show_ast: false,
latch_enabled: false,
trash_enabled: false,
trash_max_size: 10 * 1024 * 1024, glob_enabled: true,
pid: 0,
}
}
pub fn pid(&self) -> u64 {
self.pid
}
pub fn set_pid(&mut self, pid: u64) {
self.pid = pid;
}
pub fn push_frame(&mut self) {
Arc::make_mut(&mut self.frames).push(HashMap::new());
}
pub fn pop_frame(&mut self) {
if self.frames.len() > 1 {
Arc::make_mut(&mut self.frames).pop();
} else {
panic!("cannot pop the root scope frame");
}
}
pub fn set(&mut self, name: impl Into<String>, value: Value) {
if let Some(frame) = Arc::make_mut(&mut self.frames).last_mut() {
frame.insert(name.into(), value);
}
}
pub fn set_global(&mut self, name: impl Into<String>, value: Value) {
let name = name.into();
let frames = Arc::make_mut(&mut self.frames);
for frame in frames.iter_mut().rev() {
if let std::collections::hash_map::Entry::Occupied(mut e) = frame.entry(name.clone()) {
e.insert(value);
return;
}
}
if let Some(frame) = frames.first_mut() {
frame.insert(name, value);
}
}
pub fn get(&self, name: &str) -> Option<&Value> {
for frame in self.frames.iter().rev() {
if let Some(value) = frame.get(name) {
return Some(value);
}
}
None
}
pub fn remove(&mut self, name: &str) -> Option<Value> {
for frame in Arc::make_mut(&mut self.frames).iter_mut().rev() {
if let Some(value) = frame.remove(name) {
return Some(value);
}
}
None
}
pub fn set_last_result(&mut self, result: ExecResult) {
self.last_result = result;
}
pub fn last_result(&self) -> &ExecResult {
&self.last_result
}
pub fn set_positional(&mut self, script_name: impl Into<String>, args: Vec<String>) {
self.script_name = script_name.into();
self.positional = args;
}
pub fn save_positional(&self) -> (String, Vec<String>) {
(self.script_name.clone(), self.positional.clone())
}
pub fn get_positional(&self, n: usize) -> Option<&str> {
if n == 0 {
if self.script_name.is_empty() {
None
} else {
Some(&self.script_name)
}
} else {
self.positional.get(n - 1).map(|s| s.as_str())
}
}
pub fn all_args(&self) -> &[String] {
&self.positional
}
pub fn arg_count(&self) -> usize {
self.positional.len()
}
pub fn error_exit_enabled(&self) -> bool {
self.error_exit && self.errexit_suppressed == 0
}
pub fn set_error_exit(&mut self, enabled: bool) {
self.error_exit = enabled;
}
pub fn suppress_errexit(&mut self) {
self.errexit_suppressed += 1;
}
pub fn unsuppress_errexit(&mut self) {
self.errexit_suppressed = self.errexit_suppressed.saturating_sub(1);
}
pub fn show_ast(&self) -> bool {
self.show_ast
}
pub fn set_show_ast(&mut self, enabled: bool) {
self.show_ast = enabled;
}
pub fn latch_enabled(&self) -> bool {
self.latch_enabled
}
pub fn set_latch_enabled(&mut self, enabled: bool) {
self.latch_enabled = enabled;
}
pub fn trash_enabled(&self) -> bool {
self.trash_enabled
}
pub fn set_trash_enabled(&mut self, enabled: bool) {
self.trash_enabled = enabled;
}
pub fn trash_max_size(&self) -> u64 {
self.trash_max_size
}
pub fn set_trash_max_size(&mut self, size: u64) {
self.trash_max_size = size;
}
pub fn glob_enabled(&self) -> bool {
self.glob_enabled
}
pub fn set_glob_enabled(&mut self, enabled: bool) {
self.glob_enabled = enabled;
}
pub fn export(&mut self, name: impl Into<String>) {
self.exported.insert(name.into());
}
pub fn is_exported(&self, name: &str) -> bool {
self.exported.contains(name)
}
pub fn set_exported(&mut self, name: impl Into<String>, value: Value) {
let name = name.into();
self.set(&name, value);
self.export(name);
}
pub fn unexport(&mut self, name: &str) {
self.exported.remove(name);
}
pub fn exported_vars(&self) -> Vec<(String, Value)> {
let mut result = Vec::new();
for name in &self.exported {
if let Some(value) = self.get(name) {
result.push((name.clone(), value.clone()));
}
}
result.sort_by(|(a, _), (b, _)| a.cmp(b));
result
}
pub fn exported_names(&self) -> Vec<&str> {
let mut names: Vec<&str> = self.exported.iter().map(|s| s.as_str()).collect();
names.sort();
names
}
pub fn resolve_path(&self, path: &VarPath) -> Option<Value> {
if path.segments.is_empty() {
return None;
}
let VarSegment::Field(root_name) = &path.segments[0];
if root_name == "?" {
return self.resolve_result_path(&path.segments[1..]);
}
if path.segments.len() > 1 {
return None; }
self.get(root_name).cloned()
}
fn resolve_result_path(&self, segments: &[VarSegment]) -> Option<Value> {
if segments.is_empty() {
return Some(Value::Int(self.last_result.code));
}
None
}
pub fn contains(&self, name: &str) -> bool {
self.get(name).is_some()
}
pub fn all_names(&self) -> Vec<&str> {
let mut names: Vec<&str> = self
.frames
.iter()
.flat_map(|f| f.keys().map(|s| s.as_str()))
.collect();
names.sort();
names.dedup();
names
}
pub fn all(&self) -> Vec<(String, Value)> {
let mut result = std::collections::HashMap::new();
for frame in self.frames.iter() {
for (name, value) in frame {
result.insert(name.clone(), value.clone());
}
}
let mut pairs: Vec<_> = result.into_iter().collect();
pairs.sort_by(|(a, _), (b, _)| a.cmp(b));
pairs
}
}
impl Default for Scope {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_scope_has_one_frame() {
let scope = Scope::new();
assert_eq!(scope.frames.len(), 1);
}
#[test]
fn set_and_get_variable() {
let mut scope = Scope::new();
scope.set("X", Value::Int(42));
assert_eq!(scope.get("X"), Some(&Value::Int(42)));
}
#[test]
fn get_nonexistent_returns_none() {
let scope = Scope::new();
assert_eq!(scope.get("MISSING"), None);
}
#[test]
fn inner_frame_shadows_outer() {
let mut scope = Scope::new();
scope.set("X", Value::Int(1));
scope.push_frame();
scope.set("X", Value::Int(2));
assert_eq!(scope.get("X"), Some(&Value::Int(2)));
scope.pop_frame();
assert_eq!(scope.get("X"), Some(&Value::Int(1)));
}
#[test]
fn inner_frame_can_see_outer_vars() {
let mut scope = Scope::new();
scope.set("OUTER", Value::String("visible".into()));
scope.push_frame();
assert_eq!(scope.get("OUTER"), Some(&Value::String("visible".into())));
}
#[test]
fn resolve_simple_path() {
let mut scope = Scope::new();
scope.set("NAME", Value::String("Alice".into()));
let path = VarPath::simple("NAME");
assert_eq!(
scope.resolve_path(&path),
Some(Value::String("Alice".into()))
);
}
#[test]
fn resolve_bare_last_result_returns_exit_code() {
let mut scope = Scope::new();
scope.set_last_result(ExecResult::failure(127, "not found"));
let path = VarPath {
segments: vec![VarSegment::Field("?".into())],
};
assert_eq!(scope.resolve_path(&path), Some(Value::Int(127)));
}
#[test]
fn resolve_last_result_field_access_is_rejected() {
let mut scope = Scope::new();
scope.set_last_result(ExecResult::success_with_data(
"1",
Value::Json(serde_json::json!({"count": 5})),
));
let path = VarPath {
segments: vec![
VarSegment::Field("?".into()),
VarSegment::Field("data".into()),
],
};
assert_eq!(scope.resolve_path(&path), None);
}
#[test]
fn resolve_invalid_path_returns_none() {
let mut scope = Scope::new();
scope.set("X", Value::Int(42));
let path = VarPath {
segments: vec![
VarSegment::Field("X".into()),
VarSegment::Field("invalid".into()),
],
};
assert_eq!(scope.resolve_path(&path), None);
}
#[test]
fn contains_finds_variable() {
let mut scope = Scope::new();
scope.set("EXISTS", Value::Bool(true));
assert!(scope.contains("EXISTS"));
assert!(!scope.contains("MISSING"));
}
#[test]
fn all_names_lists_variables() {
let mut scope = Scope::new();
scope.set("A", Value::Int(1));
scope.set("B", Value::Int(2));
scope.push_frame();
scope.set("C", Value::Int(3));
let names = scope.all_names();
assert!(names.contains(&"A"));
assert!(names.contains(&"B"));
assert!(names.contains(&"C"));
}
#[test]
#[should_panic(expected = "cannot pop the root scope frame")]
fn pop_root_frame_panics() {
let mut scope = Scope::new();
scope.pop_frame();
}
#[test]
fn positional_params_basic() {
let mut scope = Scope::new();
scope.set_positional("my_tool", vec!["arg1".into(), "arg2".into(), "arg3".into()]);
assert_eq!(scope.get_positional(0), Some("my_tool"));
assert_eq!(scope.get_positional(1), Some("arg1"));
assert_eq!(scope.get_positional(2), Some("arg2"));
assert_eq!(scope.get_positional(3), Some("arg3"));
assert_eq!(scope.get_positional(4), None);
}
#[test]
fn positional_params_empty() {
let scope = Scope::new();
assert_eq!(scope.get_positional(0), None);
assert_eq!(scope.get_positional(1), None);
assert_eq!(scope.arg_count(), 0);
assert!(scope.all_args().is_empty());
}
#[test]
fn all_args_returns_slice() {
let mut scope = Scope::new();
scope.set_positional("test", vec!["a".into(), "b".into(), "c".into()]);
let args = scope.all_args();
assert_eq!(args, &["a", "b", "c"]);
}
#[test]
fn arg_count_returns_count() {
let mut scope = Scope::new();
scope.set_positional("test", vec!["one".into(), "two".into()]);
assert_eq!(scope.arg_count(), 2);
}
#[test]
fn export_marks_variable() {
let mut scope = Scope::new();
scope.set("X", Value::Int(42));
assert!(!scope.is_exported("X"));
scope.export("X");
assert!(scope.is_exported("X"));
}
#[test]
fn set_exported_sets_and_exports() {
let mut scope = Scope::new();
scope.set_exported("PATH", Value::String("/usr/bin".into()));
assert!(scope.is_exported("PATH"));
assert_eq!(scope.get("PATH"), Some(&Value::String("/usr/bin".into())));
}
#[test]
fn unexport_removes_export_marker() {
let mut scope = Scope::new();
scope.set_exported("VAR", Value::Int(1));
assert!(scope.is_exported("VAR"));
scope.unexport("VAR");
assert!(!scope.is_exported("VAR"));
assert!(scope.get("VAR").is_some());
}
#[test]
fn exported_vars_returns_only_exported_with_values() {
let mut scope = Scope::new();
scope.set_exported("A", Value::Int(1));
scope.set_exported("B", Value::Int(2));
scope.set("C", Value::Int(3)); scope.export("D");
let exported = scope.exported_vars();
assert_eq!(exported.len(), 2);
assert_eq!(exported[0], ("A".to_string(), Value::Int(1)));
assert_eq!(exported[1], ("B".to_string(), Value::Int(2)));
}
#[test]
fn exported_names_returns_sorted_names() {
let mut scope = Scope::new();
scope.export("Z");
scope.export("A");
scope.export("M");
let names = scope.exported_names();
assert_eq!(names, vec!["A", "M", "Z"]);
}
}