This commit is contained in:
JT 2021-09-03 16:01:45 +12:00
parent bc3f820227
commit 82cf6caba4
3 changed files with 45 additions and 1 deletions

View file

@ -5,7 +5,7 @@ use nu_protocol::{
Signature, SyntaxShape,
};
use crate::{Alias, Benchmark, BuildString, Def, Each, For, If, Length, Let, LetEnv};
use crate::{Alias, Benchmark, BuildString, Def, Do, Each, For, If, Length, Let, LetEnv};
pub fn create_default_context() -> Rc<RefCell<EngineState>> {
let engine_state = Rc::new(RefCell::new(EngineState::new()));
@ -33,6 +33,8 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
working_set.add_decl(Box::new(Each));
working_set.add_decl(Box::new(Do));
working_set.add_decl(Box::new(Benchmark));
working_set.add_decl(Box::new(Length));

View file

@ -0,0 +1,40 @@
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, "the block to run")
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let block = &call.positional[0];
let out = eval_expression(context, &block)?;
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()),
}
}
}

View file

@ -3,6 +3,7 @@ mod benchmark;
mod build_string;
mod def;
mod default_context;
mod do_;
mod each;
mod for_;
mod if_;
@ -15,6 +16,7 @@ pub use benchmark::Benchmark;
pub use build_string::BuildString;
pub use def::Def;
pub use default_context::create_default_context;
pub use do_::Do;
pub use each::Each;
pub use for_::For;
pub use if_::If;