Add a function to read variables from the nu executor state

pull/3/head
trivernis 1 year ago
parent bdaedca75b
commit 39f3b9b8df
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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"

@ -54,6 +54,22 @@ impl Context {
self.eval_block(&block, input)
}
/// Returns a variable defined in the stack
pub fn get_var<S: AsRef<str>>(&mut self, name: S) -> Option<nu_protocol::Value> {
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<S: AsRef<str>>(&mut self, name: S) -> bool {
self.engine_state

@ -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,

Loading…
Cancel
Save