nushell/crates/nu-command/src/each.rs

228 lines
9.1 KiB
Rust
Raw Normal View History

2021-09-03 09:35:29 +02:00
use nu_engine::eval_block;
2021-09-03 05:45:34 +02:00
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
2021-09-03 09:35:29 +02:00
use nu_protocol::{IntoValueStream, Signature, SyntaxShape, Value};
2021-09-03 05:45:34 +02:00
pub struct Each;
impl Command for Each {
fn name(&self) -> &str {
"each"
}
fn usage(&self) -> &str {
"Run a block on each element of input"
}
fn signature(&self) -> nu_protocol::Signature {
2021-09-11 23:26:35 +02:00
Signature::build("each")
.required(
"block",
SyntaxShape::Block(Some(vec![SyntaxShape::Any])),
"the block to run",
)
2021-09-11 23:26:35 +02:00
.switch("numbered", "iterate with an index", Some('n'))
2021-09-03 05:45:34 +02:00
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
2021-09-06 01:16:27 +02:00
let block_id = call.positional[0]
2021-09-03 05:45:34 +02:00
.as_block()
.expect("internal error: expected block");
2021-09-11 23:26:35 +02:00
let numbered = call.has_flag("numbered");
2021-09-03 05:45:34 +02:00
let context = context.clone();
2021-09-11 23:26:35 +02:00
let span = call.head;
2021-09-03 05:45:34 +02:00
match input {
2021-09-07 09:35:59 +02:00
Value::Range { val, .. } => Ok(Value::Stream {
2021-09-06 06:07:48 +02:00
stream: val
2021-09-04 08:52:28 +02:00
.into_iter()
2021-09-11 23:26:35 +02:00
.enumerate()
.map(move |(idx, x)| {
2021-09-04 08:52:28 +02:00
let engine_state = context.engine_state.borrow();
2021-09-06 01:16:27 +02:00
let block = engine_state.get_block(block_id);
2021-09-04 08:52:28 +02:00
let state = context.enter_scope();
2021-09-11 23:26:35 +02:00
2021-09-08 00:00:20 +02:00
if let Some(var) = block.signature.get_positional(0) {
2021-09-06 01:16:27 +02:00
if let Some(var_id) = &var.var_id {
2021-09-11 23:26:35 +02:00
if numbered {
state.add_var(
*var_id,
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span,
},
x,
],
span,
},
);
} else {
state.add_var(*var_id, x);
}
2021-09-06 01:16:27 +02:00
}
}
2021-09-04 08:52:28 +02:00
2021-09-06 01:16:27 +02:00
match eval_block(&state, block, Value::nothing()) {
Ok(v) => v,
2021-09-06 06:07:48 +02:00
Err(error) => Value::Error { error },
2021-09-06 01:16:27 +02:00
}
2021-09-04 08:52:28 +02:00
})
2021-09-06 06:07:48 +02:00
.into_value_stream(),
span: call.head,
}),
2021-09-07 09:35:59 +02:00
Value::List { vals: val, .. } => Ok(Value::Stream {
2021-09-06 06:07:48 +02:00
stream: val
.into_iter()
2021-09-11 23:26:35 +02:00
.enumerate()
.map(move |(idx, x)| {
2021-09-06 06:07:48 +02:00
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
let state = context.enter_scope();
2021-09-08 00:00:20 +02:00
if let Some(var) = block.signature.get_positional(0) {
2021-09-06 06:07:48 +02:00
if let Some(var_id) = &var.var_id {
2021-09-11 23:26:35 +02:00
if numbered {
state.add_var(
*var_id,
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span,
},
x,
],
span,
},
);
} else {
state.add_var(*var_id, x);
}
2021-09-06 06:07:48 +02:00
}
}
match eval_block(&state, block, Value::nothing()) {
Ok(v) => v,
Err(error) => Value::Error { error },
}
})
.into_value_stream(),
2021-09-04 08:52:28 +02:00
span: call.head,
}),
2021-09-07 09:35:59 +02:00
Value::Stream { stream, .. } => Ok(Value::Stream {
2021-09-04 08:52:28 +02:00
stream: stream
2021-09-11 23:26:35 +02:00
.enumerate()
.map(move |(idx, x)| {
2021-09-03 05:45:34 +02:00
let engine_state = context.engine_state.borrow();
2021-09-06 01:16:27 +02:00
let block = engine_state.get_block(block_id);
2021-09-03 05:45:34 +02:00
let state = context.enter_scope();
2021-09-08 00:00:20 +02:00
if let Some(var) = block.signature.get_positional(0) {
2021-09-06 01:16:27 +02:00
if let Some(var_id) = &var.var_id {
2021-09-11 23:26:35 +02:00
if numbered {
state.add_var(
*var_id,
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span,
},
x,
],
span,
},
);
} else {
state.add_var(*var_id, x);
}
2021-09-06 01:16:27 +02:00
}
}
2021-09-03 05:45:34 +02:00
2021-09-06 01:16:27 +02:00
match eval_block(&state, block, Value::nothing()) {
Ok(v) => v,
2021-09-06 06:07:48 +02:00
Err(error) => Value::Error { error },
2021-09-06 01:16:27 +02:00
}
2021-09-03 05:45:34 +02:00
})
.into_value_stream(),
span: call.head,
}),
2021-09-08 00:00:20 +02:00
Value::Record { cols, vals, .. } => {
let mut output_cols = vec![];
let mut output_vals = vec![];
for (col, val) in cols.into_iter().zip(vals.into_iter()) {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
let state = context.enter_scope();
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
state.add_var(
*var_id,
Value::Record {
cols: vec!["column".into(), "value".into()],
vals: vec![
Value::String {
val: col.clone(),
span: call.head,
},
val,
],
span: call.head,
},
);
}
}
match eval_block(&state, block, Value::nothing())? {
Value::Record {
mut cols, mut vals, ..
} => {
// TODO check that the lengths match
output_cols.append(&mut cols);
output_vals.append(&mut vals);
}
x => {
output_cols.push(col);
output_vals.push(x);
}
}
}
Ok(Value::Record {
cols: output_cols,
vals: output_vals,
span: call.head,
})
}
2021-09-06 06:16:32 +02:00
x => {
//TODO: we need to watch to make sure this is okay
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
let state = context.enter_scope();
2021-09-08 00:00:20 +02:00
if let Some(var) = block.signature.get_positional(0) {
2021-09-06 06:16:32 +02:00
if let Some(var_id) = &var.var_id {
state.add_var(*var_id, x);
}
}
eval_block(&state, block, Value::nothing())
}
2021-09-03 05:45:34 +02:00
}
}
}