Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split OutputStream into ActionStream/OutputStream #3304

Merged
merged 5 commits into from Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/nu-cli/src/lib.rs
Expand Up @@ -30,7 +30,7 @@ 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_stream::{ActionStream, InputStream, InterruptibleStream};
pub use nu_value_ext::ValueExt;
pub use num_traits::cast::ToPrimitive;

Expand Down
25 changes: 4 additions & 21 deletions crates/nu-cli/src/prelude.rs
Expand Up @@ -36,41 +36,24 @@ pub(crate) use nu_engine::Host;
pub(crate) use nu_errors::ShellError;
#[allow(unused_imports)]
pub(crate) use nu_protocol::outln;
pub(crate) use nu_stream::OutputStream;
pub(crate) use nu_stream::ActionStream;
#[allow(unused_imports)]
pub(crate) use nu_value_ext::ValueExt;
#[allow(unused_imports)]
pub(crate) use std::sync::atomic::Ordering;

#[allow(clippy::clippy::wrong_self_convention)]
pub trait FromInputStream {
fn from_input_stream(self) -> OutputStream;
fn from_input_stream(self) -> ActionStream;
}

impl<T> FromInputStream for T
where
T: Iterator<Item = nu_protocol::Value> + Send + Sync + 'static,
{
fn from_input_stream(self) -> OutputStream {
OutputStream {
fn from_input_stream(self) -> ActionStream {
ActionStream {
values: Box::new(self.map(nu_protocol::ReturnSuccess::value)),
}
}
}

#[allow(clippy::clippy::wrong_self_convention)]
pub trait ToOutputStream {
fn to_output_stream(self) -> OutputStream;
}

impl<T, U> ToOutputStream for T
where
T: Iterator<Item = U> + Send + Sync + 'static,
U: Into<nu_protocol::ReturnValue>,
{
fn to_output_stream(self) -> OutputStream {
OutputStream {
values: Box::new(self.map(|item| item.into())),
}
}
}
7 changes: 4 additions & 3 deletions crates/nu-command/src/commands/all.rs
Expand Up @@ -5,6 +5,7 @@ use nu_errors::ShellError;
use nu_protocol::{
hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue,
};
use nu_stream::ToActionStream;

pub struct Command;

Expand All @@ -30,7 +31,7 @@ impl WholeStreamCommand for Command {
"Find if the table rows matches the condition."
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
all(args)
}

Expand All @@ -52,7 +53,7 @@ impl WholeStreamCommand for Command {
}
}

fn all(args: CommandArgs) -> Result<OutputStream, ShellError> {
fn all(args: CommandArgs) -> Result<ActionStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args));
let tag = args.call_info.name_tag.clone();
let (Arguments { block }, input) = args.process()?;
Expand Down Expand Up @@ -117,7 +118,7 @@ fn all(args: CommandArgs) -> Result<OutputStream, ShellError> {
Err(e) => Err(e),
}
})?
.to_output_stream())
.to_action_stream())
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions crates/nu-command/src/commands/ansi/command.rs
Expand Up @@ -112,7 +112,7 @@ Format: #
]
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let args = args.evaluate_once()?;

let code: Option<Result<Tagged<String>, ShellError>> = args.opt(0);
Expand All @@ -130,7 +130,7 @@ Format: #
));
}
let output = format!("\x1b[{}", e.item);
return Ok(OutputStream::one(ReturnSuccess::value(
return Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(output).into_value(e.tag()),
)));
}
Expand All @@ -149,7 +149,7 @@ Format: #
//Operating system command aka osc ESC ] <- note the right brace, not left brace for osc
// OCS's need to end with a bell '\x07' char
let output = format!("\x1b]{};", o.item);
return Ok(OutputStream::one(ReturnSuccess::value(
return Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(output).into_value(o.tag()),
)));
}
Expand All @@ -159,7 +159,7 @@ Format: #
let ansi_code = str_to_ansi(&code.item);

if let Some(output) = ansi_code {
Ok(OutputStream::one(ReturnSuccess::value(
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(output).into_value(code.tag()),
)))
} else {
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/src/commands/ansi/strip.rs
Expand Up @@ -31,7 +31,7 @@ impl WholeStreamCommand for SubCommand {
"strip ansi escape sequences from string"
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
operate(args)
}

Expand All @@ -44,7 +44,7 @@ impl WholeStreamCommand for SubCommand {
}
}

fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
let (Arguments { rest }, input) = args.process()?;
let column_paths: Vec<_> = rest;

Expand All @@ -65,7 +65,7 @@ fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
ReturnSuccess::value(ret)
}
})
.to_output_stream())
.to_action_stream())
}

fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/src/commands/any.rs
Expand Up @@ -30,7 +30,7 @@ impl WholeStreamCommand for Command {
"Find if the table rows matches the condition."
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
any(args)
}

Expand All @@ -52,7 +52,7 @@ impl WholeStreamCommand for Command {
}
}

fn any(args: CommandArgs) -> Result<OutputStream, ShellError> {
fn any(args: CommandArgs) -> Result<ActionStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args));
let tag = args.call_info.name_tag.clone();
let (Arguments { block }, input) = args.process()?;
Expand Down Expand Up @@ -117,7 +117,7 @@ fn any(args: CommandArgs) -> Result<OutputStream, ShellError> {
Err(e) => Err(e),
}
})?
.to_output_stream())
.to_action_stream())
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/commands/append.rs
Expand Up @@ -27,7 +27,7 @@ impl WholeStreamCommand for Command {
"Append a row to the table."
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let (Arguments { mut value }, input) = args.process()?;

let input: Vec<Value> = input.collect();
Expand All @@ -51,7 +51,7 @@ impl WholeStreamCommand for Command {
.into_iter()
.chain(vec![value])
.map(ReturnSuccess::value)
.to_output_stream())
.to_action_stream())
}

fn examples(&self) -> Vec<Example> {
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/commands/autoenv.rs
Expand Up @@ -25,8 +25,8 @@ The .nu-env file has the same format as your $HOME/nu/config.toml file. By loadi
fn signature(&self) -> Signature {
Signature::build("autoenv")
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(ReturnSuccess::value(
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(get_full_help(&Autoenv, &args.scope)).into_value(Tag::unknown()),
)))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/commands/autoenv_trust.rs
Expand Up @@ -21,7 +21,7 @@ impl WholeStreamCommand for AutoenvTrust {
"Trust a .nu-env file in the current or given directory"
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let ctx = EvaluationContext::from_args(&args);

Expand Down Expand Up @@ -55,7 +55,7 @@ impl WholeStreamCommand for AutoenvTrust {
})?;
fs::write(config_path, tomlstr).expect("Couldn't write to toml file");

Ok(OutputStream::one(ReturnSuccess::value(
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(".nu-env trusted!").into_value(tag),
)))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/commands/autoenv_untrust.rs
Expand Up @@ -25,7 +25,7 @@ impl WholeStreamCommand for AutoenvUnTrust {
"Untrust a .nu-env file in the current or given directory"
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let ctx = EvaluationContext::from_args(&args);
let file_to_untrust = match args.call_info.evaluate(&ctx)?.args.nth(0) {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl WholeStreamCommand for AutoenvUnTrust {
})?;
fs::write(config_path, tomlstr).expect("Couldn't write to toml file");

Ok(OutputStream::one(ReturnSuccess::value(
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(".nu-env untrusted!").into_value(tag),
)))
}
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/src/commands/autoview/command.rs
Expand Up @@ -80,7 +80,7 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
);
let command_args =
create_default_command_args(&context).with_input(stream);
let result = text.run(command_args)?;
let result = text.run_with_actions(command_args)?;
let _ = result.collect::<Vec<_>>();
} else {
out!("{}", s);
Expand Down Expand Up @@ -162,7 +162,7 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
stream.push_back(x);
let command_args =
create_default_command_args(&context).with_input(stream);
let result = binary.run(command_args)?;
let result = binary.run_with_actions(command_args)?;
let _ = result.collect::<Vec<_>>();
} else {
use pretty_hex::*;
Expand Down Expand Up @@ -255,7 +255,7 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
}
}

Ok(OutputStream::empty())
Ok(InputStream::empty())
}

fn create_default_command_args(context: &RunnableContextWithoutInput) -> RawCommandArgs {
Expand Down
10 changes: 5 additions & 5 deletions crates/nu-command/src/commands/benchmark.rs
Expand Up @@ -47,7 +47,7 @@ impl WholeStreamCommand for Benchmark {
"Runs a block and returns the time it took to execute it."
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
benchmark(args)
}

Expand All @@ -67,7 +67,7 @@ impl WholeStreamCommand for Benchmark {
}
}

fn benchmark(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
fn benchmark(raw_args: CommandArgs) -> Result<ActionStream, ShellError> {
let tag = raw_args.call_info.args.span;
let mut context = EvaluationContext::from_args(&raw_args);
let scope = raw_args.scope.clone();
Expand Down Expand Up @@ -134,10 +134,10 @@ fn benchmark_output<T, Output>(
passthrough: Option<CapturedBlock>,
tag: T,
context: &mut EvaluationContext,
) -> Result<OutputStream, ShellError>
) -> Result<ActionStream, ShellError>
where
T: Into<Tag> + Copy,
Output: Into<OutputStream>,
Output: Into<ActionStream>,
{
let value = UntaggedValue::Row(Dictionary::from(
indexmap
Expand All @@ -161,7 +161,7 @@ where

Ok(block_output.into())
} else {
let benchmark_output = OutputStream::one(value);
let benchmark_output = ActionStream::one(value);
Ok(benchmark_output)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/commands/build_string.rs
Expand Up @@ -21,7 +21,7 @@ impl WholeStreamCommand for BuildString {
"Builds a string from the arguments."
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let args = args.evaluate_once()?;
let rest: Vec<Value> = args.rest(0)?;
Expand All @@ -32,7 +32,7 @@ impl WholeStreamCommand for BuildString {
output_string.push_str(&format_leaf(&r).plain_string(100_000))
}

Ok(OutputStream::one(ReturnSuccess::value(
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(output_string).into_value(tag),
)))
}
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-command/src/commands/cal.rs
Expand Up @@ -40,7 +40,7 @@ impl WholeStreamCommand for Cal {
"Display a calendar."
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
cal(args)
}

Expand All @@ -65,7 +65,7 @@ impl WholeStreamCommand for Cal {
}
}

pub fn cal(args: CommandArgs) -> Result<OutputStream, ShellError> {
pub fn cal(args: CommandArgs) -> Result<ActionStream, ShellError> {
let args = args.evaluate_once()?;
let mut calendar_vec_deque = VecDeque::new();
let tag = args.call_info.name_tag.clone();
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn cal(args: CommandArgs) -> Result<OutputStream, ShellError> {
current_day_option,
)?;

Ok(calendar_vec_deque.into_iter().to_output_stream())
Ok(calendar_vec_deque.into_iter().to_action_stream())
}

fn get_invalid_year_shell_error(year_tag: &Tag) -> ShellError {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/commands/cd.rs
Expand Up @@ -24,7 +24,7 @@ impl WholeStreamCommand for Cd {
"Change to a new path."
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let name = args.call_info.name_tag.clone();
let shell_manager = args.shell_manager.clone();
let (args, _): (CdArgs, _) = args.process()?;
Expand Down
8 changes: 4 additions & 4 deletions crates/nu-command/src/commands/char_.rs
Expand Up @@ -57,7 +57,7 @@ impl WholeStreamCommand for Char {
]
}

fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
let args = args.evaluate_once()?;

let name: Tagged<String> = args.req(0)?;
Expand All @@ -83,13 +83,13 @@ impl WholeStreamCommand for Char {
Err(e) => return Err(e),
}
}
Ok(OutputStream::one(ReturnSuccess::value(
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(multi_byte).into_value(name.tag),
)))
} else {
let decoded_char = string_to_unicode_char(&name.item, &name.tag);
if let Ok(ch) = decoded_char {
Ok(OutputStream::one(ReturnSuccess::value(
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(ch).into_value(name.tag()),
)))
} else {
Expand All @@ -103,7 +103,7 @@ impl WholeStreamCommand for Char {
} else {
let special_character = str_to_character(&name.item);
if let Some(output) = special_character {
Ok(OutputStream::one(ReturnSuccess::value(
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::string(output).into_value(name.tag()),
)))
} else {
Expand Down