more closure serialization (#14698)

# Description

This PR introduces a switch `--serialize` that allows serializing of
types that cannot be deserialized. Right now it only serializes closures
as strings in `to toml`, `to json`, `to nuon`, `to text`, some indirect
`to html` and `to yaml`.

A lot of the changes are just weaving the engine_state through calling
functions and the rest is just repetitive way of getting the closure
block span and grabbing the span's text.

In places where it has to report `<Closure 123>` I changed it to
`closure_123`. It always seemed like the `<>` were not very nushell-y.
This is still a breaking change.

I think this could also help with systematic translation of old config
to new config file.


# 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 toolkit.nu; toolkit test stdlib"` 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.
-->
This commit is contained in:
Darren Schroeder
2025-01-07 11:51:22 -06:00
committed by GitHub
parent 1f477c8eb1
commit dad956b2ee
23 changed files with 509 additions and 113 deletions

View File

@ -11,7 +11,7 @@ mod tests {
use chrono::DateTime;
use nu_protocol::{
ast::{CellPath, PathMember, RangeInclusion},
engine::Closure,
engine::{Closure, EngineState},
record, BlockId, IntRange, Range, Span, Value,
};
@ -25,11 +25,15 @@ mod tests {
/// an optional "middle" value can be given to test what the value is between `from nuon` and
/// `to nuon`.
fn nuon_end_to_end(input: &str, middle: Option<Value>) {
let engine_state = EngineState::new();
let val = from_nuon(input, None).unwrap();
if let Some(m) = middle {
assert_eq!(val, m);
}
assert_eq!(to_nuon(&val, ToStyle::Raw, None).unwrap(), input);
assert_eq!(
to_nuon(&engine_state, &val, ToStyle::Raw, None, false).unwrap(),
input
);
}
#[test]
@ -172,14 +176,19 @@ mod tests {
}
#[test]
#[ignore]
fn to_nuon_errs_on_closure() {
let engine_state = EngineState::new();
assert!(to_nuon(
&engine_state,
&Value::test_closure(Closure {
block_id: BlockId::new(0),
captures: vec![]
}),
ToStyle::Raw,
None,
false,
)
.unwrap_err()
.to_string()
@ -196,8 +205,17 @@ mod tests {
#[test]
fn binary_roundtrip() {
let engine_state = EngineState::new();
assert_eq!(
to_nuon(&from_nuon("0x[1f ff]", None).unwrap(), ToStyle::Raw, None).unwrap(),
to_nuon(
&engine_state,
&from_nuon("0x[1f ff]", None).unwrap(),
ToStyle::Raw,
None,
false,
)
.unwrap(),
"0x[1FFF]"
);
}
@ -237,40 +255,79 @@ mod tests {
#[test]
fn float_doesnt_become_int() {
let engine_state = EngineState::new();
assert_eq!(
to_nuon(&Value::test_float(1.0), ToStyle::Raw, None).unwrap(),
to_nuon(
&engine_state,
&Value::test_float(1.0),
ToStyle::Raw,
None,
false
)
.unwrap(),
"1.0"
);
}
#[test]
fn float_inf_parsed_properly() {
let engine_state = EngineState::new();
assert_eq!(
to_nuon(&Value::test_float(f64::INFINITY), ToStyle::Raw, None).unwrap(),
to_nuon(
&engine_state,
&Value::test_float(f64::INFINITY),
ToStyle::Raw,
None,
false,
)
.unwrap(),
"inf"
);
}
#[test]
fn float_neg_inf_parsed_properly() {
let engine_state = EngineState::new();
assert_eq!(
to_nuon(&Value::test_float(f64::NEG_INFINITY), ToStyle::Raw, None).unwrap(),
to_nuon(
&engine_state,
&Value::test_float(f64::NEG_INFINITY),
ToStyle::Raw,
None,
false,
)
.unwrap(),
"-inf"
);
}
#[test]
fn float_nan_parsed_properly() {
let engine_state = EngineState::new();
assert_eq!(
to_nuon(&Value::test_float(-f64::NAN), ToStyle::Raw, None).unwrap(),
to_nuon(
&engine_state,
&Value::test_float(-f64::NAN),
ToStyle::Raw,
None,
false,
)
.unwrap(),
"NaN"
);
}
#[test]
fn to_nuon_converts_columns_with_spaces() {
let engine_state = EngineState::new();
assert!(from_nuon(
&to_nuon(
&engine_state,
&Value::test_list(vec![
Value::test_record(record!(
"a" => Value::test_int(1),
@ -284,7 +341,8 @@ mod tests {
))
]),
ToStyle::Raw,
None
None,
false,
)
.unwrap(),
None,
@ -294,7 +352,15 @@ mod tests {
#[test]
fn to_nuon_quotes_empty_string() {
let res = to_nuon(&Value::test_string(""), ToStyle::Raw, None);
let engine_state = EngineState::new();
let res = to_nuon(
&engine_state,
&Value::test_string(""),
ToStyle::Raw,
None,
false,
);
assert!(res.is_ok());
assert_eq!(res.unwrap(), r#""""#);
}
@ -340,8 +406,11 @@ mod tests {
#[test]
fn does_not_quote_strings_unnecessarily() {
let engine_state = EngineState::new();
assert_eq!(
to_nuon(
&engine_state,
&Value::test_list(vec![
Value::test_record(record!(
"a" => Value::test_int(1),
@ -355,7 +424,8 @@ mod tests {
))
]),
ToStyle::Raw,
None
None,
false,
)
.unwrap(),
"[[a, b, \"c d\"]; [1, 2, 3], [4, 5, 6]]"
@ -363,12 +433,14 @@ mod tests {
assert_eq!(
to_nuon(
&engine_state,
&Value::test_record(record!(
"ro name" => Value::test_string("sam"),
"rank" => Value::test_int(10)
)),
ToStyle::Raw,
None
None,
false,
)
.unwrap(),
"{\"ro name\": sam, rank: 10}"

View File

@ -1,7 +1,6 @@
use core::fmt::Write;
use nu_engine::get_columns;
use nu_protocol::{Range, ShellError, Span, Value};
use nu_protocol::{engine::EngineState, Range, ShellError, Span, Value};
use nu_utils::{escape_quote_string, needs_quoting};
use std::ops::Bound;
@ -44,7 +43,13 @@ pub enum ToStyle {
/// > using this function in a command implementation such as [`to nuon`](https://www.nushell.sh/commands/docs/to_nuon.html).
///
/// also see [`super::from_nuon`] for the inverse operation
pub fn to_nuon(input: &Value, style: ToStyle, span: Option<Span>) -> Result<String, ShellError> {
pub fn to_nuon(
engine_state: &EngineState,
input: &Value,
style: ToStyle,
span: Option<Span>,
serialize_types: bool,
) -> Result<String, ShellError> {
let span = span.unwrap_or(Span::unknown());
let indentation = match style {
@ -53,16 +58,25 @@ pub fn to_nuon(input: &Value, style: ToStyle, span: Option<Span>) -> Result<Stri
ToStyle::Spaces(s) => Some(" ".repeat(s)),
};
let res = value_to_string(input, span, 0, indentation.as_deref())?;
let res = value_to_string(
engine_state,
input,
span,
0,
indentation.as_deref(),
serialize_types,
)?;
Ok(res)
}
fn value_to_string(
engine_state: &EngineState,
v: &Value,
span: Span,
depth: usize,
indent: Option<&str>,
serialize_types: bool,
) -> Result<String, ShellError> {
let (nl, sep) = get_true_separators(indent);
let idt = get_true_indentation(depth, indent);
@ -84,12 +98,25 @@ fn value_to_string(
}
Ok(format!("0x[{s}]"))
}
Value::Closure { .. } => Err(ShellError::UnsupportedInput {
msg: "closures are currently not nuon-compatible".into(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
}),
Value::Closure { val, .. } => {
if serialize_types {
let block = engine_state.get_block(val.block_id);
if let Some(span) = block.span {
let contents_bytes = engine_state.get_span_contents(span);
let contents_string = String::from_utf8_lossy(contents_bytes);
Ok(contents_string.to_string())
} else {
Ok(String::new())
}
} else {
Err(ShellError::UnsupportedInput {
msg: "closures are currently not nuon-compatible".into(),
input: "value originates from here".into(),
msg_span: span,
input_span: v.span(),
})
}
}
Value::Bool { val, .. } => {
if *val {
Ok("true".to_string())
@ -144,10 +171,12 @@ fn value_to_string(
if let Value::Record { val, .. } = val {
for val in val.values() {
row.push(value_to_string_without_quotes(
engine_state,
val,
span,
depth + 2,
indent,
serialize_types,
)?);
}
}
@ -165,7 +194,14 @@ fn value_to_string(
for val in vals {
collection.push(format!(
"{idt_po}{}",
value_to_string_without_quotes(val, span, depth + 1, indent,)?
value_to_string_without_quotes(
engine_state,
val,
span,
depth + 1,
indent,
serialize_types
)?
));
}
Ok(format!(
@ -178,18 +214,38 @@ fn value_to_string(
Value::Range { val, .. } => match **val {
Range::IntRange(range) => Ok(range.to_string()),
Range::FloatRange(range) => {
let start =
value_to_string(&Value::float(range.start(), span), span, depth + 1, indent)?;
let start = value_to_string(
engine_state,
&Value::float(range.start(), span),
span,
depth + 1,
indent,
serialize_types,
)?;
match range.end() {
Bound::Included(end) => Ok(format!(
"{}..{}",
start,
value_to_string(&Value::float(end, span), span, depth + 1, indent)?
value_to_string(
engine_state,
&Value::float(end, span),
span,
depth + 1,
indent,
serialize_types,
)?
)),
Bound::Excluded(end) => Ok(format!(
"{}..<{}",
start,
value_to_string(&Value::float(end, span), span, depth + 1, indent)?
value_to_string(
engine_state,
&Value::float(end, span),
span,
depth + 1,
indent,
serialize_types,
)?
)),
Bound::Unbounded => Ok(format!("{start}..",)),
}
@ -205,7 +261,14 @@ fn value_to_string(
};
collection.push(format!(
"{idt_po}{col}: {}",
value_to_string_without_quotes(val, span, depth + 1, indent)?
value_to_string_without_quotes(
engine_state,
val,
span,
depth + 1,
indent,
serialize_types
)?
));
}
Ok(format!(
@ -235,10 +298,12 @@ fn get_true_separators(indent: Option<&str>) -> (String, String) {
}
fn value_to_string_without_quotes(
engine_state: &EngineState,
v: &Value,
span: Span,
depth: usize,
indent: Option<&str>,
serialize_types: bool,
) -> Result<String, ShellError> {
match v {
Value::String { val, .. } => Ok({
@ -248,6 +313,6 @@ fn value_to_string_without_quotes(
val.clone()
}
}),
_ => value_to_string(v, span, depth, indent),
_ => value_to_string(engine_state, v, span, depth, indent, serialize_types),
}
}