mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 02:55:07 +02:00
Nucli refactor crate stream (#2828)
* nu-stream is building on its own, now clean up Cargo.toml * replace the stream crate in nu-cli * cc * since we moved stream out of the nu-cli crate and into its own crate we need to remove pub(crate) and just make it pub * clean up the prelude and hand merge everything together * clean up Cargo.tom * cargo fmt along with Cargo.lock
This commit is contained in:
@ -2,7 +2,6 @@ use crate::commands::classified::expr::run_expression_block;
|
||||
use crate::commands::classified::internal::run_internal_command;
|
||||
use crate::evaluation_context::EvaluationContext;
|
||||
use crate::prelude::*;
|
||||
use crate::stream::InputStream;
|
||||
use async_recursion::async_recursion;
|
||||
use futures::stream::TryStreamExt;
|
||||
use nu_errors::ShellError;
|
||||
@ -10,6 +9,7 @@ use nu_protocol::hir::{
|
||||
Block, Call, ClassifiedCommand, Expression, Pipeline, SpannedExpression, Synthetic,
|
||||
};
|
||||
use nu_protocol::{ReturnSuccess, UntaggedValue, Value};
|
||||
use nu_stream::InputStream;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
#[async_recursion]
|
||||
|
@ -2,9 +2,9 @@ use crate::commands::{command::CommandArgs, Command, UnevaluatedCallInfo};
|
||||
use crate::env::host::Host;
|
||||
use crate::prelude::*;
|
||||
use crate::shell::shell_manager::ShellManager;
|
||||
use crate::stream::{InputStream, OutputStream};
|
||||
use nu_protocol::hir;
|
||||
use nu_source::{Tag, Text};
|
||||
use nu_stream::{InputStream, OutputStream};
|
||||
use parking_lot::Mutex;
|
||||
use std::error::Error;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
|
@ -17,7 +17,7 @@ use crate::commands::{
|
||||
StrCollect, WholeStreamCommand, Wrap,
|
||||
};
|
||||
use crate::evaluation_context::EvaluationContext;
|
||||
use crate::stream::{InputStream, OutputStream};
|
||||
use nu_stream::{InputStream, OutputStream};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use futures::executor::block_on;
|
||||
|
@ -31,7 +31,6 @@ mod keybinding;
|
||||
mod path;
|
||||
mod plugin;
|
||||
mod shell;
|
||||
mod stream;
|
||||
pub mod types;
|
||||
pub mod utils;
|
||||
|
||||
@ -54,11 +53,11 @@ pub use crate::env::environment_syncer::EnvironmentSyncer;
|
||||
pub use crate::env::host::BasicHost;
|
||||
pub use crate::evaluation_context::EvaluationContext;
|
||||
pub use crate::prelude::ToOutputStream;
|
||||
pub use crate::stream::{InputStream, InterruptibleStream, OutputStream};
|
||||
pub use nu_data::config;
|
||||
pub use nu_data::dict::TaggedListBuilder;
|
||||
pub use nu_data::primitive;
|
||||
pub use nu_data::value;
|
||||
pub use nu_stream::{InputStream, InterruptibleStream, OutputStream};
|
||||
pub use nu_value_ext::ValueExt;
|
||||
pub use num_traits::cast::ToPrimitive;
|
||||
|
||||
|
@ -36,7 +36,7 @@ macro_rules! trace_stream {
|
||||
);
|
||||
});
|
||||
|
||||
$crate::stream::InputStream::from_stream(objects.boxed())
|
||||
nu_stream::InputStream::from_stream(objects.boxed())
|
||||
} else {
|
||||
$expr
|
||||
}
|
||||
@ -61,7 +61,7 @@ macro_rules! trace_out_stream {
|
||||
);
|
||||
});
|
||||
|
||||
$crate::stream::OutputStream::new(objects)
|
||||
nu_stream::OutputStream::new(objects)
|
||||
} else {
|
||||
$expr
|
||||
}
|
||||
@ -84,7 +84,6 @@ pub(crate) use crate::shell::filesystem_shell::FilesystemShell;
|
||||
pub(crate) use crate::shell::help_shell::HelpShell;
|
||||
pub(crate) use crate::shell::shell_manager::ShellManager;
|
||||
pub(crate) use crate::shell::value_shell::ValueShell;
|
||||
pub(crate) use crate::stream::{InputStream, InterruptibleStream, OutputStream};
|
||||
pub(crate) use bigdecimal::BigDecimal;
|
||||
pub(crate) use futures::stream::BoxStream;
|
||||
pub(crate) use futures::{Stream, StreamExt};
|
||||
@ -94,6 +93,7 @@ pub(crate) use nu_source::{
|
||||
b, AnchorLocation, DebugDocBuilder, PrettyDebug, PrettyDebugWithSource, Span, SpannedItem, Tag,
|
||||
TaggedItem, Text,
|
||||
};
|
||||
pub(crate) use nu_stream::{InputStream, InterruptibleStream, OutputStream};
|
||||
pub(crate) use nu_value_ext::ValueExt;
|
||||
pub(crate) use num_bigint::BigInt;
|
||||
pub(crate) use num_traits::cast::ToPrimitive;
|
||||
|
@ -7,7 +7,7 @@ use crate::commands::mkdir::MkdirArgs;
|
||||
use crate::commands::move_::mv::Arguments as MvArgs;
|
||||
use crate::commands::rm::RemoveArgs;
|
||||
use crate::prelude::*;
|
||||
use crate::stream::OutputStream;
|
||||
use nu_stream::OutputStream;
|
||||
|
||||
use encoding_rs::Encoding;
|
||||
use nu_errors::ShellError;
|
||||
|
@ -9,7 +9,7 @@ use crate::commands::rm::RemoveArgs;
|
||||
use crate::prelude::*;
|
||||
use crate::shell::filesystem_shell::FilesystemShell;
|
||||
use crate::shell::shell::Shell;
|
||||
use crate::stream::OutputStream;
|
||||
use nu_stream::OutputStream;
|
||||
|
||||
use encoding_rs::Encoding;
|
||||
use nu_errors::ShellError;
|
||||
|
@ -1,183 +0,0 @@
|
||||
use crate::prelude::*;
|
||||
use futures::stream::{iter, once};
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Primitive, Type, UntaggedValue, Value};
|
||||
use nu_source::{Tagged, TaggedItem};
|
||||
|
||||
pub struct InputStream {
|
||||
values: BoxStream<'static, Value>,
|
||||
|
||||
// Whether or not an empty stream was explicitly requested via InputStream::empty
|
||||
empty: bool,
|
||||
}
|
||||
|
||||
impl InputStream {
|
||||
pub fn empty() -> InputStream {
|
||||
InputStream {
|
||||
values: once(async { UntaggedValue::nothing().into_untagged_value() }).boxed(),
|
||||
empty: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn one(item: impl Into<Value>) -> InputStream {
|
||||
let mut v: VecDeque<Value> = VecDeque::new();
|
||||
v.push_back(item.into());
|
||||
v.into()
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
values.collect()
|
||||
}
|
||||
|
||||
pub fn from_stream(input: impl Stream<Item = Value> + Send + 'static) -> InputStream {
|
||||
InputStream {
|
||||
values: input.boxed(),
|
||||
empty: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn collect_string(mut self, tag: Tag) -> Result<Tagged<String>, ShellError> {
|
||||
let mut bytes = vec![];
|
||||
let mut value_tag = tag.clone();
|
||||
|
||||
loop {
|
||||
match self.values.next().await {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(s)),
|
||||
tag: value_t,
|
||||
}) => {
|
||||
value_tag = value_t;
|
||||
bytes.extend_from_slice(&s.into_bytes());
|
||||
}
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Line(s)),
|
||||
tag: value_t,
|
||||
}) => {
|
||||
value_tag = value_t;
|
||||
bytes.extend_from_slice(&s.into_bytes());
|
||||
}
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Binary(b)),
|
||||
tag: value_t,
|
||||
}) => {
|
||||
value_tag = value_t;
|
||||
bytes.extend_from_slice(&b);
|
||||
}
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Nothing),
|
||||
tag: value_t,
|
||||
}) => {
|
||||
value_tag = value_t;
|
||||
}
|
||||
Some(Value {
|
||||
tag: value_tag,
|
||||
value,
|
||||
}) => {
|
||||
return Err(ShellError::labeled_error_with_secondary(
|
||||
"Expected a string from pipeline",
|
||||
"requires string input",
|
||||
tag,
|
||||
format!(
|
||||
"{} originates from here",
|
||||
Type::from_value(&value).plain_string(100000)
|
||||
),
|
||||
value_tag,
|
||||
))
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
match String::from_utf8(bytes) {
|
||||
Ok(s) => Ok(s.tagged(value_tag.clone())),
|
||||
Err(_) => Err(ShellError::labeled_error_with_secondary(
|
||||
"Expected a string from pipeline",
|
||||
"requires string input",
|
||||
tag,
|
||||
"value originates from here",
|
||||
value_tag,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn collect_binary(mut self, tag: Tag) -> Result<Tagged<Vec<u8>>, ShellError> {
|
||||
let mut bytes = vec![];
|
||||
let mut value_tag = tag.clone();
|
||||
|
||||
loop {
|
||||
match self.values.next().await {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Binary(b)),
|
||||
tag: value_t,
|
||||
}) => {
|
||||
value_tag = value_t;
|
||||
bytes.extend_from_slice(&b);
|
||||
}
|
||||
Some(Value {
|
||||
tag: value_tag,
|
||||
value: _,
|
||||
}) => {
|
||||
return Err(ShellError::labeled_error_with_secondary(
|
||||
"Expected binary from pipeline",
|
||||
"requires binary input",
|
||||
tag,
|
||||
"value originates from here",
|
||||
value_tag,
|
||||
));
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(bytes.tagged(value_tag))
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for InputStream {
|
||||
type Item = Value;
|
||||
|
||||
fn poll_next(
|
||||
mut self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> core::task::Poll<Option<Self::Item>> {
|
||||
Stream::poll_next(std::pin::Pin::new(&mut self.values), cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BoxStream<'static, Value>> for InputStream {
|
||||
fn from(input: BoxStream<'static, Value>) -> InputStream {
|
||||
InputStream {
|
||||
values: input,
|
||||
empty: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VecDeque<Value>> for InputStream {
|
||||
fn from(input: VecDeque<Value>) -> InputStream {
|
||||
InputStream {
|
||||
values: futures::stream::iter(input).boxed(),
|
||||
empty: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Value>> for InputStream {
|
||||
fn from(input: Vec<Value>) -> InputStream {
|
||||
InputStream {
|
||||
values: futures::stream::iter(input).boxed(),
|
||||
empty: false,
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
use crate::prelude::*;
|
||||
use futures::task::Poll;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
pub struct InterruptibleStream<V> {
|
||||
inner: BoxStream<'static, V>,
|
||||
interrupt_signal: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl<V> InterruptibleStream<V> {
|
||||
pub fn new<S>(inner: S, interrupt_signal: Arc<AtomicBool>) -> InterruptibleStream<V>
|
||||
where
|
||||
S: Stream<Item = V> + Send + 'static,
|
||||
{
|
||||
InterruptibleStream {
|
||||
inner: inner.boxed(),
|
||||
interrupt_signal,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> Stream for InterruptibleStream<V> {
|
||||
type Item = V;
|
||||
|
||||
fn poll_next(
|
||||
mut self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> core::task::Poll<Option<Self::Item>> {
|
||||
if self.interrupt_signal.load(Ordering::SeqCst) {
|
||||
Poll::Ready(None)
|
||||
} else {
|
||||
Stream::poll_next(std::pin::Pin::new(&mut self.inner), cx)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
mod input;
|
||||
mod interruptible;
|
||||
mod output;
|
||||
|
||||
pub use input::*;
|
||||
pub use interruptible::*;
|
||||
pub use output::*;
|
@ -1,106 +0,0 @@
|
||||
use crate::prelude::*;
|
||||
use futures::stream::iter;
|
||||
use nu_protocol::{ReturnSuccess, ReturnValue, Value};
|
||||
use std::iter::IntoIterator;
|
||||
|
||||
pub struct OutputStream {
|
||||
pub(crate) values: BoxStream<'static, ReturnValue>,
|
||||
}
|
||||
|
||||
impl OutputStream {
|
||||
pub fn new(values: impl Stream<Item = ReturnValue> + Send + 'static) -> OutputStream {
|
||||
OutputStream {
|
||||
values: values.boxed(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn empty() -> OutputStream {
|
||||
let v: VecDeque<ReturnValue> = VecDeque::new();
|
||||
v.into()
|
||||
}
|
||||
|
||||
pub fn one(item: impl Into<ReturnValue>) -> OutputStream {
|
||||
let item = item.into();
|
||||
futures::stream::once(async move { item }).to_output_stream()
|
||||
}
|
||||
|
||||
pub fn from_input(input: impl Stream<Item = Value> + Send + 'static) -> OutputStream {
|
||||
OutputStream {
|
||||
values: input.map(ReturnSuccess::value).boxed(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn drain_vec(&mut self) -> impl Future<Output = Vec<ReturnValue>> {
|
||||
let mut values: BoxStream<'static, ReturnValue> = iter(VecDeque::new()).boxed();
|
||||
std::mem::swap(&mut values, &mut self.values);
|
||||
|
||||
values.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for OutputStream {
|
||||
type Item = ReturnValue;
|
||||
|
||||
fn poll_next(
|
||||
mut self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> core::task::Poll<Option<Self::Item>> {
|
||||
Stream::poll_next(std::pin::Pin::new(&mut self.values), cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<InputStream> for OutputStream {
|
||||
fn from(input: InputStream) -> OutputStream {
|
||||
OutputStream {
|
||||
values: input.map(ReturnSuccess::value).boxed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BoxStream<'static, Value>> for OutputStream {
|
||||
fn from(input: BoxStream<'static, Value>) -> OutputStream {
|
||||
OutputStream {
|
||||
values: input.map(ReturnSuccess::value).boxed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BoxStream<'static, ReturnValue>> for OutputStream {
|
||||
fn from(input: BoxStream<'static, ReturnValue>) -> OutputStream {
|
||||
OutputStream { values: input }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VecDeque<ReturnValue>> for OutputStream {
|
||||
fn from(input: VecDeque<ReturnValue>) -> OutputStream {
|
||||
OutputStream {
|
||||
values: futures::stream::iter(input).boxed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VecDeque<Value>> for OutputStream {
|
||||
fn from(input: VecDeque<Value>) -> OutputStream {
|
||||
let stream = input.into_iter().map(ReturnSuccess::value);
|
||||
OutputStream {
|
||||
values: futures::stream::iter(stream).boxed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<ReturnValue>> for OutputStream {
|
||||
fn from(input: Vec<ReturnValue>) -> OutputStream {
|
||||
OutputStream {
|
||||
values: futures::stream::iter(input).boxed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Value>> for OutputStream {
|
||||
fn from(input: Vec<Value>) -> OutputStream {
|
||||
let stream = input.into_iter().map(ReturnSuccess::value);
|
||||
OutputStream {
|
||||
values: futures::stream::iter(stream).boxed(),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user