Move external closer to internal (#1611)

* Refactor InputStream and affected commands.

First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.

Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.

* Add run_external internal command.

We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:

- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution

It should also make it easier for us to start introducing argument signatures
for external commands,

* Update run_external.rs

* Update run_external.rs

* Update run_external.rs

* Update run_external.rs

Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
Jason Gedge
2020-04-19 23:30:44 -04:00
committed by GitHub
parent 6b8c6dec0e
commit 522a828687
67 changed files with 441 additions and 262 deletions

View File

@ -1,22 +1,32 @@
use crate::prelude::*;
use futures::stream::iter;
use futures::stream::{iter, once};
use nu_errors::ShellError;
use nu_protocol::{Primitive, UntaggedValue, Value};
use nu_source::{Tagged, TaggedItem};
pub struct InputStream {
pub(crate) values: BoxStream<'static, Value>,
values: BoxStream<'static, Value>,
// Whether or not an empty stream was explicitly requeted via InputStream::empty
empty: bool,
}
impl InputStream {
pub fn empty() -> InputStream {
vec![UntaggedValue::nothing().into_value(Tag::unknown())].into()
InputStream {
values: once(async { UntaggedValue::nothing().into_untagged_value() }).boxed(),
empty: true,
}
}
pub fn into_vec(self) -> impl Future<Output = Vec<Value>> {
self.values.collect()
}
pub fn is_empty(&self) -> bool {
self.empty
}
pub fn drain_vec(&mut self) -> impl Future<Output = Vec<Value>> {
let mut values: BoxStream<'static, Value> = iter(VecDeque::new()).boxed();
std::mem::swap(&mut values, &mut self.values);
@ -27,6 +37,7 @@ impl InputStream {
pub fn from_stream(input: impl Stream<Item = Value> + Send + 'static) -> InputStream {
InputStream {
values: input.boxed(),
empty: false,
}
}
@ -129,7 +140,10 @@ impl Stream for InputStream {
impl From<BoxStream<'static, Value>> for InputStream {
fn from(input: BoxStream<'static, Value>) -> InputStream {
InputStream { values: input }
InputStream {
values: input,
empty: false,
}
}
}
@ -137,6 +151,7 @@ impl From<VecDeque<Value>> for InputStream {
fn from(input: VecDeque<Value>) -> InputStream {
InputStream {
values: futures::stream::iter(input).boxed(),
empty: false,
}
}
}
@ -145,6 +160,7 @@ impl From<Vec<Value>> for InputStream {
fn from(input: Vec<Value>) -> InputStream {
InputStream {
values: futures::stream::iter(input).boxed(),
empty: false,
}
}
}

View File

@ -52,7 +52,7 @@ impl Stream for OutputStream {
impl From<InputStream> for OutputStream {
fn from(input: InputStream) -> OutputStream {
OutputStream {
values: input.values.map(ReturnSuccess::value).boxed(),
values: input.map(ReturnSuccess::value).boxed(),
}
}
}