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

67
Cargo.lock generated
View file

@ -93,6 +93,15 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitmaps"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "031043d04099746d8db04daf1fa424b2bc8bd69d92b25962dcde24da39ab64a2"
dependencies = [
"typenum",
]
[[package]]
name = "bstr"
version = "0.2.17"
@ -378,6 +387,20 @@ dependencies = [
"libc",
]
[[package]]
name = "im"
version = "15.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "111c1983f3c5bb72732df25cddacee9b546d08325fb584b5ebd38148be7b0246"
dependencies = [
"bitmaps",
"rand_core 0.5.1",
"rand_xoshiro",
"sized-chunks",
"typenum",
"version_check",
]
[[package]]
name = "instant"
version = "0.1.11"
@ -640,6 +663,7 @@ dependencies = [
"byte-unit",
"chrono",
"chrono-humanize",
"im",
"miette",
"serde",
"thiserror",
@ -823,7 +847,7 @@ checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_core 0.6.3",
"rand_hc",
]
@ -834,9 +858,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.6.3",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
[[package]]
name = "rand_core"
version = "0.6.3"
@ -852,7 +882,16 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
"rand_core 0.6.3",
]
[[package]]
name = "rand_xoshiro"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9fcdd2e881d02f1d9390ae47ad8e5696a9e4be7b547a1da2afbc61973217004"
dependencies = [
"rand_core 0.5.1",
]
[[package]]
@ -1032,6 +1071,16 @@ dependencies = [
"libc",
]
[[package]]
name = "sized-chunks"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e"
dependencies = [
"bitmaps",
"typenum",
]
[[package]]
name = "smallvec"
version = "1.7.0"
@ -1198,6 +1247,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "typenum"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b63708a265f51345575b27fe43f9500ad611579e764c79edbc2037b1121959ec"
[[package]]
name = "unicode-linebreak"
version = "0.1.2"
@ -1237,6 +1292,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "vte"
version = "0.10.1"

View file

@ -4,26 +4,25 @@ use nu_engine::eval_block;
use nu_parser::{flatten_block, parse};
use nu_protocol::{
engine::{EngineState, EvaluationContext, Stack, StateWorkingSet},
Value,
PipelineData, Value,
};
use reedline::Completer;
const SEP: char = std::path::MAIN_SEPARATOR;
pub struct NuCompleter {
engine_state: Rc<RefCell<EngineState>>,
engine_state: Box<EngineState>,
}
impl NuCompleter {
pub fn new(engine_state: Rc<RefCell<EngineState>>) -> Self {
pub fn new(engine_state: Box<EngineState>) -> Self {
Self { engine_state }
}
}
impl Completer for NuCompleter {
fn complete(&self, line: &str, pos: usize) -> Vec<(reedline::Span, String)> {
let engine_state = self.engine_state.borrow();
let mut working_set = StateWorkingSet::new(&*engine_state);
let mut working_set = StateWorkingSet::new(&*self.engine_state);
let offset = working_set.next_span_start();
let pos = offset + pos;
let (output, _err) = parse(&mut working_set, Some("completer"), line.as_bytes(), false);
@ -49,7 +48,7 @@ impl Completer for NuCompleter {
}
}
}
for scope in &engine_state.scope {
for scope in &self.engine_state.scope {
for v in &scope.vars {
if v.0.starts_with(prefix) {
output.push((
@ -76,11 +75,10 @@ impl Completer for NuCompleter {
engine_state: self.engine_state.clone(),
stack: Stack::default(),
};
let result = eval_block(&context, &block, Value::nothing());
let result = eval_block(&context, &block, PipelineData::new());
let v: Vec<_> = match result {
Ok(Value::List { vals, .. }) => vals
.into_iter()
Ok(pd) => pd
.map(move |x| {
let s = x.as_string().expect(
"FIXME: better error handling for custom completions",

View file

@ -1,9 +1,10 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Example, ShellError, Signature, Span, SyntaxShape, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
@ -27,8 +28,8 @@ impl Command for SubCommand {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
into_binary(context, call, input)
}
@ -86,12 +87,13 @@ impl Command for SubCommand {
fn into_binary(
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
// let column_paths: Vec<CellPath> = call.rest(context, 0)?;
input.map(head, move |v| {
Ok(input
.map(move |v| {
action(v, head)
// FIXME: Add back in cell_path support
// if column_paths.is_empty() {
@ -106,6 +108,7 @@ fn into_binary(
// Ok(ret)
// }
})
.into_pipeline_data())
}
fn int_to_endian(n: i64) -> Vec<u8> {

View file

@ -2,9 +2,10 @@ use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Signature, Value,
IntoPipelineData, PipelineData, Signature, Value,
};
#[derive(Clone)]
pub struct Into;
impl Command for Into {
@ -24,12 +25,13 @@ impl Command for Into {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::String {
val: get_full_help(&Into.signature(), &[], context),
span: call.head,
})
}
.into_pipeline_data())
}
}

View file

@ -1,9 +1,10 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Example, ShellError, Signature, Span, SyntaxShape, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
@ -27,8 +28,8 @@ impl Command for SubCommand {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
into_filesize(context, call, input)
}
@ -114,12 +115,13 @@ impl Command for SubCommand {
fn into_filesize(
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
// let call_paths: Vec<ColumnPath> = args.rest(0)?;
input.map(head, move |v| {
Ok(input
.map(move |v| {
action(v, head)
// FIXME: Add back cell_path support
@ -137,6 +139,7 @@ fn into_filesize(
// Ok(ret)
// }
})
.into_pipeline_data())
}
pub fn action(input: Value, span: Span) -> Value {

View file

@ -1,9 +1,10 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Example, IntoValueStream, ShellError, Signature, Span, SyntaxShape, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
@ -27,8 +28,8 @@ impl Command for SubCommand {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
into_int(context, call, input)
}
@ -78,10 +79,8 @@ impl Command for SubCommand {
Example {
description: "Convert bool to integer",
example: "[$false, $true] | into int",
result: Some(Value::Stream {
stream: vec![Value::test_int(0), Value::test_int(1)]
.into_iter()
.into_value_stream(),
result: Some(Value::List {
vals: vec![Value::test_int(0), Value::test_int(1)],
span: Span::unknown(),
}),
},
@ -92,12 +91,13 @@ impl Command for SubCommand {
fn into_int(
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
// let column_paths: Vec<CellPath> = call.rest(context, 0)?;
input.map(head, move |v| {
Ok(input
.map(move |v| {
action(v, head)
// FIXME: Add back cell_path support
// if column_paths.is_empty() {
@ -112,6 +112,7 @@ fn into_int(
// Ok(ret)
// }
})
.into_pipeline_data())
}
pub fn action(input: Value, span: Span) -> Value {

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();
state.add_var(var_id, x);
match eval_block(&state, block, Value::nothing()) {
Ok(value) => value,
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())
}
}

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 LetEnv;
impl Command for LetEnv {
@ -28,8 +29,8 @@ impl Command for LetEnv {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let env_var = call.positional[0]
.as_string()
.expect("internal error: missing variable");
@ -44,8 +45,6 @@ impl Command for LetEnv {
//println!("Adding: {:?} to {}", rhs, var_id);
context.add_env_var(env_var, rhs);
Ok(Value::Nothing {
span: call.positional[0].span,
})
Ok(PipelineData::new())
}
}

View file

@ -4,19 +4,18 @@ use nu_engine::eval_block;
use nu_parser::parse;
use nu_protocol::{
engine::{Command, EngineState, EvaluationContext, StateWorkingSet},
Value,
PipelineData, Value,
};
use super::{From, Into, Split};
pub fn test_examples(cmd: impl Command + 'static) {
let examples = cmd.examples();
let engine_state = Rc::new(RefCell::new(EngineState::new()));
let mut engine_state = Box::new(EngineState::new());
let delta = {
// Base functions that are needed for testing
// Try to keep this working set small to keep tests running as fast as possible
let engine_state = engine_state.borrow();
let mut working_set = StateWorkingSet::new(&*engine_state);
working_set.add_decl(Box::new(From));
working_set.add_decl(Box::new(Into));
@ -28,7 +27,7 @@ pub fn test_examples(cmd: impl Command + 'static) {
working_set.render()
};
EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta);
EngineState::merge_delta(&mut *engine_state, delta);
for example in examples {
// Skip tests that don't have results to compare to
@ -38,7 +37,7 @@ pub fn test_examples(cmd: impl Command + 'static) {
let start = std::time::Instant::now();
let (block, delta) = {
let engine_state = engine_state.borrow();
let engine_state = engine_state;
let mut working_set = StateWorkingSet::new(&*engine_state);
let (output, err) = parse(&mut working_set, None, example.example.as_bytes(), false);
@ -49,16 +48,17 @@ pub fn test_examples(cmd: impl Command + 'static) {
(output, working_set.render())
};
EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta);
EngineState::merge_delta(&mut *engine_state, delta);
let state = EvaluationContext {
engine_state: engine_state.clone(),
stack: nu_protocol::engine::Stack::new(),
};
match eval_block(&state, &block, Value::nothing()) {
match eval_block(&state, &block, PipelineData::new()) {
Err(err) => panic!("test eval error in `{}`: {:?}", example.example, err),
Ok(result) => {
let result = result.into_value();
println!("input: {}", example.example);
println!("result: {:?}", result);
println!("done: {:?}", start.elapsed());

View file

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, Value};
use nu_protocol::{IntoPipelineData, PipelineData, Signature, Value};
#[derive(Clone)]
pub struct Git;
impl Command for Git {
@ -21,8 +22,8 @@ impl Command for Git {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
use std::process::Command as ProcessCommand;
use std::process::Stdio;
@ -37,17 +38,18 @@ impl Command for Git {
Ok(Value::String {
val: String::from_utf8_lossy(&result).to_string(),
span: call.head,
})
}
.into_pipeline_data())
}
Err(_err) => {
// FIXME: Move this to an external signature and add better error handling
Ok(Value::nothing())
Ok(PipelineData::new())
}
}
}
Err(_err) => {
// FIXME: Move this to an external signature and add better error handling
Ok(Value::nothing())
Ok(PipelineData::new())
}
}
}

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::{IntoPipelineData, PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct GitCheckout;
impl Command for GitCheckout {
@ -26,8 +27,8 @@ impl Command for GitCheckout {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
use std::process::Command as ProcessCommand;
use std::process::Stdio;
@ -52,17 +53,18 @@ impl Command for GitCheckout {
Ok(Value::String {
val: String::from_utf8_lossy(&result).to_string(),
span: call.head,
})
}
.into_pipeline_data())
}
Err(_err) => {
// FIXME: Move this to an external signature and add better error handling
Ok(Value::nothing())
Ok(PipelineData::new())
}
}
}
Err(_err) => {
// FIXME: Move this to an external signature and add better error handling
Ok(Value::nothing())
Ok(PipelineData::new())
}
}
}

View file

@ -5,8 +5,11 @@ use std::process::Stdio;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::IntoPipelineData;
use nu_protocol::PipelineData;
use nu_protocol::{Signature, Value};
#[derive(Clone)]
pub struct ListGitBranches;
//NOTE: this is not a real implementation :D. It's just a simple one to test with until we port the real one.
@ -27,8 +30,8 @@ impl Command for ListGitBranches {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let list_branches = ProcessCommand::new("git")
.arg("branch")
.stdout(Stdio::piped())
@ -55,15 +58,12 @@ impl Command for ListGitBranches {
})
.collect();
Ok(Value::List {
vals: lines,
span: call.head,
})
Ok(lines.into_iter().into_pipeline_data())
} else {
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
} else {
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}
}

View file

@ -1,8 +1,9 @@
use nu_engine::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 Cd;
impl Command for Cd {
@ -22,8 +23,8 @@ impl Command for Cd {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let path: Option<String> = call.opt(context, 0)?;
let path = match path {
@ -41,6 +42,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
context.add_env_var("PWD".into(), path);
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}

View file

@ -6,10 +6,11 @@ use nu_engine::CallExt;
use nu_path::canonicalize_with;
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};
use crate::filesystem::util::FileStructure;
#[derive(Clone)]
pub struct Cp;
#[allow(unused_must_use)]
@ -39,8 +40,8 @@ impl Command for Cp {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let source: String = call.req(context, 0)?;
let destination: String = call.req(context, 1)?;
let interactive = call.has_flag("interactive");
@ -202,6 +203,6 @@ impl Command for Cp {
}
}
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}

View file

@ -2,8 +2,9 @@ use chrono::{DateTime, Utc};
use nu_engine::eval_expression;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{IntoValueStream, Signature, SyntaxShape, Value};
use nu_protocol::{IntoPipelineData, PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Ls;
//NOTE: this is not a real implementation :D. It's just a simple one to test with until we port the real one.
@ -28,8 +29,8 @@ impl Command for Ls {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let pattern = if let Some(expr) = call.positional.get(0) {
let result = eval_expression(context, expr)?;
let mut result = result.as_string()?;
@ -50,8 +51,7 @@ impl Command for Ls {
let call_span = call.head;
let glob = glob::glob(&pattern).unwrap();
Ok(Value::Stream {
stream: glob
Ok(glob
.into_iter()
.map(move |x| match x {
Ok(path) => match std::fs::symlink_metadata(&path) {
@ -111,8 +111,6 @@ impl Command for Ls {
},
_ => Value::Nothing { span: call_span },
})
.into_value_stream(),
span: call_span,
})
.into_pipeline_data())
}
}

View file

@ -4,8 +4,11 @@ use std::env::current_dir;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value, ValueStream};
use nu_protocol::{
IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value, ValueStream,
};
#[derive(Clone)]
pub struct Mkdir;
impl Command for Mkdir {
@ -31,8 +34,8 @@ impl Command for Mkdir {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let path = current_dir()?;
let mut directories = call
.rest::<String>(context, 0)?
@ -67,8 +70,6 @@ impl Command for Mkdir {
}
}
let stream = ValueStream::from_stream(stream.into_iter());
let span = call.head;
Ok(Value::Stream { stream, span })
Ok(stream.into_iter().into_pipeline_data())
}
}

View file

@ -5,8 +5,9 @@ use super::util::get_interactive_confirmation;
use nu_engine::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};
#[derive(Clone)]
pub struct Mv;
#[allow(unused_must_use)]
@ -39,8 +40,8 @@ impl Command for Mv {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
// TODO: handle invalid directory or insufficient permissions when moving
let source: String = call.req(context, 0)?;
let destination: String = call.req(context, 1)?;
@ -128,7 +129,7 @@ impl Command for Mv {
move_file(call, &entry, &destination)?
}
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}

View file

@ -8,8 +8,11 @@ use super::util::get_interactive_confirmation;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, SyntaxShape, Value, ValueStream};
use nu_protocol::{
IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value, ValueStream,
};
#[derive(Clone)]
pub struct Rm;
// Where self.0 is the unexpanded target's positional index (i.e. call.positional[self.0].span)
@ -58,13 +61,13 @@ impl Command for Rm {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
rm(context, call)
}
}
fn rm(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
fn rm(context: &EvaluationContext, call: &Call) -> Result<PipelineData, ShellError> {
let trash = call.has_flag("trash");
let permanent = call.has_flag("permanent");
let interactive = call.has_flag("interactive");
@ -164,11 +167,7 @@ fn rm(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
// let temp = rm_helper(call, args).flatten();
// let temp = input.flatten(call.head, move |_| rm_helper(call, args));
Ok(Value::Stream {
stream: ValueStream::from_stream(response.into_iter()),
span: call.head,
})
Ok(response.into_iter().into_pipeline_data())
// Ok(Value::Nothing { span })
}

View file

@ -3,8 +3,9 @@ use std::fs::OpenOptions;
use nu_engine::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};
#[derive(Clone)]
pub struct Touch;
impl Command for Touch {
@ -30,8 +31,8 @@ impl Command for Touch {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<Value, ShellError> {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
let target: String = call.req(context, 0)?;
let rest: Vec<String> = call.rest(context, 1)?;
@ -47,6 +48,6 @@ impl Command for Touch {
}
}
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}

View file

@ -1,8 +1,9 @@
use nu_engine::eval_block;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Example, IntoValueStream, Signature, Span, SyntaxShape, Value};
use nu_protocol::{Example, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)]
pub struct Each;
impl Command for Each {
@ -43,8 +44,8 @@ impl Command for Each {
vec![Example {
example: "[1 2 3] | each { 2 * $it }",
description: "Multiplies elements in list",
result: Some(Value::Stream {
stream: stream_test_1.into_iter().into_value_stream(),
result: Some(Value::List {
vals: stream_test_1,
span: Span::unknown(),
}),
}]
@ -54,8 +55,8 @@ impl Command for Each {
&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");
@ -64,190 +65,186 @@ impl Command for Each {
let context = context.clone();
let span = call.head;
match input {
Value::Range { val, .. } => Ok(Value::Stream {
stream: val
.into_range_iter()?
.enumerate()
.map(move |(idx, x)| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
// match input {
// Value::Range { val, .. } => Ok(val
// .into_range_iter()?
// .enumerate()
// .map(move |(idx, x)| {
// let block = context.engine_state.get_block(block_id);
let state = context.enter_scope();
// let state = context.enter_scope();
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
if numbered {
state.add_var(
*var_id,
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span,
},
x,
],
span,
},
);
} else {
state.add_var(*var_id, x);
}
}
}
// if let Some(var) = block.signature.get_positional(0) {
// if let Some(var_id) = &var.var_id {
// if numbered {
// state.add_var(
// *var_id,
// Value::Record {
// cols: vec!["index".into(), "item".into()],
// vals: vec![
// Value::Int {
// val: idx as i64,
// span,
// },
// x,
// ],
// span,
// },
// );
// } else {
// state.add_var(*var_id, x);
// }
// }
// }
match eval_block(&state, block, Value::nothing()) {
Ok(v) => v,
Err(error) => Value::Error { error },
}
})
.into_value_stream(),
span: call.head,
}),
Value::List { vals: val, .. } => Ok(Value::Stream {
stream: val
.into_iter()
.enumerate()
.map(move |(idx, x)| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
// match eval_block(&state, block, Value::nothing()) {
// Ok(v) => v,
// Err(error) => Value::Error { error },
// }
// })
// .into_pipeline_data()),
// Value::List { vals: val, .. } => Ok(Value::Stream {
// stream: val
// .into_iter()
// .enumerate()
// .map(move |(idx, x)| {
// let engine_state = context.engine_state.borrow();
// let block = engine_state.get_block(block_id);
let state = context.enter_scope();
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
if numbered {
state.add_var(
*var_id,
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span,
},
x,
],
span,
},
);
} else {
state.add_var(*var_id, x);
}
}
}
// let state = context.enter_scope();
// if let Some(var) = block.signature.get_positional(0) {
// if let Some(var_id) = &var.var_id {
// if numbered {
// state.add_var(
// *var_id,
// Value::Record {
// cols: vec!["index".into(), "item".into()],
// vals: vec![
// Value::Int {
// val: idx as i64,
// span,
// },
// x,
// ],
// span,
// },
// );
// } else {
// state.add_var(*var_id, x);
// }
// }
// }
match eval_block(&state, block, Value::nothing()) {
Ok(v) => v,
Err(error) => Value::Error { error },
}
})
.into_value_stream(),
span: call.head,
}),
Value::Stream { stream, .. } => Ok(Value::Stream {
stream: stream
.enumerate()
.map(move |(idx, x)| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
// match eval_block(&state, block, Value::nothing()) {
// Ok(v) => v,
// Err(error) => Value::Error { error },
// }
// })
// .into_value_stream(),
// span: call.head,
// }),
// Value::Stream { stream, .. } => Ok(Value::Stream {
// stream: stream
// .enumerate()
// .map(move |(idx, x)| {
// let engine_state = context.engine_state.borrow();
// let block = engine_state.get_block(block_id);
let state = context.enter_scope();
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
if numbered {
state.add_var(
*var_id,
Value::Record {
cols: vec!["index".into(), "item".into()],
vals: vec![
Value::Int {
val: idx as i64,
span,
},
x,
],
span,
},
);
} else {
state.add_var(*var_id, x);
}
}
}
// let state = context.enter_scope();
// if let Some(var) = block.signature.get_positional(0) {
// if let Some(var_id) = &var.var_id {
// if numbered {
// state.add_var(
// *var_id,
// Value::Record {
// cols: vec!["index".into(), "item".into()],
// vals: vec![
// Value::Int {
// val: idx as i64,
// span,
// },
// x,
// ],
// span,
// },
// );
// } else {
// state.add_var(*var_id, x);
// }
// }
// }
match eval_block(&state, block, Value::nothing()) {
Ok(v) => v,
Err(error) => Value::Error { error },
}
})
.into_value_stream(),
span: call.head,
}),
Value::Record { cols, vals, .. } => {
let mut output_cols = vec![];
let mut output_vals = vec![];
// match eval_block(&state, block, Value::nothing()) {
// Ok(v) => v,
// Err(error) => Value::Error { error },
// }
// })
// .into_value_stream(),
// span: call.head,
// }),
// Value::Record { cols, vals, .. } => {
// let mut output_cols = vec![];
// let mut output_vals = vec![];
for (col, val) in cols.into_iter().zip(vals.into_iter()) {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
// for (col, val) in cols.into_iter().zip(vals.into_iter()) {
// let engine_state = context.engine_state.borrow();
// let block = engine_state.get_block(block_id);
let state = context.enter_scope();
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
state.add_var(
*var_id,
Value::Record {
cols: vec!["column".into(), "value".into()],
vals: vec![
Value::String {
val: col.clone(),
span: call.head,
},
val,
],
span: call.head,
},
);
}
}
// let state = context.enter_scope();
// if let Some(var) = block.signature.get_positional(0) {
// if let Some(var_id) = &var.var_id {
// state.add_var(
// *var_id,
// Value::Record {
// cols: vec!["column".into(), "value".into()],
// vals: vec![
// Value::String {
// val: col.clone(),
// span: call.head,
// },
// val,
// ],
// span: call.head,
// },
// );
// }
// }
match eval_block(&state, block, Value::nothing())? {
Value::Record {
mut cols, mut vals, ..
} => {
// TODO check that the lengths match when traversing record
output_cols.append(&mut cols);
output_vals.append(&mut vals);
}
x => {
output_cols.push(col);
output_vals.push(x);
}
}
}
// match eval_block(&state, block, Value::nothing())? {
// Value::Record {
// mut cols, mut vals, ..
// } => {
// // TODO check that the lengths match when traversing record
// output_cols.append(&mut cols);
// output_vals.append(&mut vals);
// }
// x => {
// output_cols.push(col);
// output_vals.push(x);
// }
// }
// }
Ok(Value::Record {
cols: output_cols,
vals: output_vals,
span: call.head,
})
}
x => {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block_id);
// Ok(Value::Record {
// cols: output_cols,
// vals: output_vals,
// span: call.head,
// })
// }
// x => {
// let engine_state = context.engine_state.borrow();
// let block = engine_state.get_block(block_id);
let state = context.enter_scope();
if let Some(var) = block.signature.get_positional(0) {
if let Some(var_id) = &var.var_id {
state.add_var(*var_id, x);
}
}
// let state = context.enter_scope();
// if let Some(var) = block.signature.get_positional(0) {
// if let Some(var_id) = &var.var_id {
// state.add_var(*var_id, x);
// }
// }
eval_block(&state, block, Value::nothing())
}
}
// eval_block(&state, block, Value::nothing())
// }
// }
}
}

View file

@ -1,8 +1,9 @@
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_protocol::{PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Get;
impl Command for Get {
@ -26,8 +27,8 @@ impl Command for Get {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let cell_path: CellPath = call.req(context, 0)?;
input.follow_cell_path(&cell_path.members)

View file

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, Value};
use nu_protocol::{IntoPipelineData, PipelineData, Signature, Value};
#[derive(Clone)]
pub struct Length;
impl Command for Length {
@ -21,33 +22,19 @@ impl Command for Length {
&self,
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
match input {
Value::List { vals: val, .. } => {
let length = val.len();
Ok(Value::Int {
val: length as i64,
span: call.head,
})
}
Value::Stream { stream, .. } => {
let length = stream.count();
Ok(Value::Int {
val: length as i64,
span: call.head,
})
}
Value::Nothing { .. } => Ok(Value::Int {
PipelineData::Value(Value::Nothing { .. }) => Ok(Value::Int {
val: 0,
span: call.head,
}),
}
.into_pipeline_data()),
_ => Ok(Value::Int {
val: 1,
val: input.count() as i64,
span: call.head,
}),
}
.into_pipeline_data()),
}
}
}

View file

@ -3,8 +3,9 @@ use std::rc::Rc;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{ShellError, Signature, Value, ValueStream};
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Signature, Value, ValueStream};
#[derive(Clone)]
pub struct Lines;
const SPLIT_CHAR: char = '\n';
@ -26,15 +27,15 @@ impl Command for Lines {
&self,
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let span = call.head;
match input {
#[allow(clippy::needless_collect)]
// Collect is needed because the string may not live long enough for
// the Rc structure to continue using it. If split could take ownership
// of the split values, then this wouldn't be needed
Value::String { val, span } => {
PipelineData::Value(Value::String { val, span }) => {
let lines = val
.split(SPLIT_CHAR)
.map(|s| s.to_string())
@ -48,12 +49,9 @@ impl Command for Lines {
}
});
Ok(Value::Stream {
stream: ValueStream(Rc::new(RefCell::new(iter))),
span,
})
Ok(iter.into_pipeline_data())
}
Value::Stream { stream, span: _ } => {
PipelineData::Stream(stream) => {
let iter = stream
.into_iter()
.filter_map(|value| {
@ -79,12 +77,9 @@ impl Command for Lines {
})
.flatten();
Ok(Value::Stream {
stream: ValueStream(Rc::new(RefCell::new(iter))),
span,
})
Ok(iter.into_pipeline_data())
}
val => Err(ShellError::UnsupportedInput(
PipelineData::Value(val) => Err(ShellError::UnsupportedInput(
format!("Not supported input: {}", val.as_string()?),
call.head,
)),

View file

@ -1,8 +1,11 @@
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Example, IntoValueStream, ShellError, Signature, Span, SyntaxShape, Value};
use nu_protocol::{
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct Select;
impl Command for Select {
@ -26,8 +29,8 @@ impl Command for Select {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let columns: Vec<CellPath> = call.rest(context, 0)?;
let span = call.head;
@ -50,16 +53,20 @@ impl Command for Select {
}
}
fn select(span: Span, columns: Vec<CellPath>, input: Value) -> Result<Value, ShellError> {
fn select(
span: Span,
columns: Vec<CellPath>,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
if columns.is_empty() {
return Err(ShellError::CantFindColumn(span, input.span()?));
return Err(ShellError::CantFindColumn(span, span)); //FIXME?
}
match input {
Value::List {
PipelineData::Value(Value::List {
vals: input_vals,
span,
} => {
}) => {
let mut output = vec![];
for input_val in input_vals {
@ -76,10 +83,9 @@ fn select(span: Span, columns: Vec<CellPath>, input: Value) -> Result<Value, She
output.push(Value::Record { cols, vals, span })
}
Ok(Value::List { vals: output, span })
Ok(output.into_iter().into_pipeline_data())
}
Value::Stream { stream, span } => Ok(Value::Stream {
stream: stream
PipelineData::Stream(stream) => Ok(stream
.map(move |x| {
let mut cols = vec![];
let mut vals = vec![];
@ -99,10 +105,8 @@ fn select(span: Span, columns: Vec<CellPath>, input: Value) -> Result<Value, She
Value::Record { cols, vals, span }
})
.into_value_stream(),
span,
}),
v => {
.into_pipeline_data()),
PipelineData::Value(v) => {
let mut cols = vec![];
let mut vals = vec![];
@ -114,7 +118,7 @@ fn select(span: Span, columns: Vec<CellPath>, input: Value) -> Result<Value, She
vals.push(result);
}
Ok(Value::Record { cols, vals, span })
Ok(Value::Record { cols, vals, span }.into_pipeline_data())
}
}
}

View file

@ -1,8 +1,9 @@
use nu_engine::eval_expression;
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{IntoValueStream, ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Where;
impl Command for Where {
@ -22,8 +23,8 @@ impl Command for Where {
&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].clone();
let context = context.enter_scope();
@ -37,8 +38,7 @@ impl Command for Where {
};
match input {
Value::Stream { stream, span } => {
let output_stream = stream
PipelineData::Stream(stream) => Ok(stream
.filter(move |value| {
context.add_var(var_id, value.clone());
@ -49,15 +49,8 @@ impl Command for Where {
_ => false,
}
})
.into_value_stream();
Ok(Value::Stream {
stream: output_stream,
span,
})
}
Value::List { vals, span } => {
let output_stream = vals
.into_pipeline_data()),
PipelineData::Value(Value::List { vals, span }) => Ok(vals
.into_iter()
.filter(move |value| {
context.add_var(var_id, value.clone());
@ -69,22 +62,16 @@ impl Command for Where {
_ => false,
}
})
.into_value_stream();
Ok(Value::Stream {
stream: output_stream,
span,
})
}
x => {
.into_pipeline_data()),
PipelineData::Value(x) => {
context.add_var(var_id, x.clone());
let result = eval_expression(&context, &cond)?;
if result.is_true() {
Ok(x)
Ok(x.into_pipeline_data())
} else {
Ok(Value::Nothing { span: call.head })
Ok(PipelineData::new())
}
}
}

View file

@ -1,8 +1,9 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{IntoValueStream, Signature, SyntaxShape, Value};
use nu_protocol::{IntoPipelineData, PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Wrap;
impl Command for Wrap {
@ -22,38 +23,33 @@ impl Command for Wrap {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let span = call.head;
let name: String = call.req(context, 0)?;
match input {
Value::List { vals, .. } => Ok(Value::List {
vals: vals
PipelineData::Value(Value::List { vals, .. }) => Ok(vals
.into_iter()
.map(move |x| Value::Record {
cols: vec![name.clone()],
vals: vec![x],
span,
})
.collect(),
span,
}),
Value::Stream { stream, .. } => Ok(Value::Stream {
stream: stream
.into_pipeline_data()),
PipelineData::Stream(stream) => Ok(stream
.map(move |x| Value::Record {
cols: vec![name.clone()],
vals: vec![x],
span,
})
.into_value_stream(),
span,
}),
_ => Ok(Value::Record {
.into_pipeline_data()),
PipelineData::Value(input) => Ok(Value::Record {
cols: vec![name],
vals: vec![input],
span,
}),
}
.into_pipeline_data()),
}
}
}

View file

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

View file

@ -1,7 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Example, IntoValueStream, ShellError, Signature, Span, Value};
use nu_protocol::{Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value};
#[derive(Clone)]
pub struct FromJson;
impl Command for FromJson {
@ -69,8 +70,8 @@ impl Command for FromJson {
&self,
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let span = input.span()?;
let mut string_input = input.collect_string();
string_input.push('\n');
@ -79,8 +80,7 @@ impl Command for FromJson {
if call.has_flag("objects") {
#[allow(clippy::needless_collect)]
let lines: Vec<String> = string_input.lines().map(|x| x.to_string()).collect();
Ok(Value::Stream {
stream: lines
Ok(lines
.into_iter()
.map(move |mut x| {
x.push('\n');
@ -89,11 +89,9 @@ impl Command for FromJson {
Err(error) => Value::Error { error },
}
})
.into_value_stream(),
span,
})
.into_pipeline_data())
} else {
convert_string_to_value(string_input, span)
Ok(convert_string_to_value(string_input, span)?.into_pipeline_data())
}
}
}

View file

@ -1,8 +1,11 @@
use nu_engine::eval_expression;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Example, ShellError, Signature, Span, SyntaxShape, Value};
use nu_protocol::{
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct BuildString;
impl Command for BuildString {
@ -43,8 +46,8 @@ impl Command for BuildString {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let output = call
.positional
.iter()
@ -54,7 +57,8 @@ impl Command for BuildString {
Ok(Value::String {
val: output.join(""),
span: call.head,
})
}
.into_pipeline_data())
}
}

View file

@ -4,8 +4,11 @@ use unicode_segmentation::UnicodeSegmentation;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Example, ShellError, Signature, Span, Type, Value};
use nu_protocol::{
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
pub struct Size;
impl Command for Size {
@ -25,8 +28,8 @@ impl Command for Size {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<Value, ShellError> {
input: PipelineData,
) -> Result<PipelineData, ShellError> {
size(context, call, input)
}
@ -98,9 +101,14 @@ impl Command for Size {
}
}
fn size(_context: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> {
fn size(
_context: &EvaluationContext,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
input.map(span, move |v| match v.as_string() {
Ok(input
.map(move |v| match v.as_string() {
Ok(s) => count(&s, span),
Err(_) => Value::Error {
error: ShellError::PipelineMismatch {
@ -110,6 +118,7 @@ fn size(_context: &EvaluationContext, call: &Call, input: Value) -> Result<Value
},
},
})
.into_pipeline_data())
}
fn count(contents: &str, span: Span) -> Value {

View file

@ -1,9 +1,10 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Example, ShellError, Signature, Span, Type, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
@ -40,16 +41,21 @@ impl Command for SubCommand {
&self,
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
split_chars(call, input)
}
}
fn split_chars(call: &Call, input: Value) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
fn split_chars(
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let span = call.head;
Ok(input.flat_map(span, move |x| split_chars_helper(&x, span)))
Ok(input
.flat_map(move |x| split_chars_helper(&x, span))
.into_pipeline_data())
}
fn split_chars_helper(v: &Value, name: Span) -> Vec<Value> {

View file

@ -2,9 +2,10 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
@ -35,8 +36,8 @@ impl Command for SubCommand {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
split_column(context, call, input)
}
}
@ -44,16 +45,16 @@ impl Command for SubCommand {
fn split_column(
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let name_span = call.head;
let separator: Spanned<String> = call.req(context, 0)?;
let rest: Vec<Spanned<String>> = call.rest(context, 1)?;
let collapse_empty = call.has_flag("collapse-empty");
input.map(name_span, move |x| {
split_column_helper(&x, &separator, &rest, collapse_empty, name_span)
})
Ok(input
.map(move |x| split_column_helper(&x, &separator, &rest, collapse_empty, name_span))
.into_pipeline_data())
}
fn split_column_helper(

View file

@ -2,7 +2,7 @@ use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Signature, Value,
IntoPipelineData, PipelineData, Signature, Value,
};
#[derive(Clone)]
@ -25,12 +25,13 @@ impl Command for SplitCommand {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(Value::String {
val: get_full_help(&SplitCommand.signature(), &SplitCommand.examples(), context),
span: call.head,
})
}
.into_pipeline_data())
}
}

View file

@ -2,9 +2,10 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
@ -28,8 +29,8 @@ impl Command for SubCommand {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
split_row(context, call, input)
}
}
@ -37,14 +38,14 @@ impl Command for SubCommand {
fn split_row(
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let name_span = call.head;
let separator: Spanned<String> = call.req(context, 0)?;
Ok(input.flat_map(name_span, move |x| {
split_row_helper(&x, &separator, name_span)
}))
Ok(input
.flat_map(move |x| split_row_helper(&x, &separator, name_span))
.into_pipeline_data())
}
fn split_row_helper(v: &Value, separator: &Spanned<String>, name: Span) -> Vec<Value> {

View file

@ -3,8 +3,9 @@ use std::time::Instant;
use nu_engine::eval_block;
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 Benchmark;
impl Command for Benchmark {
@ -28,21 +29,18 @@ impl Command for Benchmark {
&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 = call.positional[0]
.as_block()
.expect("internal error: expected block");
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);
let block = context.engine_state.get_block(block);
let state = context.enter_scope();
let start_time = Instant::now();
eval_block(&state, block, Value::nothing())?;
eval_block(&state, block, PipelineData::new())?;
let end_time = Instant::now();
println!("{} ms", (end_time - start_time).as_millis());
Ok(Value::Nothing {
span: call.positional[0].span,
})
Ok(PipelineData::new())
}
}

View file

@ -1,10 +1,11 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Example, ShellError, Signature, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Value,
};
use sysinfo::{ProcessExt, System, SystemExt};
#[derive(Clone)]
pub struct Ps;
impl Command for Ps {
@ -31,8 +32,8 @@ impl Command for Ps {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
run_ps(call)
}
@ -45,7 +46,7 @@ impl Command for Ps {
}
}
fn run_ps(call: &Call) -> Result<Value, ShellError> {
fn run_ps(call: &Call) -> Result<PipelineData, ShellError> {
let span = call.head;
let long = call.has_flag("long");
let mut sys = System::new_all();
@ -124,5 +125,5 @@ fn run_ps(call: &Call) -> Result<Value, ShellError> {
}
}
Ok(Value::List { vals: output, span })
Ok(output.into_iter().into_pipeline_data())
}

View file

@ -11,12 +11,13 @@ use nu_protocol::{
engine::{Command, EvaluationContext},
ShellError, Signature, SyntaxShape, Value,
};
use nu_protocol::{Span, ValueStream};
use nu_protocol::{IntoPipelineData, PipelineData, Span, ValueStream};
use nu_engine::eval_expression;
const OUTPUT_BUFFER_SIZE: usize = 8192;
#[derive(Clone)]
pub struct External;
impl Command for External {
@ -38,8 +39,8 @@ impl Command for External {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<Value, ShellError> {
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let command = ExternalCommand::try_new(call, context)?;
command.run_with_input(input)
}
@ -82,7 +83,7 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
.collect()
}
pub fn run_with_input(&self, input: Value) -> Result<Value, ShellError> {
pub fn run_with_input(&self, input: PipelineData) -> Result<PipelineData, ShellError> {
let mut process = self.create_command();
// TODO. We don't have a way to know the current directory
@ -101,11 +102,11 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
// If there is an input from the pipeline. The stdin from the process
// is piped so it can be used to send the input information
if let Value::String { .. } = input {
if let PipelineData::Value(Value::String { .. }) = input {
process.stdin(Stdio::piped());
}
if let Value::Stream { .. } = input {
if let PipelineData::Stream { .. } = input {
process.stdin(Stdio::piped());
}
@ -116,20 +117,8 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
)),
Ok(mut child) => {
// if there is a string or a stream, that is sent to the pipe std
match input {
Value::String { val, span: _ } => {
if let Some(mut stdin_write) = child.stdin.take() {
self.write_to_stdin(&mut stdin_write, val.as_bytes())?
}
}
Value::Binary { val, span: _ } => {
if let Some(mut stdin_write) = child.stdin.take() {
self.write_to_stdin(&mut stdin_write, &val)?
}
}
Value::Stream { stream, span: _ } => {
if let Some(mut stdin_write) = child.stdin.take() {
for value in stream {
for value in input {
match value {
Value::String { val, span: _ } => {
self.write_to_stdin(&mut stdin_write, val.as_bytes())?
@ -141,9 +130,6 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
}
}
}
}
_ => (),
}
// If this external is not the last expression, then its output is piped to a channel
// and we create a ValueStream that can be consumed
@ -185,12 +171,9 @@ impl<'call, 'contex> ExternalCommand<'call, 'contex> {
});
// The ValueStream is consumed by the next expression in the pipeline
Value::Stream {
stream: ValueStream(Rc::new(RefCell::new(ChannelReceiver::new(rx)))),
span: Span::unknown(),
}
ChannelReceiver::new(rx).into_pipeline_data()
} else {
Value::nothing()
PipelineData::new()
};
match child.wait() {

View file

@ -1,10 +1,11 @@
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
Example, ShellError, Signature, Span, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
};
use sysinfo::{ComponentExt, DiskExt, NetworkExt, ProcessorExt, System, SystemExt, UserExt};
#[derive(Clone)]
pub struct Sys;
impl Command for Sys {
@ -26,8 +27,8 @@ impl Command for Sys {
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
run_sys(call)
}
@ -40,7 +41,7 @@ impl Command for Sys {
}
}
fn run_sys(call: &Call) -> Result<Value, ShellError> {
fn run_sys(call: &Call) -> Result<PipelineData, ShellError> {
let span = call.head;
let mut sys = System::new();
@ -76,7 +77,8 @@ fn run_sys(call: &Call) -> Result<Value, ShellError> {
cols: headers,
vals: values,
span,
})
}
.into_pipeline_data())
}
pub fn trim_cstyle_null(s: String) -> String {

View file

@ -8,6 +8,7 @@ use nu_protocol::{
use nu_term_grid::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions};
use terminal_size::{Height, Width};
#[derive(Clone)]
pub struct Griddle;
impl Command for Griddle {

View file

@ -5,6 +5,7 @@ use nu_table::StyledString;
use std::collections::HashMap;
use terminal_size::{Height, Width};
#[derive(Clone)]
pub struct Table;
//NOTE: this is not a real implementation :D. It's just a simple one to test with until we port the real one.

View file

@ -26,11 +26,10 @@ fn generate_doc(name: &str, context: &EvaluationContext) -> (Vec<String>, Vec<Va
let mut cols = vec![];
let mut vals = vec![];
let engine_state = context.engine_state.borrow();
let command = engine_state
let command = context
.engine_state
.find_decl(name.as_bytes())
.map(|decl_id| engine_state.get_decl(decl_id))
.map(|decl_id| context.engine_state.get_decl(decl_id))
.unwrap_or_else(|| panic!("Expected command '{}' from names to be in registry", name));
cols.push("name".to_string());

View file

@ -1,6 +1,8 @@
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
use nu_protocol::engine::EvaluationContext;
use nu_protocol::{Range, ShellError, Span, Spanned, Type, Unit, Value};
use nu_protocol::{
IntoPipelineData, PipelineData, Range, ShellError, Span, Spanned, Type, Unit, Value,
};
use crate::get_full_help;
@ -16,17 +18,21 @@ pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
}
}
fn eval_call(context: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> {
let engine_state = context.engine_state.borrow();
let decl = engine_state.get_decl(call.decl_id);
fn eval_call(
context: &EvaluationContext,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let decl = context.engine_state.get_decl(call.decl_id);
if call.named.iter().any(|(flag, _)| flag.item == "help") {
let full_help = get_full_help(&decl.signature(), &decl.examples(), context);
Ok(Value::String {
val: full_help,
span: call.head,
})
}
.into_pipeline_data())
} else if let Some(block_id) = decl.get_block_id() {
let state = context.enter_scope();
let mut state = context.enter_scope();
for (arg, param) in call.positional.iter().zip(
decl.signature()
.required_positional
@ -102,8 +108,7 @@ fn eval_call(context: &EvaluationContext, call: &Call, input: Value) -> Result<V
}
}
}
let engine_state = state.engine_state.borrow();
let block = engine_state.get_block(block_id);
let block = context.engine_state.get_block(block_id);
eval_block(&state, block, input)
} else {
decl.run(context, call, input)
@ -115,16 +120,15 @@ fn eval_external(
name: &str,
name_span: &Span,
args: &[Expression],
input: Value,
input: PipelineData,
last_expression: bool,
) -> Result<Value, ShellError> {
let engine_state = context.engine_state.borrow();
let decl_id = engine_state
) -> Result<PipelineData, ShellError> {
let decl_id = context
.engine_state
.find_decl("run_external".as_bytes())
.ok_or_else(|| ShellError::ExternalNotSupported(*name_span))?;
let command = engine_state.get_decl(decl_id);
let command = context.engine_state.get_decl(decl_id);
let mut call = Call::new();
@ -216,9 +220,20 @@ pub fn eval_expression(
value.follow_cell_path(&cell_path.tail)
}
Expr::RowCondition(_, expr) => eval_expression(context, expr),
Expr::Call(call) => eval_call(context, call, Value::nothing()),
Expr::Call(call) => {
// FIXME: protect this collect with ctrl-c
Ok(Value::List {
vals: eval_call(context, call, PipelineData::new())?.collect(),
span: expr.span,
})
}
Expr::ExternalCall(name, span, args) => {
eval_external(context, name, span, args, Value::nothing(), true)
// FIXME: protect this collect with ctrl-c
Ok(Value::List {
vals: eval_external(context, name, span, args, PipelineData::new(), true)?
.collect(),
span: expr.span,
})
}
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
Expr::BinaryOp(lhs, op, rhs) => {
@ -249,11 +264,15 @@ pub fn eval_expression(
}
}
Expr::Subexpression(block_id) => {
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();
eval_block(&state, block, Value::nothing())
// FIXME: protect this collect with ctrl-c
Ok(Value::List {
vals: eval_block(&state, block, PipelineData::new())?.collect(),
span: expr.span,
})
}
Expr::Block(block_id) => Ok(Value::Block {
val: *block_id,
@ -313,8 +332,8 @@ pub fn eval_expression(
pub fn eval_block(
context: &EvaluationContext,
block: &Block,
mut input: Value,
) -> Result<Value, ShellError> {
mut input: PipelineData,
) -> Result<PipelineData, ShellError> {
for stmt in block.stmts.iter() {
if let Statement::Pipeline(pipeline) = stmt {
for (i, elem) in pipeline.expressions.iter().enumerate() {
@ -340,7 +359,7 @@ pub fn eval_block(
}
elem => {
input = eval_expression(context, elem)?;
input = eval_expression(context, elem)?.into_pipeline_data();
}
}
}

View file

@ -7,6 +7,7 @@ use nu_protocol::{
};
#[cfg(test)]
#[derive(Clone)]
pub struct Let;
#[cfg(test)]
@ -33,8 +34,8 @@ impl Command for Let {
&self,
_context: &nu_protocol::engine::EvaluationContext,
_call: &nu_protocol::ast::Call,
_input: nu_protocol::Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
_input: nu_protocol::PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
todo!()
}
}

View file

@ -12,3 +12,4 @@ serde = {version = "1.0.130", features = ["derive"]}
chrono = { version="0.4.19", features=["serde"] }
chrono-humanize = "0.2.1"
byte-unit = "4.0.9"
im = "15.0.0"

View file

@ -1,8 +1,8 @@
use crate::{ast::Call, value::Value, BlockId, Example, ShellError, Signature};
use crate::{ast::Call, value::Value, BlockId, Example, PipelineData, ShellError, Signature};
use super::EvaluationContext;
pub trait Command: Send + Sync {
pub trait Command: Send + Sync + CommandClone {
fn name(&self) -> &str;
fn signature(&self) -> Signature {
@ -19,8 +19,8 @@ pub trait Command: Send + Sync {
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<Value, ShellError>;
input: PipelineData,
) -> Result<PipelineData, ShellError>;
fn is_binary(&self) -> bool {
false
@ -55,3 +55,22 @@ pub trait Command: Send + Sync {
None
}
}
pub trait CommandClone {
fn clone_box(&self) -> Box<dyn Command>;
}
impl<T> CommandClone for T
where
T: 'static + Command + Clone,
{
fn clone_box(&self) -> Box<dyn Command> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn Command> {
fn clone(&self) -> Box<dyn Command> {
self.clone_box()
}
}

View file

@ -3,13 +3,14 @@ use crate::{ast::Block, BlockId, DeclId, Example, Signature, Span, Type, VarId};
use core::panic;
use std::{collections::HashMap, slice::Iter};
#[derive(Clone)]
pub struct EngineState {
files: Vec<(String, usize, usize)>,
file_contents: Vec<u8>,
vars: Vec<Type>,
decls: Vec<Box<dyn Command>>,
blocks: Vec<Block>,
pub scope: Vec<ScopeFrame>,
files: im::Vector<(String, usize, usize)>,
file_contents: im::Vector<(Vec<u8>, usize, usize)>,
vars: im::Vector<Type>,
decls: im::Vector<Box<dyn Command + 'static>>,
blocks: im::Vector<Block>,
pub scope: im::Vector<ScopeFrame>,
}
// Tells whether a decl etc. is visible or not
@ -53,7 +54,7 @@ impl Visibility {
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ScopeFrame {
pub vars: HashMap<Vec<u8>, VarId>,
predecls: HashMap<Vec<u8>, DeclId>, // temporary storage for predeclarations
@ -95,12 +96,12 @@ impl Default for EngineState {
impl EngineState {
pub fn new() -> Self {
Self {
files: vec![],
file_contents: vec![],
vars: vec![],
decls: vec![],
blocks: vec![],
scope: vec![ScopeFrame::new()],
files: im::vector![],
file_contents: im::vector![],
vars: im::vector![],
decls: im::vector![],
blocks: im::vector![],
scope: im::vector![ScopeFrame::new()],
}
}
@ -112,7 +113,7 @@ impl EngineState {
this.vars.extend(delta.vars);
this.blocks.extend(delta.blocks);
if let Some(last) = this.scope.last_mut() {
if let Some(last) = this.scope.back_mut() {
let first = delta.scope.remove(0);
for item in first.decls.into_iter() {
last.decls.insert(item.0, item.1);
@ -165,9 +166,11 @@ impl EngineState {
}
pub fn print_contents(&self) {
let string = String::from_utf8_lossy(&self.file_contents);
for (contents, _, _) in self.file_contents.iter() {
let string = String::from_utf8_lossy(&contents);
println!("{}", string);
}
}
pub fn find_decl(&self, name: &[u8]) -> Option<DeclId> {
let mut visibility: Visibility = Visibility::new();
@ -200,7 +203,13 @@ impl EngineState {
}
pub fn get_span_contents(&self, span: &Span) -> &[u8] {
&self.file_contents[span.start..span.end]
for (contents, start, finish) in &self.file_contents {
if span.start >= *start && span.start < *finish {
return &contents[(span.start - start)..(span.end - start)];
}
}
panic!("internal error: span missing in file contents cache")
}
pub fn get_var(&self, var_id: VarId) -> &Type {
@ -256,7 +265,7 @@ impl EngineState {
self.file_contents.len()
}
pub fn files(&self) -> Iter<(String, usize, usize)> {
pub fn files(&self) -> impl Iterator<Item = &(String, usize, usize)> {
self.files.iter()
}
@ -273,8 +282,11 @@ impl EngineState {
pub fn get_file_source(&self, file_id: usize) -> String {
for file in self.files.iter().enumerate() {
if file.0 == file_id {
let output =
String::from_utf8_lossy(&self.file_contents[file.1 .1..file.1 .2]).to_string();
let contents = self.get_span_contents(&Span {
start: file.1 .1,
end: file.1 .2,
});
let output = String::from_utf8_lossy(contents).to_string();
return output;
}
@ -286,12 +298,13 @@ impl EngineState {
#[allow(unused)]
pub(crate) fn add_file(&mut self, filename: String, contents: Vec<u8>) -> usize {
let next_span_start = self.next_span_start();
let next_span_end = next_span_start + contents.len();
self.file_contents.extend(&contents);
self.file_contents
.push_back((contents, next_span_start, next_span_end));
let next_span_end = self.next_span_start();
self.files.push((filename, next_span_start, next_span_end));
self.files
.push_back((filename, next_span_start, next_span_end));
self.num_files() - 1
}
@ -304,7 +317,7 @@ pub struct StateWorkingSet<'a> {
pub struct StateDelta {
files: Vec<(String, usize, usize)>,
pub(crate) file_contents: Vec<u8>,
pub(crate) file_contents: Vec<(Vec<u8>, usize, usize)>,
vars: Vec<Type>, // indexed by VarId
decls: Vec<Box<dyn Command>>, // indexed by DeclId
blocks: Vec<Block>, // indexed by BlockId
@ -520,10 +533,11 @@ impl<'a> StateWorkingSet<'a> {
pub fn add_file(&mut self, filename: String, contents: &[u8]) -> usize {
let next_span_start = self.next_span_start();
let next_span_end = next_span_start + contents.len();
self.delta.file_contents.extend(contents);
let next_span_end = self.next_span_start();
self.delta
.file_contents
.push((contents.to_vec(), next_span_start, next_span_end));
self.delta
.files
@ -535,11 +549,17 @@ impl<'a> StateWorkingSet<'a> {
pub fn get_span_contents(&self, span: Span) -> &[u8] {
let permanent_end = self.permanent_state.next_span_start();
if permanent_end <= span.start {
&self.delta.file_contents[(span.start - permanent_end)..(span.end - permanent_end)]
} else {
&self.permanent_state.file_contents[span.start..span.end]
for (contents, start, finish) in &self.delta.file_contents {
if (span.start >= *start) && (span.start < *finish) {
return &contents[(span.start - permanent_end)..(span.end - permanent_end)];
}
}
} else {
return self.permanent_state.get_span_contents(&span);
}
panic!("internal error: missing span contents in file cache")
}
pub fn enter_scope(&mut self) {
self.delta.enter_scope();

View file

@ -5,7 +5,7 @@ use crate::{Example, ShellError, Signature, Value, VarId};
#[derive(Clone)]
pub struct EvaluationContext {
pub engine_state: Rc<RefCell<EngineState>>,
pub engine_state: Box<EngineState>,
pub stack: Stack,
}
@ -21,25 +21,11 @@ impl EvaluationContext {
}
}
pub fn add_var(&self, var_id: VarId, value: Value) {
// We need to make values concreate before we assign them to variables, as stream values
// will drain and remain drained.
//
// TODO: find a good home for converting a stream->list when storing into a variable
// TODO: add ctrl-c support when setting a var
let value = match value {
Value::Stream { stream, span } => Value::List {
vals: stream.collect(),
span,
},
x => x,
};
pub fn add_var(&mut self, var_id: VarId, value: Value) {
self.stack.add_var(var_id, value);
}
pub fn add_env_var(&self, var: String, value: String) {
pub fn add_env_var(&mut self, var: String, value: String) {
self.stack.add_env_var(var, value);
}
@ -48,23 +34,22 @@ impl EvaluationContext {
}
pub fn get_signatures(&self) -> Vec<Signature> {
self.engine_state.borrow().get_signatures()
self.engine_state.get_signatures()
}
pub fn get_signatures_with_examples(&self) -> Vec<(Signature, Vec<Example>)> {
self.engine_state.borrow().get_signatures_with_examples()
self.engine_state.get_signatures_with_examples()
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct StackFrame {
pub vars: HashMap<VarId, Value>,
pub env_vars: HashMap<String, String>,
pub parent: Option<Stack>,
}
#[derive(Clone, Debug)]
pub struct Stack(Rc<RefCell<StackFrame>>);
pub struct Stack(Vec<StackFrame>);
impl Default for Stack {
fn default() -> Self {
@ -74,64 +59,77 @@ impl Default for Stack {
impl Stack {
pub fn new() -> Stack {
Stack(Rc::new(RefCell::new(StackFrame {
Stack(vec![StackFrame {
vars: HashMap::new(),
env_vars: HashMap::new(),
parent: None,
})))
}])
}
pub fn get_var(&self, var_id: VarId) -> Result<Value, ShellError> {
let this = self.0.borrow();
match this.vars.get(&var_id) {
Some(v) => Ok(v.clone()),
_ => {
if let Some(parent) = &this.parent {
parent.get_var(var_id)
} else {
for frame in self.0.iter().rev() {
if let Some(v) = frame.vars.get(&var_id) {
return Ok(v.clone());
}
}
Err(ShellError::InternalError("variable not found".into()))
}
}
}
pub fn add_var(&mut self, var_id: VarId, value: Value) {
let frame = self
.0
.last_mut()
.expect("internal error: can't access stack frame");
frame.vars.insert(var_id, value);
}
pub fn add_var(&self, var_id: VarId, value: Value) {
let mut this = self.0.borrow_mut();
this.vars.insert(var_id, value);
pub fn add_env_var(&mut self, var: String, value: String) {
let frame = self
.0
.last_mut()
.expect("internal error: can't access stack frame");
frame.env_vars.insert(var, value);
}
pub fn add_env_var(&self, var: String, value: String) {
let mut this = self.0.borrow_mut();
this.env_vars.insert(var, value);
}
pub fn enter_scope(self) -> Stack {
Stack(Rc::new(RefCell::new(StackFrame {
pub fn enter_scope(&self) -> Stack {
// FIXME: VERY EXPENSIVE to clone entire stack
let mut output = self.clone();
output.0.push(StackFrame {
vars: HashMap::new(),
env_vars: HashMap::new(),
parent: Some(self),
})))
});
output
}
pub fn get_env_vars(&self) -> HashMap<String, String> {
self.0.borrow().env_vars.clone()
let mut output = HashMap::new();
for frame in &self.0 {
output.extend(frame.env_vars.clone().into_iter());
}
output
}
pub fn get_env_var(&self, name: &str) -> Option<String> {
self.0.borrow().env_vars.get(name).cloned()
for frame in self.0.iter().rev() {
if let Some(v) = frame.env_vars.get(name) {
return Some(v.to_string());
}
}
None
}
pub fn print_stack(&self) {
for frame in self.0.iter().rev() {
println!("===frame===");
println!("vars:");
for (var, val) in &self.0.borrow().vars {
for (var, val) in &frame.vars {
println!(" {}: {:?}", var, val);
}
println!("env vars:");
for (var, val) in &self.0.borrow().env_vars {
for (var, val) in &frame.env_vars {
println!(" {}: {:?}", var, val);
}
if let Some(parent) = &self.0.borrow().parent {
parent.print_stack()
}
}
}

View file

@ -2,6 +2,7 @@ pub mod ast;
pub mod engine;
mod example;
mod id;
mod pipeline_data;
mod shell_error;
mod signature;
mod span;
@ -12,6 +13,7 @@ pub use value::Value;
pub use example::*;
pub use id::*;
pub use pipeline_data::*;
pub use shell_error::*;
pub use signature::*;
pub use span::*;

View file

@ -0,0 +1,62 @@
use crate::{Span, Value, ValueStream};
pub enum PipelineData {
Value(Value),
Stream(ValueStream),
}
impl PipelineData {
pub fn new() -> PipelineData {
PipelineData::Value(Value::nothing())
}
pub fn into_value(self) -> Value {
match self {
PipelineData::Value(v) => v,
PipelineData::Stream(s) => Value::List {
vals: s.collect(),
span: Span::unknown(), // FIXME?
},
}
}
}
impl Default for PipelineData {
fn default() -> Self {
PipelineData::new()
}
}
impl Iterator for PipelineData {
type Item = Value;
fn next(&mut self) -> Option<Self::Item> {
match self {
PipelineData::Value(Value::Nothing { .. }) => None,
PipelineData::Value(v) => {
let prev = std::mem::take(v);
Some(prev)
}
PipelineData::Stream(stream) => stream.next(),
}
}
}
pub trait IntoPipelineData {
fn into_pipeline_data(self) -> PipelineData;
}
impl IntoPipelineData for Value {
fn into_pipeline_data(self) -> PipelineData {
PipelineData::Value(self)
}
}
impl<T> IntoPipelineData for T
where
T: Iterator<Item = Value> + Send + 'static,
{
fn into_pipeline_data(self) -> PipelineData {
PipelineData::Stream(ValueStream(Box::new(self)))
}
}

View file

@ -1,7 +1,9 @@
use crate::ast::Call;
use crate::engine::Command;
use crate::engine::CommandClone;
use crate::engine::EvaluationContext;
use crate::BlockId;
use crate::PipelineData;
use crate::SyntaxShape;
use crate::Value;
use crate::VarId;
@ -335,6 +337,7 @@ impl Signature {
}
}
#[derive(Clone)]
struct Predeclaration {
signature: Signature,
}
@ -356,12 +359,13 @@ impl Command for Predeclaration {
&self,
_context: &EvaluationContext,
_call: &Call,
_input: Value,
) -> Result<crate::Value, crate::ShellError> {
_input: PipelineData,
) -> Result<PipelineData, crate::ShellError> {
panic!("Internal error: can't run a predeclaration without a body")
}
}
#[derive(Clone)]
struct BlockCommand {
signature: Signature,
block_id: BlockId,
@ -384,8 +388,8 @@ impl Command for BlockCommand {
&self,
_context: &EvaluationContext,
_call: &Call,
_input: Value,
) -> Result<crate::Value, crate::ShellError> {
_input: PipelineData,
) -> Result<crate::PipelineData, crate::ShellError> {
panic!("Internal error: can't run custom command with 'run', use block_id");
}

View file

@ -59,10 +59,6 @@ pub enum Value {
vals: Vec<Value>,
span: Span,
},
Stream {
stream: ValueStream,
span: Span,
},
List {
vals: Vec<Value>,
span: Span,
@ -110,7 +106,6 @@ impl Value {
Value::Record { span, .. } => Ok(*span),
Value::List { span, .. } => Ok(*span),
Value::Block { span, .. } => Ok(*span),
Value::Stream { span, .. } => Ok(*span),
Value::Nothing { span, .. } => Ok(*span),
Value::Binary { span, .. } => Ok(*span),
Value::CellPath { span, .. } => Ok(*span),
@ -129,7 +124,6 @@ impl Value {
Value::Range { span, .. } => *span = new_span,
Value::String { span, .. } => *span = new_span,
Value::Record { span, .. } => *span = new_span,
Value::Stream { span, .. } => *span = new_span,
Value::List { span, .. } => *span = new_span,
Value::Block { span, .. } => *span = new_span,
Value::Nothing { span, .. } => *span = new_span,
@ -158,7 +152,6 @@ impl Value {
Value::List { .. } => Type::List(Box::new(Type::Unknown)), // FIXME
Value::Nothing { .. } => Type::Nothing,
Value::Block { .. } => Type::Block,
Value::Stream { .. } => Type::ValueStream,
Value::Error { .. } => Type::Error,
Value::Binary { .. } => Type::Binary,
Value::CellPath { .. } => Type::CellPath,
@ -186,7 +179,6 @@ impl Value {
Err(error) => format!("{:?}", error),
},
Value::String { val, .. } => val,
Value::Stream { stream, .. } => stream.into_string(),
Value::List { vals: val, .. } => format!(
"[{}]",
val.into_iter()
@ -228,7 +220,6 @@ impl Value {
}
},
Value::String { val, .. } => val,
Value::Stream { stream, .. } => stream.collect_string(),
Value::List { vals: val, .. } => val
.into_iter()
.map(|x| x.collect_string())
@ -274,13 +265,6 @@ impl Value {
return Err(ShellError::AccessBeyondEnd(val.len(), *origin_span));
}
}
Value::Stream { stream, .. } => {
if let Some(item) = stream.nth(*count) {
current = item;
} else {
return Err(ShellError::AccessBeyondEndOfStream(*origin_span));
}
}
x => {
return Err(ShellError::IncompatiblePathAccess(
format!("{}", x.get_type()),
@ -329,27 +313,6 @@ impl Value {
span: *span,
};
}
Value::Stream { stream, span } => {
let mut output = vec![];
for val in stream {
output.push(val.clone().follow_cell_path(&[PathMember::String {
val: column_name.clone(),
span: *origin_span,
}])?);
// if let Value::Record { cols, vals, .. } = val {
// for col in cols.iter().enumerate() {
// if col.1 == column_name {
// output.push(vals[col.0].clone());
// }
// }
// }
}
current = Value::List {
vals: output,
span: *span,
};
}
x => {
return Err(ShellError::IncompatiblePathAccess(
format!("{}", x.get_type()),
@ -374,64 +337,6 @@ impl Value {
}
}
pub fn map<F>(self, span: Span, mut f: F) -> Result<Value, ShellError>
where
Self: Sized,
F: FnMut(Self) -> Value + 'static,
{
match self {
Value::List { vals, .. } => Ok(Value::Stream {
stream: vals.into_iter().map(f).into_value_stream(),
span,
}),
Value::Stream { stream, .. } => Ok(Value::Stream {
stream: stream.map(f).into_value_stream(),
span,
}),
Value::Range { val, .. } => Ok(Value::Stream {
stream: val.into_range_iter()?.map(f).into_value_stream(),
span,
}),
v => {
let output = f(v);
match output {
Value::Error { error } => Err(error),
v => Ok(v),
}
}
}
}
pub fn flat_map<U, F>(self, span: Span, mut f: F) -> Value
where
Self: Sized,
U: IntoIterator<Item = Value>,
<U as IntoIterator>::IntoIter: 'static,
F: FnMut(Self) -> U + 'static,
{
match self {
Value::List { vals, .. } => Value::Stream {
stream: vals.into_iter().map(f).flatten().into_value_stream(),
span,
},
Value::Stream { stream, .. } => Value::Stream {
stream: stream.map(f).flatten().into_value_stream(),
span,
},
Value::Range { val, .. } => match val.into_range_iter() {
Ok(iter) => Value::Stream {
stream: iter.map(f).flatten().into_value_stream(),
span,
},
Err(error) => Value::Error { error },
},
v => Value::Stream {
stream: f(v).into_iter().into_value_stream(),
span,
},
}
}
pub fn string(val: impl Into<String>, span: Span) -> Value {
Value::String {
val: val.into(),
@ -460,6 +365,12 @@ impl Value {
}
}
impl Default for Value {
fn default() -> Self {
Value::nothing()
}
}
impl PartialOrd for Value {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
// Compare two floating point numbers. The decision interval for equality is dynamically
@ -511,24 +422,6 @@ impl PartialOrd for Value {
..
},
) if lhs_headers == rhs_headers && lhs == rhs => Some(Ordering::Equal),
(Value::Stream { stream: lhs, .. }, Value::Stream { stream: rhs, .. }) => {
lhs.clone().partial_cmp(rhs.clone())
}
(Value::Stream { stream: lhs, .. }, Value::String { val: rhs, .. }) => {
lhs.clone().collect_string().partial_cmp(rhs)
}
(Value::String { val: lhs, .. }, Value::Stream { stream: rhs, .. }) => {
lhs.partial_cmp(&rhs.clone().collect_string())
}
// NOTE: This may look a bit strange, but a `Stream` is still just a `List`, it just
// happens to be in an iterator form instead of a concrete form. The contained values
// can be compared.
(Value::Stream { stream: lhs, .. }, Value::List { vals: rhs, .. }) => {
lhs.clone().collect::<Vec<Value>>().partial_cmp(rhs)
}
(Value::List { vals: lhs, .. }, Value::Stream { stream: rhs, .. }) => {
lhs.partial_cmp(&rhs.clone().collect::<Vec<Value>>())
}
(Value::Binary { val: lhs, .. }, Value::Binary { val: rhs, .. }) => {
lhs.partial_cmp(rhs)
}
@ -852,10 +745,6 @@ impl Value {
val: rhs.contains(lhs),
span,
}),
(lhs, Value::Stream { stream: rhs, .. }) => Ok(Value::Bool {
val: rhs.clone().any(|x| lhs == &x),
span,
}),
_ => Err(ShellError::OperatorMismatch {
op_span: op,
lhs_ty: self.get_type(),
@ -886,10 +775,6 @@ impl Value {
val: !rhs.contains(lhs),
span,
}),
(lhs, Value::Stream { stream: rhs, .. }) => Ok(Value::Bool {
val: rhs.clone().all(|x| lhs != &x),
span,
}),
_ => Err(ShellError::OperatorMismatch {
op_span: op,
lhs_ty: self.get_type(),

View file

@ -1,10 +1,7 @@
use crate::*;
use std::{cell::RefCell, fmt::Debug, rc::Rc};
use std::fmt::Debug;
use serde::{ser::SerializeSeq, Deserialize, Serialize};
#[derive(Clone)]
pub struct ValueStream(pub Rc<RefCell<dyn Iterator<Item = Value>>>);
pub struct ValueStream(pub Box<dyn Iterator<Item = Value> + Send + 'static>);
impl ValueStream {
pub fn into_string(self) -> String {
@ -22,8 +19,8 @@ impl ValueStream {
.join("\n")
}
pub fn from_stream(input: impl Iterator<Item = Value> + 'static) -> ValueStream {
ValueStream(Rc::new(RefCell::new(input)))
pub fn from_stream(input: impl Iterator<Item = Value> + Send + 'static) -> ValueStream {
ValueStream(Box::new(input))
}
}
@ -38,66 +35,66 @@ impl Iterator for ValueStream {
fn next(&mut self) -> Option<Self::Item> {
{
self.0.borrow_mut().next()
self.0.next()
}
}
}
impl Serialize for ValueStream {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(None)?;
// impl Serialize for ValueStream {
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
// where
// S: serde::Serializer,
// {
// let mut seq = serializer.serialize_seq(None)?;
for element in self.0.borrow_mut().into_iter() {
seq.serialize_element(&element)?;
}
seq.end()
}
}
// for element in self.0.borrow_mut().into_iter() {
// seq.serialize_element(&element)?;
// }
// seq.end()
// }
// }
impl<'de> Deserialize<'de> for ValueStream {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_seq(MySeqVisitor)
}
}
// impl<'de> Deserialize<'de> for ValueStream {
// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
// where
// D: serde::Deserializer<'de>,
// {
// deserializer.deserialize_seq(MySeqVisitor)
// }
// }
struct MySeqVisitor;
// struct MySeqVisitor;
impl<'a> serde::de::Visitor<'a> for MySeqVisitor {
type Value = ValueStream;
// impl<'a> serde::de::Visitor<'a> for MySeqVisitor {
// type Value = ValueStream;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a value stream")
}
// fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
// formatter.write_str("a value stream")
// }
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'a>,
{
let mut output: Vec<Value> = vec![];
// fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
// where
// A: serde::de::SeqAccess<'a>,
// {
// let mut output: Vec<Value> = vec![];
while let Some(value) = seq.next_element()? {
output.push(value);
}
// while let Some(value) = seq.next_element()? {
// output.push(value);
// }
Ok(ValueStream(Rc::new(RefCell::new(output.into_iter()))))
}
}
// Ok(ValueStream(Rc::new(RefCell::new(output.into_iter()))))
// }
// }
pub trait IntoValueStream {
fn into_value_stream(self) -> ValueStream;
}
// pub trait IntoValueStream {
// fn into_value_stream(self) -> ValueStream;
// }
impl<T> IntoValueStream for T
where
T: Iterator<Item = Value> + 'static,
{
fn into_value_stream(self) -> ValueStream {
ValueStream::from_stream(self)
}
}
// impl<T> IntoValueStream for T
// where
// T: Iterator<Item = Value> + 'static,
// {
// fn into_value_stream(self) -> ValueStream {
// ValueStream::from_stream(self)
// }
// }