forked from extern/nushell
Move row condition to block (#368)
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
use nu_engine::eval_expression;
|
||||
use nu_engine::eval_block;
|
||||
use nu_protocol::ast::{Call, Expr, Expression};
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape};
|
||||
@ -28,30 +28,34 @@ impl Command for Where {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let span = call.head;
|
||||
let cond = call.positional[0].clone();
|
||||
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
let engine_state = engine_state.clone();
|
||||
|
||||
// FIXME: expensive
|
||||
let mut stack = stack.clone();
|
||||
|
||||
let (var_id, cond) = match cond {
|
||||
let block_id = match cond {
|
||||
Expression {
|
||||
expr: Expr::RowCondition(var_id, expr),
|
||||
expr: Expr::RowCondition(block_id),
|
||||
..
|
||||
} => (var_id, expr),
|
||||
} => block_id,
|
||||
_ => return Err(ShellError::InternalError("Expected row condition".into())),
|
||||
};
|
||||
|
||||
let block = engine_state.get_block(block_id).clone();
|
||||
let mut stack = stack.collect_captures(&block.captures);
|
||||
|
||||
input.filter(
|
||||
move |value| {
|
||||
stack.add_var(var_id, value.clone());
|
||||
|
||||
let result = eval_expression(&engine_state, &mut stack, &cond);
|
||||
if let Some(var) = block.signature.get_positional(0) {
|
||||
if let Some(var_id) = &var.var_id {
|
||||
stack.add_var(*var_id, value.clone());
|
||||
}
|
||||
}
|
||||
let result = eval_block(&engine_state, &mut stack, &block, PipelineData::new(span));
|
||||
|
||||
match result {
|
||||
Ok(result) => result.is_true(),
|
||||
Ok(result) => result.into_value(span).is_true(),
|
||||
_ => false,
|
||||
}
|
||||
},
|
||||
|
@ -1,8 +1,7 @@
|
||||
use nu_protocol::ast::{Call, PathMember};
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
|
||||
Signature, Value,
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -15,12 +14,6 @@ impl Command for ToJson {
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("to json").category(Category::Formats)
|
||||
// .named(
|
||||
// "pretty",
|
||||
// SyntaxShape::Int,
|
||||
// "Formats the JSON text with the provided indentation setting",
|
||||
// Some('p'),
|
||||
// )
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
@ -29,12 +22,12 @@ impl Command for ToJson {
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, ShellError> {
|
||||
to_json(engine_state, call, input)
|
||||
to_json(call, input)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
@ -95,143 +88,23 @@ fn json_list(input: &[Value]) -> Result<Vec<nu_json::Value>, ShellError> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn to_json(
|
||||
engine_state: &EngineState,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let name_span = call.head;
|
||||
// let pretty: Option<Value> = args.get_flag("pretty")?;
|
||||
fn to_json(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError> {
|
||||
let span = call.head;
|
||||
|
||||
//let input: Vec<Value> = input.collect();
|
||||
let value = input.into_value(span);
|
||||
|
||||
// let to_process_input = match input.len() {
|
||||
// x if x > 1 => {
|
||||
// let tag = input[0].tag.clone();
|
||||
// vec![Value:: {
|
||||
// value: UntaggedValue::Table(input),
|
||||
// tag,
|
||||
// }]
|
||||
// }
|
||||
// 1 => input,
|
||||
// _ => vec![],
|
||||
// };
|
||||
|
||||
match input {
|
||||
PipelineData::Value(value) => {
|
||||
let json_value = value_to_json_value(&value)?;
|
||||
match nu_json::to_string(&json_value) {
|
||||
Ok(serde_json_string) => Ok(Value::String {
|
||||
val: serde_json_string,
|
||||
span: name_span,
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
_ => Ok(Value::Error {
|
||||
error: ShellError::CantConvert(
|
||||
"JSON".into(),
|
||||
value.get_type().to_string(),
|
||||
name_span,
|
||||
),
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
}
|
||||
let json_value = value_to_json_value(&value)?;
|
||||
match nu_json::to_string(&json_value) {
|
||||
Ok(serde_json_string) => Ok(Value::String {
|
||||
val: serde_json_string,
|
||||
span,
|
||||
}
|
||||
PipelineData::Stream(stream) => Ok(stream
|
||||
.map(move |value| {
|
||||
if let Ok(json_value) = value_to_json_value(&value) {
|
||||
match nu_json::to_string(&json_value) {
|
||||
Ok(serde_json_string) => Value::String {
|
||||
val: serde_json_string,
|
||||
span: name_span,
|
||||
},
|
||||
_ => Value::Error {
|
||||
error: ShellError::CantConvert(
|
||||
"JSON".into(),
|
||||
value.get_type().to_string(),
|
||||
name_span,
|
||||
),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
Value::Error {
|
||||
error: ShellError::CantConvert(
|
||||
"JSON".into(),
|
||||
value.get_type().to_string(),
|
||||
name_span,
|
||||
),
|
||||
}
|
||||
}
|
||||
})
|
||||
.into_pipeline_data(engine_state.ctrlc.clone())),
|
||||
.into_pipeline_data()),
|
||||
_ => Ok(Value::Error {
|
||||
error: ShellError::CantConvert("JSON".into(), value.get_type().to_string(), span),
|
||||
}
|
||||
.into_pipeline_data()),
|
||||
}
|
||||
// input
|
||||
// // .into_iter()
|
||||
// .map(
|
||||
// move |value| {
|
||||
// let value_span = value.span().expect("non-error");
|
||||
// match value_to_json_value(&value) {
|
||||
// Ok(json_value) => {
|
||||
// match nu_json::to_string(&json_value) {
|
||||
// Ok(serde_json_string) => {
|
||||
// // if let Some(pretty_value) = &pretty {
|
||||
// // let mut pretty_format_failed = true;
|
||||
|
||||
// // if let Ok(pretty_u64) = pretty_value.as_u64() {
|
||||
// // if let Ok(serde_json_value) =
|
||||
// // serde_json::from_str::<serde_json::Value>(&serde_json_string)
|
||||
// // {
|
||||
// // let indentation_string = " ".repeat(pretty_u64 as usize);
|
||||
// // let serde_formatter =
|
||||
// // serde_json::ser::PrettyFormatter::with_indent(
|
||||
// // indentation_string.as_bytes(),
|
||||
// // );
|
||||
// // let serde_buffer = Vec::new();
|
||||
// // let mut serde_serializer =
|
||||
// // serde_json::Serializer::with_formatter(
|
||||
// // serde_buffer,
|
||||
// // serde_formatter,
|
||||
// // );
|
||||
// // let serde_json_object = json!(serde_json_value);
|
||||
|
||||
// // if let Ok(()) =
|
||||
// // serde_json_object.serialize(&mut serde_serializer)
|
||||
// // {
|
||||
// // if let Ok(ser_json_string) =
|
||||
// // String::from_utf8(serde_serializer.into_inner())
|
||||
// // {
|
||||
// // pretty_format_failed = false;
|
||||
// // serde_json_string = ser_json_string
|
||||
// // }
|
||||
// // }
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // if pretty_format_failed {
|
||||
// // return Value::error(ShellError::labeled_error(
|
||||
// // "Pretty formatting failed",
|
||||
// // "failed",
|
||||
// // pretty_value.tag(),
|
||||
// // ));
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// Value::String {
|
||||
// val: serde_json_string,
|
||||
// span: value_span,
|
||||
// }
|
||||
// }
|
||||
// _ => Value::Error {
|
||||
// error: ShellError::CantConvert("JSON".into(), value_span),
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
// _ => Value::Error {
|
||||
// error: ShellError::CantConvert("JSON".into(), value_span),
|
||||
// },
|
||||
// }
|
||||
// },
|
||||
// engine_state.ctrlc.clone(),
|
||||
// )
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
Reference in New Issue
Block a user