diff --git a/Cargo.toml b/Cargo.toml index ebbe29e..f20e6a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "embed-nu" -version = "0.3.3" +version = "0.3.4" edition = "2021" license = "Apache-2.0" repository = "https://github.com/Trivernis/embed-nu" diff --git a/src/context/mod.rs b/src/context/mod.rs index 7d4d1d2..d22cb54 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -54,6 +54,22 @@ impl Context { self.eval_block(&block, input) } + /// Returns a variable defined in the stack + pub fn get_var>(&mut self, name: S) -> Option { + let name = name.as_ref(); + let dollar_name = format!("${name}"); + let var_id = self + .engine_state + .active_overlays(&vec![]) + .iter() + .find_map(|o| { + o.vars + .get(dollar_name.as_bytes()) + .or(o.vars.get(name.as_bytes())) + })?; + self.stack.get_var(*var_id, Span::new(0, 0)).ok() + } + /// Returns if the given function exists in the context pub fn has_fn>(&mut self, name: S) -> bool { self.engine_state diff --git a/tests/test_eval.rs b/tests/test_eval.rs index a3faa2a..fbf6b01 100644 --- a/tests/test_eval.rs +++ b/tests/test_eval.rs @@ -15,6 +15,15 @@ fn it_evals_strings() { ctx.print_pipeline(pipeline).unwrap() } +#[test] +fn it_returns_variables() { + let mut ctx = get_context(); + ctx.eval_raw(r#"let hello = 'world'"#, PipelineData::empty()) + .unwrap(); + let val = ctx.get_var("hello").expect("No variable returned"); + assert_eq!(val.as_string().unwrap(), String::from("world")) +} + #[derive(RustyValue)] struct TestArg { foo: String,