mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:55:42 +02:00
Move Value to helpers, separate span call (#10121)
# Description As part of the refactor to split spans off of Value, this moves to using helper functions to create values, and using `.span()` instead of matching span out of Value directly. Hoping to get a few more helping hands to finish this, as there are a lot of commands to update :) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> Co-authored-by: WindSoilder <windsoilder@outlook.com>
This commit is contained in:
@ -231,7 +231,7 @@ fn get_documentation(
|
||||
redirect_stderr: true,
|
||||
parser_info: HashMap::new(),
|
||||
},
|
||||
PipelineData::Value(Value::List { vals, span }, None),
|
||||
PipelineData::Value(Value::list(vals, span), None),
|
||||
) {
|
||||
if let Ok((str, ..)) = result.collect_string_strict(span) {
|
||||
let _ = writeln!(long_desc, "\n{G}Input/output types{RESET}:");
|
||||
|
@ -191,15 +191,16 @@ pub fn current_dir_str(engine_state: &EngineState, stack: &Stack) -> Result<Stri
|
||||
/// Simplified version of current_dir_str() for constant evaluation
|
||||
pub fn current_dir_str_const(working_set: &StateWorkingSet) -> Result<String, ShellError> {
|
||||
if let Some(pwd) = working_set.get_env_var(PWD_ENV) {
|
||||
let span = pwd.span();
|
||||
match pwd {
|
||||
Value::String { val, span } => {
|
||||
Value::String { val, .. } => {
|
||||
if Path::new(val).is_absolute() {
|
||||
Ok(val.clone())
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Invalid current directory".to_string(),
|
||||
format!("The 'PWD' environment variable must be set to an absolute path. Found: '{val}'"),
|
||||
Some(*span),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new()
|
||||
))
|
||||
@ -383,38 +384,39 @@ fn get_converted_value(
|
||||
},
|
||||
];
|
||||
|
||||
if let Ok(Value::Closure {
|
||||
val: block_id,
|
||||
span: from_span,
|
||||
..
|
||||
}) = env_conversions.follow_cell_path_not_from_user_input(path_members, false)
|
||||
{
|
||||
let block = engine_state.get_block(block_id);
|
||||
if let Ok(v) = env_conversions.follow_cell_path_not_from_user_input(path_members, false) {
|
||||
let from_span = v.span();
|
||||
match v {
|
||||
Value::Closure { val: block_id, .. } => {
|
||||
let block = engine_state.get_block(block_id);
|
||||
|
||||
if let Some(var) = block.signature.get_positional(0) {
|
||||
let mut stack = stack.gather_captures(engine_state, &block.captures);
|
||||
if let Some(var_id) = &var.var_id {
|
||||
stack.add_var(*var_id, orig_val.clone());
|
||||
if let Some(var) = block.signature.get_positional(0) {
|
||||
let mut stack = stack.gather_captures(engine_state, &block.captures);
|
||||
if let Some(var_id) = &var.var_id {
|
||||
stack.add_var(*var_id, orig_val.clone());
|
||||
}
|
||||
|
||||
let result = eval_block(
|
||||
engine_state,
|
||||
&mut stack,
|
||||
block,
|
||||
PipelineData::new_with_metadata(None, val_span),
|
||||
true,
|
||||
true,
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(data) => ConversionResult::Ok(data.into_value(val_span)),
|
||||
Err(e) => ConversionResult::ConversionError(e),
|
||||
}
|
||||
} else {
|
||||
ConversionResult::ConversionError(ShellError::MissingParameter {
|
||||
param_name: "block input".into(),
|
||||
span: from_span,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let result = eval_block(
|
||||
engine_state,
|
||||
&mut stack,
|
||||
block,
|
||||
PipelineData::new_with_metadata(None, val_span),
|
||||
true,
|
||||
true,
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(data) => ConversionResult::Ok(data.into_value(val_span)),
|
||||
Err(e) => ConversionResult::ConversionError(e),
|
||||
}
|
||||
} else {
|
||||
ConversionResult::ConversionError(ShellError::MissingParameter {
|
||||
param_name: "block input".into(),
|
||||
span: from_span,
|
||||
})
|
||||
_ => ConversionResult::CellPathError,
|
||||
}
|
||||
} else {
|
||||
ConversionResult::CellPathError
|
||||
@ -428,48 +430,47 @@ fn ensure_path(scope: &mut HashMap<String, Value>, env_path_name: &str) -> Optio
|
||||
let mut error = None;
|
||||
|
||||
// If PATH/Path is still a string, force-convert it to a list
|
||||
match scope.get(env_path_name) {
|
||||
Some(Value::String { val, span }) => {
|
||||
// Force-split path into a list
|
||||
let span = *span;
|
||||
let paths = std::env::split_paths(val)
|
||||
.map(|p| Value::String {
|
||||
val: p.to_string_lossy().to_string(),
|
||||
span,
|
||||
})
|
||||
.collect();
|
||||
if let Some(value) = scope.get(env_path_name) {
|
||||
let span = value.span();
|
||||
match value {
|
||||
Value::String { val, .. } => {
|
||||
// Force-split path into a list
|
||||
let paths = std::env::split_paths(val)
|
||||
.map(|p| Value::string(p.to_string_lossy().to_string(), span))
|
||||
.collect();
|
||||
|
||||
scope.insert(env_path_name.to_string(), Value::list(paths, span));
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
// Must be a list of strings
|
||||
if !vals.iter().all(|v| matches!(v, Value::String { .. })) {
|
||||
error = error.or_else(|| {
|
||||
Some(ShellError::GenericError(
|
||||
format!("Wrong {env_path_name} environment variable value"),
|
||||
format!("{env_path_name} must be a list of strings"),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
val => {
|
||||
// All other values are errors
|
||||
let span = val.span();
|
||||
|
||||
scope.insert(env_path_name.to_string(), Value::List { vals: paths, span });
|
||||
}
|
||||
Some(Value::List { vals, span }) => {
|
||||
// Must be a list of strings
|
||||
if !vals.iter().all(|v| matches!(v, Value::String { .. })) {
|
||||
error = error.or_else(|| {
|
||||
Some(ShellError::GenericError(
|
||||
format!("Wrong {env_path_name} environment variable value"),
|
||||
format!("{env_path_name} must be a list of strings"),
|
||||
Some(*span),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
});
|
||||
}
|
||||
}
|
||||
Some(val) => {
|
||||
// All other values are errors
|
||||
let span = val.span();
|
||||
|
||||
error = error.or_else(|| {
|
||||
Some(ShellError::GenericError(
|
||||
format!("Wrong {env_path_name} environment variable value"),
|
||||
format!("{env_path_name} must be a list of strings"),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
});
|
||||
}
|
||||
None => { /* not preset, do nothing */ }
|
||||
}
|
||||
|
||||
error
|
||||
|
@ -33,7 +33,7 @@ pub fn eval_call(
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
if nu_utils::ctrl_c::was_pressed(&engine_state.ctrlc) {
|
||||
return Ok(Value::Nothing { span: call.head }.into_pipeline_data());
|
||||
return Ok(Value::nothing(call.head).into_pipeline_data());
|
||||
}
|
||||
let decl = engine_state.get_decl(call.decl_id);
|
||||
|
||||
@ -49,11 +49,7 @@ pub fn eval_call(
|
||||
caller_stack,
|
||||
decl.is_parser_keyword(),
|
||||
);
|
||||
Ok(Value::String {
|
||||
val: full_help,
|
||||
span: call.head,
|
||||
}
|
||||
.into_pipeline_data())
|
||||
Ok(Value::string(full_help, call.head).into_pipeline_data())
|
||||
} else if let Some(block_id) = decl.get_block_id() {
|
||||
let block = engine_state.get_block(block_id);
|
||||
|
||||
@ -101,10 +97,7 @@ pub fn eval_call(
|
||||
rest_positional
|
||||
.var_id
|
||||
.expect("Internal error: rest positional parameter lacks var_id"),
|
||||
Value::List {
|
||||
vals: rest_items,
|
||||
span,
|
||||
},
|
||||
Value::list(rest_items, span),
|
||||
)
|
||||
}
|
||||
|
||||
@ -145,7 +138,7 @@ pub fn eval_call(
|
||||
} else if let Some(value) = named.default_value {
|
||||
callee_stack.add_var(var_id, value);
|
||||
} else {
|
||||
callee_stack.add_var(var_id, Value::Nothing { span: call.head })
|
||||
callee_stack.add_var(var_id, Value::nothing(call.head))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -278,10 +271,7 @@ pub fn eval_expression(
|
||||
Expr::Bool(b) => Ok(Value::bool(*b, expr.span)),
|
||||
Expr::Int(i) => Ok(Value::int(*i, expr.span)),
|
||||
Expr::Float(f) => Ok(Value::float(*f, expr.span)),
|
||||
Expr::Binary(b) => Ok(Value::Binary {
|
||||
val: b.clone(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Binary(b) => Ok(Value::binary(b.clone(), expr.span)),
|
||||
Expr::ValueWithUnit(e, unit) => match eval_expression(engine_state, stack, e)? {
|
||||
Value::Int { val, .. } => compute(val, unit.item, unit.span),
|
||||
x => Err(ShellError::CantConvert {
|
||||
@ -295,46 +285,40 @@ pub fn eval_expression(
|
||||
let from = if let Some(f) = from {
|
||||
eval_expression(engine_state, stack, f)?
|
||||
} else {
|
||||
Value::Nothing { span: expr.span }
|
||||
Value::nothing(expr.span)
|
||||
};
|
||||
|
||||
let next = if let Some(s) = next {
|
||||
eval_expression(engine_state, stack, s)?
|
||||
} else {
|
||||
Value::Nothing { span: expr.span }
|
||||
Value::nothing(expr.span)
|
||||
};
|
||||
|
||||
let to = if let Some(t) = to {
|
||||
eval_expression(engine_state, stack, t)?
|
||||
} else {
|
||||
Value::Nothing { span: expr.span }
|
||||
Value::nothing(expr.span)
|
||||
};
|
||||
|
||||
Ok(Value::Range {
|
||||
val: Box::new(Range::new(expr.span, from, next, to, operator)?),
|
||||
span: expr.span,
|
||||
})
|
||||
Ok(Value::range(
|
||||
Range::new(expr.span, from, next, to, operator)?,
|
||||
expr.span,
|
||||
))
|
||||
}
|
||||
Expr::Var(var_id) => eval_variable(engine_state, stack, *var_id, expr.span),
|
||||
Expr::VarDecl(_) => Ok(Value::Nothing { span: expr.span }),
|
||||
Expr::CellPath(cell_path) => Ok(Value::CellPath {
|
||||
val: cell_path.clone(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::VarDecl(_) => Ok(Value::nothing(expr.span)),
|
||||
Expr::CellPath(cell_path) => Ok(Value::cell_path(cell_path.clone(), expr.span)),
|
||||
Expr::FullCellPath(cell_path) => {
|
||||
let value = eval_expression(engine_state, stack, &cell_path.head)?;
|
||||
|
||||
value.follow_cell_path(&cell_path.tail, false)
|
||||
}
|
||||
Expr::ImportPattern(_) => Ok(Value::Nothing { span: expr.span }),
|
||||
Expr::ImportPattern(_) => Ok(Value::nothing(expr.span)),
|
||||
Expr::Overlay(_) => {
|
||||
let name =
|
||||
String::from_utf8_lossy(engine_state.get_span_contents(expr.span)).to_string();
|
||||
|
||||
Ok(Value::String {
|
||||
val: name,
|
||||
span: expr.span,
|
||||
})
|
||||
Ok(Value::string(name, expr.span))
|
||||
}
|
||||
Expr::Call(call) => {
|
||||
// FIXME: protect this collect with ctrl-c
|
||||
@ -354,16 +338,10 @@ pub fn eval_expression(
|
||||
)?
|
||||
.into_value(span))
|
||||
}
|
||||
Expr::DateTime(dt) => Ok(Value::Date {
|
||||
val: *dt,
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
||||
Expr::MatchPattern(pattern) => Ok(Value::MatchPattern {
|
||||
val: pattern.clone(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::MatchBlock(_) => Ok(Value::Nothing { span: expr.span }), // match blocks are handled by `match`
|
||||
Expr::DateTime(dt) => Ok(Value::date(*dt, expr.span)),
|
||||
Expr::Operator(_) => Ok(Value::nothing(expr.span)),
|
||||
Expr::MatchPattern(pattern) => Ok(Value::match_pattern(*pattern.clone(), expr.span)),
|
||||
Expr::MatchBlock(_) => Ok(Value::nothing(expr.span)), // match blocks are handled by `match`
|
||||
Expr::UnaryNot(expr) => {
|
||||
let lhs = eval_expression(engine_state, stack, expr)?;
|
||||
match lhs {
|
||||
@ -567,25 +545,15 @@ pub fn eval_expression(
|
||||
for var_id in &block.captures {
|
||||
captures.insert(*var_id, stack.get_var(*var_id, expr.span)?);
|
||||
}
|
||||
Ok(Value::Closure {
|
||||
val: *block_id,
|
||||
captures,
|
||||
span: expr.span,
|
||||
})
|
||||
Ok(Value::closure(*block_id, captures, expr.span))
|
||||
}
|
||||
Expr::Block(block_id) => Ok(Value::Block {
|
||||
val: *block_id,
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Block(block_id) => Ok(Value::block(*block_id, expr.span)),
|
||||
Expr::List(x) => {
|
||||
let mut output = vec![];
|
||||
for expr in x {
|
||||
output.push(eval_expression(engine_state, stack, expr)?);
|
||||
}
|
||||
Ok(Value::List {
|
||||
vals: output,
|
||||
span: expr.span,
|
||||
})
|
||||
Ok(Value::list(output, expr.span))
|
||||
}
|
||||
Expr::Record(fields) => {
|
||||
let mut record = Record::new();
|
||||
@ -629,10 +597,7 @@ pub fn eval_expression(
|
||||
expr.span,
|
||||
));
|
||||
}
|
||||
Ok(Value::List {
|
||||
vals: output_rows,
|
||||
span: expr.span,
|
||||
})
|
||||
Ok(Value::list(output_rows, expr.span))
|
||||
}
|
||||
Expr::Keyword(_, _, expr) => eval_expression(engine_state, stack, expr),
|
||||
Expr::StringInterpolation(exprs) => {
|
||||
@ -647,15 +612,9 @@ pub fn eval_expression(
|
||||
.into_iter()
|
||||
.into_pipeline_data(None)
|
||||
.collect_string("", config)
|
||||
.map(|x| Value::String {
|
||||
val: x,
|
||||
span: expr.span,
|
||||
})
|
||||
.map(|x| Value::string(x, expr.span))
|
||||
}
|
||||
Expr::String(s) => Ok(Value::String {
|
||||
val: s.clone(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::String(s) => Ok(Value::string(s.clone(), expr.span)),
|
||||
Expr::Filepath(s) => {
|
||||
let cwd = current_dir_str(engine_state, stack)?;
|
||||
let path = expand_path_with(s, cwd);
|
||||
@ -678,9 +637,9 @@ pub fn eval_expression(
|
||||
|
||||
Ok(Value::string(path.to_string_lossy(), expr.span))
|
||||
}
|
||||
Expr::Signature(_) => Ok(Value::Nothing { span: expr.span }),
|
||||
Expr::Garbage => Ok(Value::Nothing { span: expr.span }),
|
||||
Expr::Nothing => Ok(Value::Nothing { span: expr.span }),
|
||||
Expr::Signature(_) => Ok(Value::nothing(expr.span)),
|
||||
Expr::Garbage => Ok(Value::nothing(expr.span)),
|
||||
Expr::Nothing => Ok(Value::nothing(expr.span)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -1152,10 +1111,10 @@ pub fn eval_block(
|
||||
}
|
||||
(Err(error), true) => {
|
||||
input = PipelineData::Value(
|
||||
Value::Error {
|
||||
error: Box::new(error),
|
||||
span: Span::unknown(), // FIXME: where does this span come from?
|
||||
},
|
||||
Value::error(
|
||||
error,
|
||||
Span::unknown(), // FIXME: where does this span come from?
|
||||
),
|
||||
None,
|
||||
)
|
||||
}
|
||||
@ -1301,11 +1260,8 @@ fn collect_profiling_metadata(
|
||||
Ok((PipelineData::ExternalStream { .. }, ..)) => {
|
||||
Value::string("raw stream", element_span)
|
||||
}
|
||||
Ok((PipelineData::Empty, ..)) => Value::Nothing { span: element_span },
|
||||
Err(err) => Value::Error {
|
||||
error: Box::new(err.clone()),
|
||||
span: element_span,
|
||||
},
|
||||
Ok((PipelineData::Empty, ..)) => Value::nothing(element_span),
|
||||
Err(err) => Value::error(err.clone(), element_span),
|
||||
};
|
||||
|
||||
record.push("value", value);
|
||||
|
@ -141,15 +141,10 @@ impl<'e, 's> ScopeData<'e, 's> {
|
||||
.map(|(input_type, output_type)| {
|
||||
(
|
||||
input_type.to_shape().to_string(),
|
||||
Value::List {
|
||||
vals: self.collect_signature_entries(
|
||||
input_type,
|
||||
output_type,
|
||||
signature,
|
||||
span,
|
||||
),
|
||||
Value::list(
|
||||
self.collect_signature_entries(input_type, output_type, signature, span),
|
||||
span,
|
||||
},
|
||||
),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<(String, Value)>>();
|
||||
@ -162,10 +157,10 @@ impl<'e, 's> ScopeData<'e, 's> {
|
||||
let any_type = &Type::Any;
|
||||
sigs.push((
|
||||
any_type.to_shape().to_string(),
|
||||
Value::List {
|
||||
vals: self.collect_signature_entries(any_type, any_type, signature, span),
|
||||
Value::list(
|
||||
self.collect_signature_entries(any_type, any_type, signature, span),
|
||||
span,
|
||||
},
|
||||
),
|
||||
));
|
||||
}
|
||||
sigs.sort_unstable_by(|(k1, _), (k2, _)| k1.cmp(k2));
|
||||
@ -518,10 +513,7 @@ impl<'e, 's> ScopeData<'e, 's> {
|
||||
|
||||
let export_env_block = module.env_block.map_or_else(
|
||||
|| Value::nothing(span),
|
||||
|block_id| Value::Block {
|
||||
val: block_id,
|
||||
span,
|
||||
},
|
||||
|block_id| Value::block(block_id, span),
|
||||
);
|
||||
|
||||
let module_usage = self
|
||||
|
Reference in New Issue
Block a user