mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 14:36:08 +02:00
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:
@ -154,7 +154,8 @@ fn get_arguments(
|
||||
eval_expression_fn,
|
||||
);
|
||||
let arg_type = "expr";
|
||||
let arg_value_name = debug_string_without_formatting(&evaluated_expression);
|
||||
let arg_value_name =
|
||||
debug_string_without_formatting(engine_state, &evaluated_expression);
|
||||
let arg_value_type = &evaluated_expression.get_type().to_string();
|
||||
let evaled_span = evaluated_expression.span();
|
||||
let arg_value_name_span_start = evaled_span.start as i64;
|
||||
@ -174,7 +175,8 @@ fn get_arguments(
|
||||
let arg_type = "positional";
|
||||
let evaluated_expression =
|
||||
get_expression_as_value(engine_state, stack, inner_expr, eval_expression_fn);
|
||||
let arg_value_name = debug_string_without_formatting(&evaluated_expression);
|
||||
let arg_value_name =
|
||||
debug_string_without_formatting(engine_state, &evaluated_expression);
|
||||
let arg_value_type = &evaluated_expression.get_type().to_string();
|
||||
let evaled_span = evaluated_expression.span();
|
||||
let arg_value_name_span_start = evaled_span.start as i64;
|
||||
@ -193,7 +195,8 @@ fn get_arguments(
|
||||
let arg_type = "unknown";
|
||||
let evaluated_expression =
|
||||
get_expression_as_value(engine_state, stack, inner_expr, eval_expression_fn);
|
||||
let arg_value_name = debug_string_without_formatting(&evaluated_expression);
|
||||
let arg_value_name =
|
||||
debug_string_without_formatting(engine_state, &evaluated_expression);
|
||||
let arg_value_type = &evaluated_expression.get_type().to_string();
|
||||
let evaled_span = evaluated_expression.span();
|
||||
let arg_value_name_span_start = evaled_span.start as i64;
|
||||
@ -212,7 +215,8 @@ fn get_arguments(
|
||||
let arg_type = "spread";
|
||||
let evaluated_expression =
|
||||
get_expression_as_value(engine_state, stack, inner_expr, eval_expression_fn);
|
||||
let arg_value_name = debug_string_without_formatting(&evaluated_expression);
|
||||
let arg_value_name =
|
||||
debug_string_without_formatting(engine_state, &evaluated_expression);
|
||||
let arg_value_type = &evaluated_expression.get_type().to_string();
|
||||
let evaled_span = evaluated_expression.span();
|
||||
let arg_value_name_span_start = evaled_span.start as i64;
|
||||
@ -245,7 +249,7 @@ fn get_expression_as_value(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn debug_string_without_formatting(value: &Value) -> String {
|
||||
pub fn debug_string_without_formatting(engine_state: &EngineState, value: &Value) -> String {
|
||||
match value {
|
||||
Value::Bool { val, .. } => val.to_string(),
|
||||
Value::Int { val, .. } => val.to_string(),
|
||||
@ -259,19 +263,31 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
|
||||
Value::List { vals: val, .. } => format!(
|
||||
"[{}]",
|
||||
val.iter()
|
||||
.map(debug_string_without_formatting)
|
||||
.map(|v| debug_string_without_formatting(engine_state, v))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
),
|
||||
Value::Record { val, .. } => format!(
|
||||
"{{{}}}",
|
||||
val.iter()
|
||||
.map(|(x, y)| format!("{}: {}", x, debug_string_without_formatting(y)))
|
||||
.map(|(x, y)| format!(
|
||||
"{}: {}",
|
||||
x,
|
||||
debug_string_without_formatting(engine_state, y)
|
||||
))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
),
|
||||
//TODO: It would be good to drill deeper into closures.
|
||||
Value::Closure { val, .. } => format!("<Closure {}>", val.block_id.get()),
|
||||
Value::Closure { val, .. } => {
|
||||
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);
|
||||
contents_string.to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
Value::Nothing { .. } => String::new(),
|
||||
Value::Error { error, .. } => format!("{error:?}"),
|
||||
Value::Binary { val, .. } => format!("{val:?}"),
|
||||
@ -280,7 +296,7 @@ pub fn debug_string_without_formatting(value: &Value) -> String {
|
||||
// that critical here
|
||||
Value::Custom { val, .. } => val
|
||||
.to_base_value(value.span())
|
||||
.map(|val| debug_string_without_formatting(&val))
|
||||
.map(|val| debug_string_without_formatting(engine_state, &val))
|
||||
.unwrap_or_else(|_| format!("<{}>", val.type_name())),
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ impl Command for Inspect {
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
@ -40,7 +40,7 @@ impl Command for Inspect {
|
||||
|
||||
let (cols, _rows) = terminal_size().unwrap_or((0, 0));
|
||||
|
||||
let table = inspect_table::build_table(input_val, description, cols as usize);
|
||||
let table = inspect_table::build_table(engine_state, input_val, description, cols as usize);
|
||||
|
||||
// Note that this is printed to stderr. The reason for this is so it doesn't disrupt the regular nushell
|
||||
// tabular output. If we printed to stdout, nushell would get confused with two outputs.
|
||||
|
@ -1,19 +1,23 @@
|
||||
// note: Seems like could be simplified
|
||||
// IMHO: it shall not take 300+ lines :)
|
||||
|
||||
use self::{global_horizontal_char::SetHorizontalChar, set_widths::SetWidths};
|
||||
use nu_protocol::engine::EngineState;
|
||||
use nu_protocol::Value;
|
||||
use nu_table::{string_width, string_wrap};
|
||||
|
||||
use tabled::{
|
||||
grid::config::ColoredConfig,
|
||||
settings::{peaker::Priority, width::Wrap, Settings, Style},
|
||||
Table,
|
||||
};
|
||||
|
||||
use self::{global_horizontal_char::SetHorizontalChar, set_widths::SetWidths};
|
||||
|
||||
pub fn build_table(value: Value, description: String, termsize: usize) -> String {
|
||||
let (head, mut data) = util::collect_input(value);
|
||||
pub fn build_table(
|
||||
engine_state: &EngineState,
|
||||
value: Value,
|
||||
description: String,
|
||||
termsize: usize,
|
||||
) -> String {
|
||||
let (head, mut data) = util::collect_input(engine_state, value);
|
||||
let count_columns = head.len();
|
||||
data.insert(0, head);
|
||||
|
||||
@ -195,10 +199,14 @@ fn push_empty_column(data: &mut Vec<Vec<String>>) {
|
||||
mod util {
|
||||
use crate::debug::explain::debug_string_without_formatting;
|
||||
use nu_engine::get_columns;
|
||||
use nu_protocol::engine::EngineState;
|
||||
use nu_protocol::Value;
|
||||
|
||||
/// Try to build column names and a table grid.
|
||||
pub fn collect_input(value: Value) -> (Vec<String>, Vec<Vec<String>>) {
|
||||
pub fn collect_input(
|
||||
engine_state: &EngineState,
|
||||
value: Value,
|
||||
) -> (Vec<String>, Vec<Vec<String>>) {
|
||||
let span = value.span();
|
||||
match value {
|
||||
Value::Record { val: record, .. } => {
|
||||
@ -210,7 +218,7 @@ mod util {
|
||||
},
|
||||
match vals
|
||||
.into_iter()
|
||||
.map(|s| debug_string_without_formatting(&s))
|
||||
.map(|s| debug_string_without_formatting(engine_state, &s))
|
||||
.collect::<Vec<String>>()
|
||||
{
|
||||
vals if vals.is_empty() => vec![],
|
||||
@ -220,7 +228,7 @@ mod util {
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
let mut columns = get_columns(&vals);
|
||||
let data = convert_records_to_dataset(&columns, vals);
|
||||
let data = convert_records_to_dataset(engine_state, &columns, vals);
|
||||
|
||||
if columns.is_empty() {
|
||||
columns = vec![String::from("")];
|
||||
@ -232,7 +240,7 @@ mod util {
|
||||
let lines = val
|
||||
.lines()
|
||||
.map(|line| Value::string(line.to_string(), span))
|
||||
.map(|val| vec![debug_string_without_formatting(&val)])
|
||||
.map(|val| vec![debug_string_without_formatting(engine_state, &val)])
|
||||
.collect();
|
||||
|
||||
(vec![String::from("")], lines)
|
||||
@ -240,47 +248,59 @@ mod util {
|
||||
Value::Nothing { .. } => (vec![], vec![]),
|
||||
value => (
|
||||
vec![String::from("")],
|
||||
vec![vec![debug_string_without_formatting(&value)]],
|
||||
vec![vec![debug_string_without_formatting(engine_state, &value)]],
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_records_to_dataset(cols: &[String], records: Vec<Value>) -> Vec<Vec<String>> {
|
||||
fn convert_records_to_dataset(
|
||||
engine_state: &EngineState,
|
||||
cols: &[String],
|
||||
records: Vec<Value>,
|
||||
) -> Vec<Vec<String>> {
|
||||
if !cols.is_empty() {
|
||||
create_table_for_record(cols, &records)
|
||||
create_table_for_record(engine_state, cols, &records)
|
||||
} else if cols.is_empty() && records.is_empty() {
|
||||
vec![]
|
||||
} else if cols.len() == records.len() {
|
||||
vec![records
|
||||
.into_iter()
|
||||
.map(|s| debug_string_without_formatting(&s))
|
||||
.map(|s| debug_string_without_formatting(engine_state, &s))
|
||||
.collect()]
|
||||
} else {
|
||||
records
|
||||
.into_iter()
|
||||
.map(|record| vec![debug_string_without_formatting(&record)])
|
||||
.map(|record| vec![debug_string_without_formatting(engine_state, &record)])
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn create_table_for_record(headers: &[String], items: &[Value]) -> Vec<Vec<String>> {
|
||||
fn create_table_for_record(
|
||||
engine_state: &EngineState,
|
||||
headers: &[String],
|
||||
items: &[Value],
|
||||
) -> Vec<Vec<String>> {
|
||||
let mut data = vec![Vec::new(); items.len()];
|
||||
|
||||
for (i, item) in items.iter().enumerate() {
|
||||
let row = record_create_row(headers, item);
|
||||
let row = record_create_row(engine_state, headers, item);
|
||||
data[i] = row;
|
||||
}
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
fn record_create_row(headers: &[String], item: &Value) -> Vec<String> {
|
||||
fn record_create_row(
|
||||
engine_state: &EngineState,
|
||||
headers: &[String],
|
||||
item: &Value,
|
||||
) -> Vec<String> {
|
||||
if let Value::Record { val, .. } = item {
|
||||
headers
|
||||
.iter()
|
||||
.map(|col| {
|
||||
val.get(col)
|
||||
.map(debug_string_without_formatting)
|
||||
.map(|v| debug_string_without_formatting(engine_state, v))
|
||||
.unwrap_or_else(String::new)
|
||||
})
|
||||
.collect()
|
||||
|
Reference in New Issue
Block a user