Compare commits

...

5 Commits

@ -1,6 +1,6 @@
[package]
name = "embed-nu"
version = "0.5.4"
version = "0.5.6"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/Trivernis/embed-nu"

@ -5,7 +5,7 @@ pub use builder::*;
pub use command_group_config::CommandGroupConfig;
use nu_protocol::{
ast::{Block, Call},
engine::{EngineState, Stack},
engine::{EngineState, Stack, StateWorkingSet},
PipelineData, Span,
};
@ -13,7 +13,7 @@ use crate::{
argument::IntoArgument,
error::{CrateError, CrateResult},
utils::parse_nu_script,
NewEmpty,
IntoValue, NewEmpty,
};
/// Represents the evaluation context of nu scripts and commands
@ -125,4 +125,21 @@ impl Context {
Ok(())
}
/// Adds a variable to the context
pub fn add_var<S: ToString, V: IntoValue>(&mut self, name: S, value: V) -> CrateResult<()> {
let mut working_set = StateWorkingSet::new(&self.engine_state);
let var_id = working_set.add_variable(
name.to_string().into_bytes(),
Span::empty(),
nu_protocol::Type::Any,
false,
);
self.stack.add_var(var_id, value.into_value());
let delta = working_set.render();
self.engine_state.merge_delta(delta)?;
Ok(())
}
}

@ -11,6 +11,7 @@ pub use context::{CommandGroupConfig, Context, ContextBuilder};
pub use into_expression::*;
pub use into_value::*;
pub use nu_engine::{self, CallExt};
pub use nu_parser;
pub use nu_protocol::{self, PipelineData, Value};
pub use rusty_value;
pub use utils::NewEmpty;

@ -1,7 +1,7 @@
use embed_nu::{rusty_value::*, IntoValue};
use embed_nu::{rusty_value::*, IntoValue, NewEmpty};
use embed_nu::{CommandGroupConfig, Context, PipelineData};
use nu_protocol::engine::Command;
use nu_protocol::{Config, Signature, SyntaxShape};
use nu_protocol::{Config, Signature, Span, SyntaxShape};
#[test]
fn it_evals_strings() {
@ -24,6 +24,22 @@ fn it_returns_variables() {
assert_eq!(val.as_string().unwrap(), String::from("world"))
}
#[test]
fn it_accepts_variables() {
let mut ctx = get_context();
ctx.add_var("hello", "world").unwrap();
let val = ctx.get_var("hello").expect("No variable returned");
assert_eq!(val.as_string().unwrap(), String::from("world"));
let val = ctx
.eval_raw(r#"$hello"#, PipelineData::empty())
.unwrap()
.into_value(Span::empty());
assert_eq!(val.as_string().unwrap(), String::from("world"))
}
#[derive(RustyValue)]
struct TestArg {
foo: String,

Loading…
Cancel
Save