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:
JT
2023-09-04 02:27:29 +12:00
committed by GitHub
parent af79eb2943
commit 6cdfee3573
372 changed files with 5811 additions and 7448 deletions

View File

@ -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);