Reuse Closure type in Value::Closure (#10894)

# Description
Reuses the existing `Closure` type in `Value::Closure`. This will help
with the span refactoring for `Value`. Additionally, this allows us to
more easily box or unbox the `Closure` case should we chose to do so in
the future.

# User-Facing Changes
Breaking API change for `nu_protocol`.
This commit is contained in:
Ian Manske
2023-10-30 22:34:23 +00:00
committed by GitHub
parent d4cbab454e
commit 72cb4b6032
15 changed files with 119 additions and 121 deletions

View File

@ -178,7 +178,10 @@ impl PartialEq for HashableValue {
#[cfg(test)]
mod test {
use super::*;
use nu_protocol::ast::{CellPath, PathMember};
use nu_protocol::{
ast::{CellPath, PathMember},
engine::Closure,
};
use std::collections::{HashMap, HashSet};
#[test]
@ -237,7 +240,13 @@ mod test {
let span = Span::test_data();
let values = [
Value::list(vec![Value::bool(true, span)], span),
Value::closure(0, HashMap::new(), span),
Value::closure(
Closure {
block_id: 0,
captures: HashMap::new(),
},
span,
),
Value::nothing(span),
Value::error(ShellError::DidYouMean("what?".to_string(), span), span),
Value::cell_path(

View File

@ -259,7 +259,7 @@ fn nu_value_to_string(value: Value, separator: &str) -> String {
Err(error) => format!("{error:?}"),
},
Value::Block { val, .. } => format!("<Block {val}>"),
Value::Closure { val, .. } => format!("<Closure {val}>"),
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id),
Value::Nothing { .. } => String::new(),
Value::Error { error, .. } => format!("{error:?}"),
Value::Binary { val, .. } => format!("{val:?}"),

View File

@ -253,7 +253,7 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
},
//TODO: It would be good to drill in deeper to blocks and closures.
Value::Block { val, .. } => format!("<Block {val}>"),
Value::Closure { val, .. } => format!("<Closure {val}>"),
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id),
Value::Nothing { .. } => String::new(),
Value::Error { error, .. } => format!("{error:?}"),
Value::Binary { val, .. } => format!("{val:?}"),

View File

@ -37,17 +37,6 @@ impl Command for ViewSource {
let arg_span = arg.span();
match arg {
Value::Block { val: block_id, .. } | Value::Closure { val: block_id, .. } => {
let block = engine_state.get_block(block_id);
if let Some(span) = block.span {
let contents = engine_state.get_span_contents(span);
Ok(Value::string(String::from_utf8_lossy(contents), call.head)
.into_pipeline_data())
} else {
Ok(Value::string("<internal command>", call.head).into_pipeline_data())
}
}
Value::String { val, .. } => {
if let Some(decl_id) = engine_state.find_decl(val.as_bytes(), &[]) {
// arg is a command
@ -139,13 +128,27 @@ impl Command for ViewSource {
))
}
}
_ => Err(ShellError::GenericError(
"Cannot view value".to_string(),
"this value cannot be viewed".to_string(),
Some(arg_span),
None,
Vec::new(),
)),
value => {
if let Ok(block_id) = value.as_block() {
let block = engine_state.get_block(block_id);
if let Some(span) = block.span {
let contents = engine_state.get_span_contents(span);
Ok(Value::string(String::from_utf8_lossy(contents), call.head)
.into_pipeline_data())
} else {
Ok(Value::string("<internal command>", call.head).into_pipeline_data())
}
} else {
Err(ShellError::GenericError(
"Cannot view value".to_string(),
"this value cannot be viewed".to_string(),
Some(arg_span),
None,
Vec::new(),
))
}
}
}
}

View File

@ -142,7 +142,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
Err(error) => format!("{error:?}"),
},
Value::Block { val, .. } => format!("<Block {val}>"),
Value::Closure { val, .. } => format!("<Closure {val}>"),
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id),
Value::Nothing { .. } => String::new(),
Value::Error { error, .. } => format!("{error:?}"),
Value::Binary { val, .. } => format!("{val:?}"),