forked from extern/nushell
Improve external output in subexprs (#294)
This commit is contained in:
@ -131,7 +131,7 @@ pub fn action(input: &Value, span: Span) -> Value {
|
||||
fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
||||
match a_string.parse::<bytesize::ByteSize>() {
|
||||
Ok(n) => Ok(n.0 as i64),
|
||||
Err(_) => Err(ShellError::CantConvert("int".into(), span)),
|
||||
Err(_) => Err(ShellError::CantConvert("int".into(), "string".into(), span)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,7 +137,11 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
||||
Ok(n) => Ok(n),
|
||||
Err(_) => match a_string.parse::<f64>() {
|
||||
Ok(f) => Ok(f as i64),
|
||||
_ => Err(ShellError::CantConvert("into int".into(), span)),
|
||||
_ => Err(ShellError::CantConvert(
|
||||
"into int".into(),
|
||||
"string".into(),
|
||||
span,
|
||||
)),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -229,11 +229,8 @@ pub fn action(
|
||||
span,
|
||||
),
|
||||
},
|
||||
_ => Value::Error {
|
||||
error: ShellError::CantConvert(
|
||||
String::from(" into string. Probably this type is not supported yet"),
|
||||
span,
|
||||
),
|
||||
x => Value::Error {
|
||||
error: ShellError::CantConvert(String::from("string"), x.get_type().to_string(), span),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -28,9 +28,9 @@ impl Command for Alias {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -29,9 +29,9 @@ impl Command for Def {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value, ValueStream,
|
||||
};
|
||||
use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape, Value, ValueStream};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Echo;
|
||||
@ -43,7 +41,7 @@ impl Command for Echo {
|
||||
// When there are no elements, we echo the empty string
|
||||
std::cmp::Ordering::Less => PipelineData::Value(Value::String {
|
||||
val: "".to_string(),
|
||||
span: Span::unknown(),
|
||||
span: call.head,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
@ -29,9 +29,9 @@ impl Command for ExportDef {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -44,6 +44,7 @@ impl Command for For {
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
let var_id = call.positional[0]
|
||||
.as_var()
|
||||
.expect("internal error: missing variable");
|
||||
@ -69,8 +70,8 @@ impl Command for For {
|
||||
stack.add_var(var_id, x);
|
||||
|
||||
//let block = engine_state.get_block(block_id);
|
||||
match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) {
|
||||
Ok(pipeline_data) => pipeline_data.into_value(),
|
||||
match eval_block(&engine_state, &mut stack, &block, PipelineData::new(head)) {
|
||||
Ok(pipeline_data) => pipeline_data.into_value(head),
|
||||
Err(error) => Value::Error { error },
|
||||
}
|
||||
})
|
||||
@ -81,8 +82,8 @@ impl Command for For {
|
||||
stack.add_var(var_id, x);
|
||||
|
||||
//let block = engine_state.get_block(block_id);
|
||||
match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) {
|
||||
Ok(pipeline_data) => pipeline_data.into_value(),
|
||||
match eval_block(&engine_state, &mut stack, &block, PipelineData::new(head)) {
|
||||
Ok(pipeline_data) => pipeline_data.into_value(head),
|
||||
Err(error) => Value::Error { error },
|
||||
}
|
||||
})
|
||||
@ -90,7 +91,7 @@ impl Command for For {
|
||||
x => {
|
||||
stack.add_var(var_id, x);
|
||||
|
||||
eval_block(&engine_state, &mut stack, &block, PipelineData::new())
|
||||
eval_block(&engine_state, &mut stack, &block, PipelineData::new(head))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ impl Command for Hide {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -40,9 +40,9 @@ impl Command for If {
|
||||
let else_case = call.positional.get(2);
|
||||
|
||||
let result = eval_expression(engine_state, stack, cond)?;
|
||||
match result {
|
||||
match &result {
|
||||
Value::Bool { val, .. } => {
|
||||
if val {
|
||||
if *val {
|
||||
let block = engine_state.get_block(then_block);
|
||||
let mut stack = stack.collect_captures(&block.captures);
|
||||
eval_block(engine_state, &mut stack, block, input)
|
||||
@ -61,10 +61,14 @@ impl Command for If {
|
||||
.map(|x| x.into_pipeline_data())
|
||||
}
|
||||
} else {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
_ => Err(ShellError::CantConvert("bool".into(), result.span()?)),
|
||||
x => Err(ShellError::CantConvert(
|
||||
"bool".into(),
|
||||
x.get_type().to_string(),
|
||||
result.span()?,
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,6 +45,6 @@ impl Command for Let {
|
||||
//println!("Adding: {:?} to {}", rhs, var_id);
|
||||
|
||||
stack.add_var(var_id, rhs);
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -28,9 +28,9 @@ impl Command for Module {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -26,9 +26,9 @@ impl Command for Register {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ impl Command for Use {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -111,7 +111,7 @@ fn _to_timezone(dt: DateTime<FixedOffset>, timezone: &Spanned<String>, span: Spa
|
||||
match datetime_in_timezone(&dt, timezone.item.as_str()) {
|
||||
Ok(dt) => Value::Date { val: dt, span },
|
||||
Err(_) => Value::Error {
|
||||
error: ShellError::UnsupportedInput(String::from("invalid time zone"), Span::unknown()),
|
||||
error: ShellError::UnsupportedInput(String::from("invalid time zone"), span),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
2
crates/nu-command/src/env/let_env.rs
vendored
2
crates/nu-command/src/env/let_env.rs
vendored
@ -46,6 +46,6 @@ impl Command for LetEnv {
|
||||
//println!("Adding: {:?} to {}", rhs, var_id);
|
||||
|
||||
stack.add_env_var(env_var, rhs);
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
12
crates/nu-command/src/env/with_env.rs
vendored
12
crates/nu-command/src/env/with_env.rs
vendored
@ -91,7 +91,11 @@ impl TryFrom<&Value> for EnvVar {
|
||||
Ok(EnvVar::Proper(s))
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::CantConvert("string".into(), value.span()?))
|
||||
Err(ShellError::CantConvert(
|
||||
"string".into(),
|
||||
value.get_type().to_string(),
|
||||
value.span()?,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -123,9 +127,10 @@ fn with_env(
|
||||
env.insert(k.to_string(), v.try_into()?);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
x => {
|
||||
return Err(ShellError::CantConvert(
|
||||
"string list or single row".into(),
|
||||
x.get_type().to_string(),
|
||||
call.positional[1].span,
|
||||
));
|
||||
}
|
||||
@ -145,9 +150,10 @@ fn with_env(
|
||||
env.insert(k.clone(), v.try_into()?);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
x => {
|
||||
return Err(ShellError::CantConvert(
|
||||
"string list or single row".into(),
|
||||
x.get_type().to_string(),
|
||||
call.positional[1].span,
|
||||
));
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ use nu_engine::eval_block;
|
||||
use nu_parser::parse;
|
||||
use nu_protocol::{
|
||||
engine::{Command, EngineState, Stack, StateWorkingSet},
|
||||
PipelineData,
|
||||
PipelineData, Span,
|
||||
};
|
||||
|
||||
use crate::To;
|
||||
@ -56,10 +56,15 @@ pub fn test_examples(cmd: impl Command + 'static) {
|
||||
|
||||
let mut stack = Stack::new();
|
||||
|
||||
match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) {
|
||||
match eval_block(
|
||||
&engine_state,
|
||||
&mut stack,
|
||||
&block,
|
||||
PipelineData::new(Span::unknown()),
|
||||
) {
|
||||
Err(err) => panic!("test eval error in `{}`: {:?}", example.example, err),
|
||||
Ok(result) => {
|
||||
let result = result.into_value();
|
||||
let result = result.into_value(Span::unknown());
|
||||
println!("input: {}", example.example);
|
||||
println!("result: {:?}", result);
|
||||
println!("done: {:?}", start.elapsed());
|
||||
|
@ -44,13 +44,13 @@ impl Command for Git {
|
||||
}
|
||||
Err(_err) => {
|
||||
// FIXME: Move this to an external signature and add better error handling
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_err) => {
|
||||
// FIXME: Move this to an external signature and add better error handling
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,13 +59,13 @@ impl Command for GitCheckout {
|
||||
}
|
||||
Err(_err) => {
|
||||
// FIXME: Move this to an external signature and add better error handling
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_err) => {
|
||||
// FIXME: Move this to an external signature and add better error handling
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -66,10 +66,10 @@ impl Command for ListGitBranches {
|
||||
.into_iter()
|
||||
.into_pipeline_data(engine_state.ctrlc.clone()))
|
||||
} else {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
} else {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,6 @@ impl Command for Cd {
|
||||
//FIXME: this only changes the current scope, but instead this environment variable
|
||||
//should probably be a block that loads the information from the state in the overlay
|
||||
stack.add_env_var("PWD".into(), path);
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -204,6 +204,6 @@ impl Command for Cp {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ impl Command for Mv {
|
||||
move_file(call, &entry, &destination)?
|
||||
}
|
||||
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,6 +49,6 @@ impl Command for Touch {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -102,8 +102,8 @@ impl Command for Each {
|
||||
}
|
||||
}
|
||||
|
||||
match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) {
|
||||
Ok(v) => v.into_value(),
|
||||
match eval_block(&engine_state, &mut stack, &block, PipelineData::new(span)) {
|
||||
Ok(v) => v.into_value(span),
|
||||
Err(error) => Value::Error { error },
|
||||
}
|
||||
})
|
||||
@ -136,7 +136,7 @@ impl Command for Each {
|
||||
}
|
||||
}
|
||||
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new())? {
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new(span))? {
|
||||
PipelineData::Value(Value::Record {
|
||||
mut cols, mut vals, ..
|
||||
}) => {
|
||||
@ -146,7 +146,7 @@ impl Command for Each {
|
||||
}
|
||||
x => {
|
||||
output_cols.push(col);
|
||||
output_vals.push(x.into_value());
|
||||
output_vals.push(x.into_value(span));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -167,7 +167,7 @@ impl Command for Each {
|
||||
}
|
||||
}
|
||||
|
||||
eval_block(&engine_state, &mut stack, block, PipelineData::new())
|
||||
eval_block(&engine_state, &mut stack, block, PipelineData::new(span))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ impl Command for ParEach {
|
||||
}
|
||||
}
|
||||
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new()) {
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new(span)) {
|
||||
Ok(v) => v,
|
||||
Err(error) => Value::Error { error }.into_pipeline_data(),
|
||||
}
|
||||
@ -129,7 +129,7 @@ impl Command for ParEach {
|
||||
}
|
||||
}
|
||||
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new()) {
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new(span)) {
|
||||
Ok(v) => v,
|
||||
Err(error) => Value::Error { error }.into_pipeline_data(),
|
||||
}
|
||||
@ -169,7 +169,7 @@ impl Command for ParEach {
|
||||
}
|
||||
}
|
||||
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new()) {
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new(span)) {
|
||||
Ok(v) => v,
|
||||
Err(error) => Value::Error { error }.into_pipeline_data(),
|
||||
}
|
||||
@ -206,7 +206,7 @@ impl Command for ParEach {
|
||||
}
|
||||
}
|
||||
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new())? {
|
||||
match eval_block(&engine_state, &mut stack, block, PipelineData::new(span))? {
|
||||
PipelineData::Value(Value::Record {
|
||||
mut cols, mut vals, ..
|
||||
}) => {
|
||||
@ -216,7 +216,7 @@ impl Command for ParEach {
|
||||
}
|
||||
x => {
|
||||
output_cols.push(col);
|
||||
output_vals.push(x.into_value());
|
||||
output_vals.push(x.into_value(span));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -237,7 +237,7 @@ impl Command for ParEach {
|
||||
}
|
||||
}
|
||||
|
||||
eval_block(&engine_state, &mut stack, block, PipelineData::new())
|
||||
eval_block(&engine_state, &mut stack, block, PipelineData::new(span))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ impl Command for Where {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let head = call.head;
|
||||
let cond = call.positional[0].clone();
|
||||
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
@ -79,7 +80,7 @@ impl Command for Where {
|
||||
if result.is_true() {
|
||||
Ok(x.into_pipeline_data())
|
||||
} else {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(head))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ impl Command for From {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -128,7 +128,11 @@ fn convert_nujson_to_value(value: &nu_json::Value, span: Span) -> Value {
|
||||
nu_json::Value::U64(u) => {
|
||||
if *u > i64::MAX as u64 {
|
||||
Value::Error {
|
||||
error: ShellError::CantConvert("i64 sized integer".into(), span),
|
||||
error: ShellError::CantConvert(
|
||||
"i64 sized integer".into(),
|
||||
"larger than i64".into(),
|
||||
span,
|
||||
),
|
||||
}
|
||||
} else {
|
||||
Value::Int {
|
||||
@ -151,6 +155,7 @@ fn convert_string_to_value(string_input: String, span: Span) -> Result<Value, Sh
|
||||
|
||||
Err(_x) => Err(ShellError::CantConvert(
|
||||
"structured data from json".into(),
|
||||
"string".into(),
|
||||
span,
|
||||
)),
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ impl Command for To {
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
_call: &Call,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, ShellError> {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -127,7 +127,11 @@ fn to_json(
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
_ => Ok(Value::Error {
|
||||
error: ShellError::CantConvert("JSON".into(), name_span),
|
||||
error: ShellError::CantConvert(
|
||||
"JSON".into(),
|
||||
value.get_type().to_string(),
|
||||
name_span,
|
||||
),
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
}
|
||||
@ -141,12 +145,20 @@ fn to_json(
|
||||
span: name_span,
|
||||
},
|
||||
_ => Value::Error {
|
||||
error: ShellError::CantConvert("JSON".into(), name_span),
|
||||
error: ShellError::CantConvert(
|
||||
"JSON".into(),
|
||||
value.get_type().to_string(),
|
||||
name_span,
|
||||
),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
Value::Error {
|
||||
error: ShellError::CantConvert("JSON".into(), name_span),
|
||||
error: ShellError::CantConvert(
|
||||
"JSON".into(),
|
||||
value.get_type().to_string(),
|
||||
name_span,
|
||||
),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -41,7 +41,7 @@ impl Command for SubCommand {
|
||||
|
||||
pub fn maximum(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
let max_func = reducer_for(Reduce::Maximum);
|
||||
max_func(Value::nothing(), values.to_vec(), *head)
|
||||
max_func(Value::nothing(*head), values.to_vec(), *head)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -41,7 +41,7 @@ impl Command for SubCommand {
|
||||
|
||||
pub fn minimum(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
let min_func = reducer_for(Reduce::Minimum);
|
||||
min_func(Value::nothing(), values.to_vec(), *head)
|
||||
min_func(Value::nothing(*head), values.to_vec(), *head)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -42,7 +42,7 @@ impl Command for SubCommand {
|
||||
/// Calculate product of given values
|
||||
pub fn product(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
let product_func = reducer_for(Reduce::Product);
|
||||
product_func(Value::nothing(), values.to_vec(), *head)
|
||||
product_func(Value::nothing(*head), values.to_vec(), *head)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -24,7 +24,7 @@ pub fn reducer_for(command: Reduce) -> ReducerFunction {
|
||||
pub fn max(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
let mut biggest = data
|
||||
.first()
|
||||
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), Span::unknown()))?
|
||||
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), head))?
|
||||
.clone();
|
||||
|
||||
for value in &data {
|
||||
@ -48,7 +48,7 @@ pub fn max(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
pub fn min(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
let mut smallest = data
|
||||
.first()
|
||||
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), Span::unknown()))?
|
||||
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), head))?
|
||||
.clone();
|
||||
|
||||
for value in &data {
|
||||
@ -87,9 +87,9 @@ pub fn sum(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
}),
|
||||
None => Err(ShellError::UnsupportedInput(
|
||||
"Empty input".to_string(),
|
||||
Span::unknown(),
|
||||
head,
|
||||
)),
|
||||
_ => Ok(Value::nothing()),
|
||||
_ => Ok(Value::nothing(head)),
|
||||
}?;
|
||||
|
||||
for value in &data {
|
||||
@ -127,7 +127,7 @@ pub fn product(data: Vec<Value>, head: Span) -> Result<Value, ShellError> {
|
||||
"Empty input".to_string(),
|
||||
Span::unknown(),
|
||||
)),
|
||||
_ => Ok(Value::nothing()),
|
||||
_ => Ok(Value::nothing(head)),
|
||||
}?;
|
||||
|
||||
for value in &data {
|
||||
|
@ -48,7 +48,7 @@ impl Command for SubCommand {
|
||||
|
||||
pub fn summation(values: &[Value], head: &Span) -> Result<Value, ShellError> {
|
||||
let sum_func = reducer_for(Reduce::Summation);
|
||||
sum_func(Value::nothing(), values.to_vec(), *head)
|
||||
sum_func(Value::nothing(*head), values.to_vec(), *head)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -38,7 +38,7 @@ impl Command for Format {
|
||||
Ok(pattern) => {
|
||||
let string_pattern = pattern.as_string().unwrap();
|
||||
let ops = extract_formatting_operations(string_pattern);
|
||||
format(input, &ops)
|
||||
format(input, &ops, call.head)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -116,8 +116,9 @@ fn extract_formatting_operations(input: String) -> Vec<FormatOperation> {
|
||||
fn format(
|
||||
input_data: PipelineData,
|
||||
format_operations: &[FormatOperation],
|
||||
span: Span,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let data_as_value = input_data.into_value();
|
||||
let data_as_value = input_data.into_value(span);
|
||||
|
||||
// We can only handle a Record or a List of Record's
|
||||
match data_as_value {
|
||||
|
@ -39,7 +39,13 @@ impl Command for Benchmark {
|
||||
|
||||
let mut stack = stack.collect_captures(&block.captures);
|
||||
let start_time = Instant::now();
|
||||
eval_block(engine_state, &mut stack, block, PipelineData::new())?.into_value();
|
||||
eval_block(
|
||||
engine_state,
|
||||
&mut stack,
|
||||
block,
|
||||
PipelineData::new(call.head),
|
||||
)?
|
||||
.into_value(call.head);
|
||||
|
||||
let end_time = Instant::now();
|
||||
|
||||
|
@ -161,9 +161,9 @@ impl ExternalCommand {
|
||||
});
|
||||
|
||||
// The ValueStream is consumed by the next expression in the pipeline
|
||||
ChannelReceiver::new(rx).into_pipeline_data(ctrlc)
|
||||
ChannelReceiver::new(rx, self.name.span).into_pipeline_data(ctrlc)
|
||||
} else {
|
||||
PipelineData::new()
|
||||
PipelineData::new(self.name.span)
|
||||
};
|
||||
|
||||
match child.wait() {
|
||||
@ -214,11 +214,12 @@ enum Data {
|
||||
// It implements iterator so it can be used as a ValueStream
|
||||
struct ChannelReceiver {
|
||||
rx: mpsc::Receiver<Data>,
|
||||
span: Span,
|
||||
}
|
||||
|
||||
impl ChannelReceiver {
|
||||
pub fn new(rx: mpsc::Receiver<Data>) -> Self {
|
||||
Self { rx }
|
||||
pub fn new(rx: mpsc::Receiver<Data>, span: Span) -> Self {
|
||||
Self { rx, span }
|
||||
}
|
||||
}
|
||||
|
||||
@ -230,11 +231,11 @@ impl Iterator for ChannelReceiver {
|
||||
Ok(v) => match v {
|
||||
Data::String(s) => Some(Value::String {
|
||||
val: s,
|
||||
span: Span::unknown(),
|
||||
span: self.span,
|
||||
}),
|
||||
Data::Bytes(b) => Some(Value::Binary {
|
||||
val: b,
|
||||
span: Span::unknown(),
|
||||
span: self.span,
|
||||
}),
|
||||
},
|
||||
Err(_) => None,
|
||||
|
@ -70,7 +70,7 @@ prints out the list properly."#
|
||||
separator_param,
|
||||
))
|
||||
} else {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
PipelineData::Stream(stream) => {
|
||||
@ -86,7 +86,7 @@ prints out the list properly."#
|
||||
))
|
||||
} else {
|
||||
// dbg!(data);
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
PipelineData::Value(Value::Record { cols, vals, .. }) => {
|
||||
|
@ -48,7 +48,7 @@ impl Command for Table {
|
||||
}
|
||||
.into_pipeline_data())
|
||||
} else {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
PipelineData::Stream(stream) => {
|
||||
@ -63,7 +63,7 @@ impl Command for Table {
|
||||
}
|
||||
.into_pipeline_data())
|
||||
} else {
|
||||
Ok(PipelineData::new())
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
PipelineData::Value(Value::Record { cols, vals, .. }) => {
|
||||
|
Reference in New Issue
Block a user