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}"