This commit is contained in:
JT
2021-10-25 17:01:02 +13:00
parent ab9d6b206d
commit b6d269e90a
63 changed files with 1075 additions and 1013 deletions

View File

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Alias;
impl Command for Alias {
@ -27,8 +28,8 @@ impl Command for Alias {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
Ok(Value::Nothing { span: call.head })
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new())
}
}

View File

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Def;
impl Command for Def {
@ -28,8 +29,8 @@ impl Command for Def {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
Ok(Value::Nothing { span: call.head })
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new())
}
}

View File

@ -1,8 +1,9 @@
use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Do;
impl Command for Do {
@ -29,15 +30,14 @@ impl Command for Do {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let block_id = call.positional[0]
.as_block()
.expect("internal error: expected block");
let rest: Vec<Value> = call.rest(context, 1)?;
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
let block = context.engine_state.get_block(block_id);
let state = context.enter_scope();

View File

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct ExportDef;
impl Command for ExportDef {
@ -28,8 +29,8 @@ impl Command for ExportDef {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
Ok(Value::Nothing { span: call.head })
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new())
}
}

View File

@ -1,8 +1,9 @@
use nu_engine::{eval_block, eval_expression};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Example, Signature, Span, SyntaxShape, Value};
use nu_protocol::{Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)]
pub struct For;
impl Command for For {
@ -38,8 +39,8 @@ impl Command for For {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let var_id = call.positional[0]
.as_var()
.expect("internal error: missing variable");
@ -55,19 +56,26 @@ impl Command for For {
let context = context.clone();
values.map(call.head, move |x| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);
match values {
Value::List { vals, span } => Ok(vals
.into_iter()
.map(move |x| {
let block = context.engine_state.get_block(block);
let state = context.enter_scope();
let state = context.enter_scope();
state.add_var(var_id, x);
state.add_var(var_id, x);
match eval_block(&state, block, Value::nothing()) {
Ok(value) => value,
Err(error) => Value::Error { error },
}
})
match eval_block(&state, block, PipelineData::new()) {
Ok(value) => Value::List {
vals: value.collect(),
span,
},
Err(error) => Value::Error { error },
}
})
.into_pipeline_data()),
}
}
fn examples(&self) -> Vec<Example> {

View File

@ -1,11 +1,13 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
span, Example, ShellError, Signature, Spanned, SyntaxShape, Value,
span, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape,
Value,
};
use nu_engine::{get_full_help, CallExt};
#[derive(Clone)]
pub struct Help;
impl Command for Help {
@ -36,8 +38,8 @@ impl Command for Help {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
help(context, call)
}
@ -72,7 +74,7 @@ impl Command for Help {
}
}
fn help(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
fn help(context: &EvaluationContext, call: &Call) -> Result<PipelineData, ShellError> {
let head = call.head;
let find: Option<Spanned<String>> = call.get_flag(context, "find")?;
let rest: Vec<Spanned<String>> = call.rest(context, 0)?;
@ -114,10 +116,7 @@ fn help(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
}
}
return Ok(Value::List {
vals: found_cmds_vec,
span: head,
});
return Ok(found_cmds_vec.into_iter().into_pipeline_data());
}
if !rest.is_empty() {
@ -151,10 +150,7 @@ fn help(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
});
}
Ok(Value::List {
vals: found_cmds_vec,
span: head,
})
Ok(found_cmds_vec.into_iter().into_pipeline_data())
} else {
let mut name = String::new();
let mut output = String::new();
@ -177,7 +173,8 @@ fn help(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
Ok(Value::String {
val: output,
span: call.head,
})
}
.into_pipeline_data())
} else {
Err(ShellError::CommandNotFound(span(&[
rest[0].span,
@ -355,7 +352,8 @@ You can also learn more at https://www.nushell.sh/book/"#;
Ok(Value::String {
val: msg.into(),
span: head,
})
}
.into_pipeline_data())
}
}

View File

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Hide;
impl Command for Hide {
@ -21,8 +22,8 @@ impl Command for Hide {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
Ok(Value::Nothing { span: call.head })
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new())
}
}

View File

@ -1,8 +1,9 @@
use nu_engine::{eval_block, eval_expression};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct If;
impl Command for If {
@ -29,8 +30,8 @@ impl Command for If {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let cond = &call.positional[0];
let then_block = call.positional[1]
.as_block()
@ -40,25 +41,24 @@ impl Command for If {
let result = eval_expression(context, cond)?;
match result {
Value::Bool { val, span } => {
let engine_state = context.engine_state.borrow();
if val {
let block = engine_state.get_block(then_block);
let block = context.engine_state.get_block(then_block);
let state = context.enter_scope();
eval_block(&state, block, input)
} else if let Some(else_case) = else_case {
if let Some(else_expr) = else_case.as_keyword() {
if let Some(block_id) = else_expr.as_block() {
let block = engine_state.get_block(block_id);
let block = context.engine_state.get_block(block_id);
let state = context.enter_scope();
eval_block(&state, block, input)
} else {
eval_expression(context, else_expr)
eval_expression(context, else_expr).map(|x| x.into_pipeline_data())
}
} else {
eval_expression(context, else_case)
eval_expression(context, else_case).map(|x| x.into_pipeline_data())
}
} else {
Ok(Value::Nothing { span })
Ok(PipelineData::new())
}
}
_ => Err(ShellError::CantConvert("bool".into(), result.span()?)),

View File

@ -1,8 +1,9 @@
use nu_engine::eval_expression;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Let;
impl Command for Let {
@ -28,8 +29,8 @@ impl Command for Let {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let var_id = call.positional[0]
.as_var()
.expect("internal error: missing variable");
@ -43,8 +44,6 @@ impl Command for Let {
//println!("Adding: {:?} to {}", rhs, var_id);
context.add_var(var_id, rhs);
Ok(Value::Nothing {
span: call.positional[0].span,
})
Ok(PipelineData::new())
}
}

View File

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Module;
impl Command for Module {
@ -27,8 +28,8 @@ impl Command for Module {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
Ok(Value::Nothing { span: call.head })
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new())
}
}

View File

@ -1,9 +1,10 @@
use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Value};
/// Source a file for environment variables.
#[derive(Clone)]
pub struct Source;
impl Command for Source {
@ -27,17 +28,13 @@ impl Command for Source {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<Value, ShellError> {
input: PipelineData,
) -> Result<PipelineData, ShellError> {
// Note: this hidden positional is the block_id that corresponded to the 0th position
// it is put here by the parser
let block_id: i64 = call.req(context, 1)?;
let block = context
.engine_state
.borrow()
.get_block(block_id as usize)
.clone();
let block = context.engine_state.get_block(block_id as usize).clone();
eval_block(context, &block, input)
}
}

View File

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Use;
impl Command for Use {
@ -21,8 +22,8 @@ impl Command for Use {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
Ok(Value::Nothing { span: call.head })
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new())
}
}