forked from extern/nushell
51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
use nu_engine::eval_expression;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EvaluationContext};
|
|
use nu_protocol::{Signature, SyntaxShape, Value};
|
|
|
|
pub struct Let;
|
|
|
|
impl Command for Let {
|
|
fn name(&self) -> &str {
|
|
"let"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Create a variable and give it a value."
|
|
}
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
Signature::build("let")
|
|
.required("var_name", SyntaxShape::VarWithOptType, "variable name")
|
|
.required(
|
|
"initial_value",
|
|
SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::Expression)),
|
|
"equals sign followed by value",
|
|
)
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
context: &EvaluationContext,
|
|
call: &Call,
|
|
_input: Value,
|
|
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
|
let var_id = call.positional[0]
|
|
.as_var()
|
|
.expect("internal error: missing variable");
|
|
|
|
let keyword_expr = call.positional[1]
|
|
.as_keyword()
|
|
.expect("internal error: missing keyword");
|
|
|
|
let rhs = eval_expression(context, keyword_expr)?;
|
|
|
|
//println!("Adding: {:?} to {}", rhs, var_id);
|
|
|
|
context.add_var(var_id, rhs);
|
|
Ok(Value::Nothing {
|
|
span: call.positional[0].span,
|
|
})
|
|
}
|
|
}
|