Allow parse-time evaluation of calls, pipelines and subexpressions (#9499)

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
This commit is contained in:
Jakub Žádník
2023-08-26 16:41:29 +03:00
committed by GitHub
parent 3d73287ea4
commit 5ac5b90aed
37 changed files with 849 additions and 161 deletions

View File

@ -3,7 +3,7 @@ use std::path::Path;
use super::PathSubcommandArguments;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
@ -45,6 +45,10 @@ impl Command for SubCommand {
"Get the final component of a path."
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -67,6 +71,27 @@ impl Command for SubCommand {
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
replace: call.get_flag_const(working_set, "replace")?,
};
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&get_basename, &args, value, head),
working_set.permanent().ctrlc.clone(),
)
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example> {
vec![

View File

@ -2,7 +2,7 @@ use std::path::Path;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
@ -53,6 +53,10 @@ impl Command for SubCommand {
"Get the parent directory of a path."
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -76,6 +80,28 @@ impl Command for SubCommand {
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
replace: call.get_flag_const(working_set, "replace")?,
num_levels: call.get_flag_const(working_set, "num-levels")?,
};
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&get_dirname, &args, value, head),
working_set.permanent().ctrlc.clone(),
)
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example> {
vec![

View File

@ -1,9 +1,9 @@
use std::path::{Path, PathBuf};
use nu_engine::current_dir;
use nu_engine::{current_dir, current_dir_const};
use nu_path::expand_path_with;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
@ -45,6 +45,10 @@ impl Command for SubCommand {
If you need to distinguish dirs and files, please use `path type`."#
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -66,6 +70,26 @@ If you need to distinguish dirs and files, please use `path type`."#
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
pwd: current_dir_const(working_set)?,
};
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&exists, &args, value, head),
working_set.permanent().ctrlc.clone(),
)
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example> {
vec![

View File

@ -1,9 +1,9 @@
use std::path::Path;
use nu_engine::env::current_dir_str;
use nu_engine::env::{current_dir_str, current_dir_str_const};
use nu_path::{canonicalize_with, expand_path_with};
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
@ -48,6 +48,10 @@ impl Command for SubCommand {
"Try to expand a path to its absolute form."
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -71,6 +75,28 @@ impl Command for SubCommand {
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
strict: call.has_flag("strict"),
cwd: current_dir_str_const(working_set)?,
not_follow_symlink: call.has_flag("no-symlink"),
};
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&expand, &args, value, head),
working_set.permanent().ctrlc.clone(),
)
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example> {
vec![

View File

@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
@ -46,6 +46,10 @@ impl Command for SubCommand {
the output of 'path parse' and 'path split' subcommands."#
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -53,29 +57,24 @@ the output of 'path parse' and 'path split' subcommands."#
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
append: call.rest(engine_state, stack, 0)?,
};
let metadata = input.metadata();
run(call, &args, input)
}
match input {
PipelineData::Value(val, md) => {
Ok(PipelineData::Value(handle_value(val, &args, head), md))
}
PipelineData::ListStream(..) => Ok(PipelineData::Value(
handle_value(input.into_value(head), &args, head),
metadata,
)),
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: head }),
_ => Err(ShellError::UnsupportedInput(
"Input value cannot be joined".to_string(),
"value originates from here".into(),
head,
input.span().unwrap_or(call.head),
)),
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let args = Arguments {
append: call.rest_const(working_set, 0)?,
};
run(call, &args, input)
}
#[cfg(windows)]
@ -147,6 +146,27 @@ the output of 'path parse' and 'path split' subcommands."#
}
}
fn run(call: &Call, args: &Arguments, input: PipelineData) -> Result<PipelineData, ShellError> {
let head = call.head;
let metadata = input.metadata();
match input {
PipelineData::Value(val, md) => Ok(PipelineData::Value(handle_value(val, args, head), md)),
PipelineData::ListStream(..) => Ok(PipelineData::Value(
handle_value(input.into_value(head), args, head),
metadata,
)),
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: head }),
_ => Err(ShellError::UnsupportedInput(
"Input value cannot be joined".to_string(),
"value originates from here".into(),
head,
input.span().unwrap_or(call.head),
)),
}
}
fn handle_value(v: Value, args: &Arguments, head: Span) -> Value {
match v {
Value::String { ref val, .. } => join_single(Path::new(val), head, args),

View File

@ -2,7 +2,7 @@ use std::path::Path;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
@ -48,6 +48,10 @@ impl Command for SubCommand {
On Windows, an extra 'prefix' column is added."#
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -70,6 +74,27 @@ On Windows, an extra 'prefix' column is added."#
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
extension: call.get_flag_const(working_set, "extension")?,
};
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&parse, &args, value, head),
working_set.permanent().ctrlc.clone(),
)
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example> {
vec![

View File

@ -3,7 +3,7 @@ use std::path::Path;
use nu_engine::CallExt;
use nu_path::expand_to_real_path;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
@ -52,6 +52,10 @@ absolute or both relative. The argument path needs to be a parent of the input
path."#
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -74,6 +78,27 @@ path."#
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments {
path: call.req_const(working_set, 0)?,
};
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&relative_to, &args, value, head),
working_set.permanent().ctrlc.clone(),
)
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example> {
vec![

View File

@ -1,7 +1,7 @@
use std::path::{Component, Path};
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
@ -36,6 +36,10 @@ impl Command for SubCommand {
"Split a path into a list based on the system's path separator."
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -56,6 +60,25 @@ impl Command for SubCommand {
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments;
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&split, &args, value, head),
working_set.permanent().ctrlc.clone(),
)
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example> {
vec![

View File

@ -2,7 +2,7 @@ use std::path::Path;
use nu_path::expand_tilde;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
@ -43,6 +43,10 @@ impl Command for SubCommand {
If nothing is found, an empty string will be returned."#
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -63,6 +67,25 @@ If nothing is found, an empty string will be returned."#
)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let args = Arguments;
// This doesn't match explicit nulls
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: head });
}
input.map(
move |value| super::operate(&r#type, &args, value, head),
working_set.permanent().ctrlc.clone(),
)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {

View File

@ -3,7 +3,7 @@ use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::Category;
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
use unicode_segmentation::UnicodeSegmentation;
@ -62,6 +62,10 @@ impl Command for SubCommand {
vec!["size", "count"]
}
fn is_const(&self) -> bool {
true
}
fn run(
&self,
engine_state: &EngineState,
@ -70,11 +74,17 @@ impl Command for SubCommand {
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let args = Arguments {
cell_paths: (!cell_paths.is_empty()).then_some(cell_paths),
graphemes: grapheme_flags(call)?,
};
operate(action, args, input, call.head, engine_state.ctrlc.clone())
run(cell_paths, engine_state, call, input)
}
fn run_const(
&self,
working_set: &StateWorkingSet,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let cell_paths: Vec<CellPath> = call.rest_const(working_set, 0)?;
run(cell_paths, working_set.permanent(), call, input)
}
fn examples(&self) -> Vec<Example> {
@ -101,6 +111,19 @@ impl Command for SubCommand {
}
}
fn run(
cell_paths: Vec<CellPath>,
engine_state: &EngineState,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let args = Arguments {
cell_paths: (!cell_paths.is_empty()).then_some(cell_paths),
graphemes: grapheme_flags(call)?,
};
operate(action, args, input, call.head, engine_state.ctrlc.clone())
}
fn action(input: &Value, arg: &Arguments, head: Span) -> Value {
match input {
Value::String { val, .. } => Value::int(