2020-12-18 08:53:49 +01:00
|
|
|
use crate::prelude::*;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::{evaluate_baseline_expr, WholeStreamCommand};
|
2020-12-18 08:53:49 +01:00
|
|
|
|
|
|
|
use nu_errors::ShellError;
|
2021-01-03 07:44:21 +01:00
|
|
|
use nu_protocol::{hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape};
|
2020-12-18 08:53:49 +01:00
|
|
|
use nu_source::Tagged;
|
|
|
|
|
2021-01-05 00:30:55 +01:00
|
|
|
pub struct Let;
|
2020-12-18 08:53:49 +01:00
|
|
|
|
2021-01-05 00:30:55 +01:00
|
|
|
impl WholeStreamCommand for Let {
|
2020-12-18 08:53:49 +01:00
|
|
|
fn name(&self) -> &str {
|
2021-01-05 00:30:55 +01:00
|
|
|
"let"
|
2020-12-18 08:53:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2021-01-05 00:30:55 +01:00
|
|
|
Signature::build("let")
|
2020-12-18 08:53:49 +01:00
|
|
|
.required("name", SyntaxShape::String, "the name of the variable")
|
|
|
|
.required("equals", SyntaxShape::String, "the equals sign")
|
|
|
|
.required(
|
|
|
|
"expr",
|
2020-12-21 05:32:06 +01:00
|
|
|
SyntaxShape::MathExpression,
|
2021-01-05 00:30:55 +01:00
|
|
|
"the value for the variable",
|
2020-12-18 08:53:49 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2021-01-05 00:30:55 +01:00
|
|
|
"Create a variable and give it a value."
|
2020-12-18 08:53:49 +01:00
|
|
|
}
|
|
|
|
|
2021-04-12 04:35:01 +02:00
|
|
|
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
2021-04-06 18:19:43 +02:00
|
|
|
letcmd(args)
|
2020-12-18 08:53:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 04:35:01 +02:00
|
|
|
pub fn letcmd(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
2020-12-18 08:53:49 +01:00
|
|
|
let tag = args.call_info.name_tag.clone();
|
|
|
|
let ctx = EvaluationContext::from_args(&args);
|
2021-04-08 10:15:36 +02:00
|
|
|
let args = args.evaluate_once()?;
|
2020-12-18 08:53:49 +01:00
|
|
|
|
2021-04-08 10:15:36 +02:00
|
|
|
//let (LetArgs { name, rhs, .. }, _) = args.process()?;
|
|
|
|
let name: Tagged<String> = args.req(0)?;
|
|
|
|
let rhs: CapturedBlock = args.req(2)?;
|
2020-12-18 08:53:49 +01:00
|
|
|
|
|
|
|
let (expr, captured) = {
|
|
|
|
if rhs.block.block.len() != 1 {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected a value",
|
|
|
|
"expected a value",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
match rhs.block.block[0].pipelines.get(0) {
|
|
|
|
Some(item) => match item.list.get(0) {
|
2021-04-08 21:47:41 +02:00
|
|
|
Some(ClassifiedCommand::Expr(expr)) => (expr, &rhs.captured),
|
2020-12-18 08:53:49 +01:00
|
|
|
_ => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected a value",
|
|
|
|
"expected a value",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected a value",
|
|
|
|
"expected a value",
|
|
|
|
tag,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.scope.enter_scope();
|
|
|
|
ctx.scope.add_vars(&captured.entries);
|
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
let value = evaluate_baseline_expr(&expr, &ctx);
|
2020-12-18 08:53:49 +01:00
|
|
|
|
|
|
|
ctx.scope.exit_scope();
|
|
|
|
|
2020-12-19 07:25:03 +01:00
|
|
|
let value = value?;
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
let name = if name.item.starts_with('$') {
|
2021-04-06 18:19:43 +02:00
|
|
|
name.item
|
2020-12-18 08:53:49 +01:00
|
|
|
} else {
|
|
|
|
format!("${}", name.item)
|
|
|
|
};
|
|
|
|
|
2021-01-03 07:44:21 +01:00
|
|
|
// Note: this is a special case for setting the context from a command
|
|
|
|
// In this case, if we don't set it now, we'll lose the scope that this
|
|
|
|
// variable should be set into.
|
|
|
|
ctx.scope.add_var(name, value);
|
|
|
|
|
2021-04-12 04:35:01 +02:00
|
|
|
Ok(ActionStream::empty())
|
2020-12-18 08:53:49 +01:00
|
|
|
}
|