This commit is contained in:
JT
2021-09-06 11:16:27 +12:00
parent 036c6a9a52
commit aaee3a8b61
10 changed files with 192 additions and 124 deletions

View File

@ -15,13 +15,7 @@ impl Command for Each {
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("each")
.required(
"var_name",
SyntaxShape::Variable,
"name of the looping variable",
)
.required("block", SyntaxShape::Block, "the block to run")
Signature::build("each").required("block", SyntaxShape::Block, "the block to run")
}
fn run(
@ -30,11 +24,7 @@ impl Command for Each {
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 block = call.positional[1]
let block_id = call.positional[0]
.as_block()
.expect("internal error: expected block");
let context = context.clone();
@ -45,13 +35,19 @@ impl Command for Each {
.into_iter()
.map(move |x| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);
let block = engine_state.get_block(block_id);
let state = context.enter_scope();
state.add_var(var_id, x);
if let Some(var) = block.signature.required_positional.first() {
if let Some(var_id) = &var.var_id {
state.add_var(*var_id, x);
}
}
//FIXME: DON'T UNWRAP
eval_block(&state, block, Value::nothing()).unwrap()
match eval_block(&state, block, Value::nothing()) {
Ok(v) => v,
Err(err) => Value::Error { err },
}
})
.collect(),
span: call.head,
@ -60,13 +56,19 @@ impl Command for Each {
stream: stream
.map(move |x| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);
let block = engine_state.get_block(block_id);
let state = context.enter_scope();
state.add_var(var_id, x);
if let Some(var) = block.signature.required_positional.first() {
if let Some(var_id) = &var.var_id {
state.add_var(*var_id, x);
}
}
//FIXME: DON'T UNWRAP
eval_block(&state, block, Value::nothing()).unwrap()
match eval_block(&state, block, Value::nothing()) {
Ok(v) => v,
Err(err) => Value::Error { err },
}
})
.into_value_stream(),
span: call.head,