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

allow mut to be used with pipelines #9634

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions crates/nu-cmd-lang/src/core_commands/mut_.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use nu_engine::eval_expression_with_input;
use nu_engine::eval_block;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
Expand Down Expand Up @@ -54,25 +54,22 @@ impl Command for Mut {
.as_var()
.expect("internal error: missing variable");

let keyword_expr = call
let block_id = call
.positional_nth(1)
.expect("checked through parser")
.as_keyword()
.expect("internal error: missing keyword");
.as_block()
.expect("internal error: missing right hand side");

let rhs = eval_expression_with_input(
let pipeline_data = eval_block(
engine_state,
stack,
keyword_expr,
engine_state.get_block(block_id),
input,
call.redirect_stdout,
call.redirect_stderr,
)?
.0;
)?;

//println!("Adding: {:?} to {}", rhs, var_id);

stack.add_var(var_id, rhs.into_value(call.head));
stack.add_var(var_id, pipeline_data.into_value(call.head));
Ok(PipelineData::empty())
}

Expand Down
16 changes: 16 additions & 0 deletions crates/nu-command/tests/commands/mut_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,19 @@ fn mut_records_update_properly() {
let actual = nu!(pipeline("mut a = {}; $a.b.c = 100; $a.b.c"));
assert_eq!(actual.out, "100");
}

#[test]
fn mut_takes_pipeline() {
let actual = nu!(r#"mut x = "hello world" | str length; print $x"#);

assert_eq!(actual.out, "11");
}

#[test]
fn mut_pipeline_allows_in() {
let actual = nu!(r#"
def foo [] { mut x = $in | str length; print ($x + 10) }; "hello world" | foo
"#);

assert_eq!(actual.out, "21");
}
4 changes: 2 additions & 2 deletions crates/nu-parser/src/parse_keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2898,13 +2898,13 @@ pub fn parse_let(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipeline
}]);
} else {
working_set.error(ParseError::UnknownState(
"internal error: let or const statements not found in core language".into(),
"internal error: let statement not found in core language".into(),
span(spans),
))
}

working_set.error(ParseError::UnknownState(
"internal error: let or const statement unparsable".into(),
"internal error: let statement unparsable".into(),
span(spans),
));

Expand Down
Loading