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

45 lines
1.1 KiB
Rust
Raw Normal View History

2021-09-03 06:01:45 +02:00
use nu_engine::{eval_block, eval_expression};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
pub struct Do;
impl Command for Do {
fn name(&self) -> &str {
"do"
}
fn usage(&self) -> &str {
"Run a block"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("do").required(
"block",
SyntaxShape::Block(Some(vec![])),
"the block to run",
)
2021-09-03 06:01:45 +02:00
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let block = &call.positional[0];
2021-09-04 09:59:38 +02:00
let out = eval_expression(context, block)?;
2021-09-03 06:01:45 +02:00
match out {
Value::Block { val: block_id, .. } => {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
eval_block(context, block, input)
}
_ => Ok(Value::nothing()),
}
}
}