mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
create nuon
crate from from nuon
and to nuon
(#12553)
# Description playing with the NUON format in Rust code in some plugins, we agreed with the team it was a great time to create a standalone NUON format to allow Rust devs to use this Nushell file format. > **Note** > this PR almost copy-pastes the code from `nu_commands/src/formats/from/nuon.rs` and `nu_commands/src/formats/to/nuon.rs` to `nuon/src/from.rs` and `nuon/src/to.rs`, with minor tweaks to make then standalone functions, e.g. remove the rest of the command implementations ### TODO - [x] add tests - [x] add documentation # User-Facing Changes devs will have access to a new crate, `nuon`, and two functions, `from_nuon` and `to_nuon` ```rust from_nuon( input: &str, span: Option<Span>, ) -> Result<Value, ShellError> ``` ```rust to_nuon( input: &Value, raw: bool, tabs: Option<usize>, indent: Option<usize>, span: Option<Span>, ) -> Result<String, ShellError> ``` # Tests + Formatting i've basically taken all the tests from `crates/nu-command/tests/format_conversions/nuon.rs` and converted them to use `from_nuon` and `to_nuon` instead of Nushell commands - i've created a `nuon_end_to_end` to run both conversions with an optional middle value to check that all is fine > **Note** > the `nuon::tests::read_code_should_fail_rather_than_panic` test does give different results locally and in the CI... > i've left it ignored with comments to help future us :) # After Submitting mention that in the release notes for sure!!
This commit is contained in:
@ -27,6 +27,7 @@ nu-table = { path = "../nu-table", version = "0.92.3" }
|
||||
nu-term-grid = { path = "../nu-term-grid", version = "0.92.3" }
|
||||
nu-utils = { path = "../nu-utils", version = "0.92.3" }
|
||||
nu-ansi-term = { workspace = true }
|
||||
nuon = { path = "../nuon", version = "0.92.3" }
|
||||
|
||||
alphanumeric-sort = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
|
@ -1,4 +1,3 @@
|
||||
use crate::formats::value_to_string;
|
||||
use itertools::Itertools;
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::PipelineMetadata;
|
||||
@ -216,7 +215,7 @@ fn sort_attributes(val: Value) -> Value {
|
||||
|
||||
fn generate_key(item: &ValueCounter) -> Result<String, ShellError> {
|
||||
let value = sort_attributes(item.val_to_compare.clone()); //otherwise, keys could be different for Records
|
||||
value_to_string(&value, Span::unknown(), 0, None)
|
||||
nuon::to_nuon(&value, true, None, None, Some(Span::unknown()))
|
||||
}
|
||||
|
||||
fn generate_results_with_count(head: Span, uniq_values: Vec<ValueCounter>) -> Vec<Value> {
|
||||
|
@ -1,10 +1,4 @@
|
||||
use nu_engine::command_prelude::*;
|
||||
use nu_protocol::{
|
||||
ast::{Expr, Expression, ListItem, RecordItem},
|
||||
engine::StateWorkingSet,
|
||||
Range, Unit,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FromNuon;
|
||||
@ -46,7 +40,7 @@ impl Command for FromNuon {
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
@ -54,98 +48,7 @@ impl Command for FromNuon {
|
||||
let head = call.head;
|
||||
let (string_input, _span, metadata) = input.collect_string_strict(head)?;
|
||||
|
||||
let engine_state = engine_state.clone();
|
||||
|
||||
let mut working_set = StateWorkingSet::new(&engine_state);
|
||||
|
||||
let mut block = nu_parser::parse(&mut working_set, None, string_input.as_bytes(), false);
|
||||
|
||||
if let Some(pipeline) = block.pipelines.get(1) {
|
||||
if let Some(element) = pipeline.elements.first() {
|
||||
return Err(ShellError::GenericError {
|
||||
error: "error when loading nuon text".into(),
|
||||
msg: "could not load nuon text".into(),
|
||||
span: Some(head),
|
||||
help: None,
|
||||
inner: vec![ShellError::OutsideSpannedLabeledError {
|
||||
src: string_input,
|
||||
error: "error when loading".into(),
|
||||
msg: "excess values when loading".into(),
|
||||
span: element.expr.span,
|
||||
}],
|
||||
});
|
||||
} else {
|
||||
return Err(ShellError::GenericError {
|
||||
error: "error when loading nuon text".into(),
|
||||
msg: "could not load nuon text".into(),
|
||||
span: Some(head),
|
||||
help: None,
|
||||
inner: vec![ShellError::GenericError {
|
||||
error: "error when loading".into(),
|
||||
msg: "excess values when loading".into(),
|
||||
span: Some(head),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
}],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let expr = if block.pipelines.is_empty() {
|
||||
Expression {
|
||||
expr: Expr::Nothing,
|
||||
span: head,
|
||||
custom_completion: None,
|
||||
ty: Type::Nothing,
|
||||
}
|
||||
} else {
|
||||
let mut pipeline = Arc::make_mut(&mut block).pipelines.remove(0);
|
||||
|
||||
if let Some(expr) = pipeline.elements.get(1) {
|
||||
return Err(ShellError::GenericError {
|
||||
error: "error when loading nuon text".into(),
|
||||
msg: "could not load nuon text".into(),
|
||||
span: Some(head),
|
||||
help: None,
|
||||
inner: vec![ShellError::OutsideSpannedLabeledError {
|
||||
src: string_input,
|
||||
error: "error when loading".into(),
|
||||
msg: "detected a pipeline in nuon file".into(),
|
||||
span: expr.expr.span,
|
||||
}],
|
||||
});
|
||||
}
|
||||
|
||||
if pipeline.elements.is_empty() {
|
||||
Expression {
|
||||
expr: Expr::Nothing,
|
||||
span: head,
|
||||
custom_completion: None,
|
||||
ty: Type::Nothing,
|
||||
}
|
||||
} else {
|
||||
pipeline.elements.remove(0).expr
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(err) = working_set.parse_errors.first() {
|
||||
return Err(ShellError::GenericError {
|
||||
error: "error when parsing nuon text".into(),
|
||||
msg: "could not parse nuon text".into(),
|
||||
span: Some(head),
|
||||
help: None,
|
||||
inner: vec![ShellError::OutsideSpannedLabeledError {
|
||||
src: string_input,
|
||||
error: "error when parsing".into(),
|
||||
msg: err.to_string(),
|
||||
span: err.span(),
|
||||
}],
|
||||
});
|
||||
}
|
||||
|
||||
let result = convert_to_value(expr, head, &string_input);
|
||||
|
||||
match result {
|
||||
match nuon::from_nuon(&string_input, Some(head)) {
|
||||
Ok(result) => Ok(result.into_pipeline_data_with_metadata(metadata)),
|
||||
Err(err) => Err(ShellError::GenericError {
|
||||
error: "error when loading nuon text".into(),
|
||||
@ -158,360 +61,6 @@ impl Command for FromNuon {
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_to_value(
|
||||
expr: Expression,
|
||||
span: Span,
|
||||
original_text: &str,
|
||||
) -> Result<Value, ShellError> {
|
||||
match expr.expr {
|
||||
Expr::BinaryOp(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "binary operators not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::UnaryNot(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "unary operators not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Block(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "blocks not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Closure(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "closures not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Binary(val) => Ok(Value::binary(val, span)),
|
||||
Expr::Bool(val) => Ok(Value::bool(val, span)),
|
||||
Expr::Call(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "calls not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::CellPath(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "subexpressions and cellpaths not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::DateTime(dt) => Ok(Value::date(dt, span)),
|
||||
Expr::ExternalCall(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "calls not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Filepath(val, _) => Ok(Value::string(val, span)),
|
||||
Expr::Directory(val, _) => Ok(Value::string(val, span)),
|
||||
Expr::Float(val) => Ok(Value::float(val, span)),
|
||||
Expr::FullCellPath(full_cell_path) => {
|
||||
if !full_cell_path.tail.is_empty() {
|
||||
Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "subexpressions and cellpaths not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
})
|
||||
} else {
|
||||
convert_to_value(full_cell_path.head, span, original_text)
|
||||
}
|
||||
}
|
||||
|
||||
Expr::Garbage => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "extra tokens in input file".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::GlobPattern(val, _) => Ok(Value::string(val, span)),
|
||||
Expr::ImportPattern(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "imports not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Overlay(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "overlays not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Int(val) => Ok(Value::int(val, span)),
|
||||
Expr::Keyword(kw, ..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: format!("{} not supported in nuon", String::from_utf8_lossy(&kw)),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::List(vals) => {
|
||||
let mut output = vec![];
|
||||
|
||||
for item in vals {
|
||||
match item {
|
||||
ListItem::Item(expr) => {
|
||||
output.push(convert_to_value(expr, span, original_text)?);
|
||||
}
|
||||
ListItem::Spread(_, inner) => {
|
||||
return Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "spread operator not supported in nuon".into(),
|
||||
span: inner.span,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::list(output, span))
|
||||
}
|
||||
Expr::MatchBlock(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "match blocks not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Nothing => Ok(Value::nothing(span)),
|
||||
Expr::Operator(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "operators not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Range(from, next, to, operator) => {
|
||||
let from = if let Some(f) = from {
|
||||
convert_to_value(*f, span, original_text)?
|
||||
} else {
|
||||
Value::nothing(expr.span)
|
||||
};
|
||||
|
||||
let next = if let Some(s) = next {
|
||||
convert_to_value(*s, span, original_text)?
|
||||
} else {
|
||||
Value::nothing(expr.span)
|
||||
};
|
||||
|
||||
let to = if let Some(t) = to {
|
||||
convert_to_value(*t, span, original_text)?
|
||||
} else {
|
||||
Value::nothing(expr.span)
|
||||
};
|
||||
|
||||
Ok(Value::range(
|
||||
Range::new(from, next, to, operator.inclusion, expr.span)?,
|
||||
expr.span,
|
||||
))
|
||||
}
|
||||
Expr::Record(key_vals) => {
|
||||
let mut record = Record::with_capacity(key_vals.len());
|
||||
let mut key_spans = Vec::with_capacity(key_vals.len());
|
||||
|
||||
for key_val in key_vals {
|
||||
match key_val {
|
||||
RecordItem::Pair(key, val) => {
|
||||
let key_str = match key.expr {
|
||||
Expr::String(key_str) => key_str,
|
||||
_ => {
|
||||
return Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "only strings can be keys".into(),
|
||||
span: key.span,
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(i) = record.index_of(&key_str) {
|
||||
return Err(ShellError::ColumnDefinedTwice {
|
||||
col_name: key_str,
|
||||
second_use: key.span,
|
||||
first_use: key_spans[i],
|
||||
});
|
||||
} else {
|
||||
key_spans.push(key.span);
|
||||
record.push(key_str, convert_to_value(val, span, original_text)?);
|
||||
}
|
||||
}
|
||||
RecordItem::Spread(_, inner) => {
|
||||
return Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "spread operator not supported in nuon".into(),
|
||||
span: inner.span,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::record(record, span))
|
||||
}
|
||||
Expr::RowCondition(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "row conditions not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Signature(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "signatures not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::String(s) => Ok(Value::string(s, span)),
|
||||
Expr::StringInterpolation(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "string interpolation not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Subexpression(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "subexpressions not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Table(mut headers, cells) => {
|
||||
let mut cols = vec![];
|
||||
|
||||
let mut output = vec![];
|
||||
|
||||
for key in headers.iter_mut() {
|
||||
let key_str = match &mut key.expr {
|
||||
Expr::String(key_str) => key_str,
|
||||
_ => {
|
||||
return Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "only strings can be keys".into(),
|
||||
span: expr.span,
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(idx) = cols.iter().position(|existing| existing == key_str) {
|
||||
return Err(ShellError::ColumnDefinedTwice {
|
||||
col_name: key_str.clone(),
|
||||
second_use: key.span,
|
||||
first_use: headers[idx].span,
|
||||
});
|
||||
} else {
|
||||
cols.push(std::mem::take(key_str));
|
||||
}
|
||||
}
|
||||
|
||||
for row in cells {
|
||||
if cols.len() != row.len() {
|
||||
return Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "table has mismatched columns".into(),
|
||||
span: expr.span,
|
||||
});
|
||||
}
|
||||
|
||||
let record = cols
|
||||
.iter()
|
||||
.zip(row)
|
||||
.map(|(col, cell)| {
|
||||
convert_to_value(cell, span, original_text).map(|val| (col.clone(), val))
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
output.push(Value::record(record, span));
|
||||
}
|
||||
|
||||
Ok(Value::list(output, span))
|
||||
}
|
||||
Expr::ValueWithUnit(val, unit) => {
|
||||
let size = match val.expr {
|
||||
Expr::Int(val) => val,
|
||||
_ => {
|
||||
return Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "non-integer unit value".into(),
|
||||
span: expr.span,
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
match unit.item {
|
||||
Unit::Byte => Ok(Value::filesize(size, span)),
|
||||
Unit::Kilobyte => Ok(Value::filesize(size * 1000, span)),
|
||||
Unit::Megabyte => Ok(Value::filesize(size * 1000 * 1000, span)),
|
||||
Unit::Gigabyte => Ok(Value::filesize(size * 1000 * 1000 * 1000, span)),
|
||||
Unit::Terabyte => Ok(Value::filesize(size * 1000 * 1000 * 1000 * 1000, span)),
|
||||
Unit::Petabyte => Ok(Value::filesize(
|
||||
size * 1000 * 1000 * 1000 * 1000 * 1000,
|
||||
span,
|
||||
)),
|
||||
Unit::Exabyte => Ok(Value::filesize(
|
||||
size * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
||||
span,
|
||||
)),
|
||||
|
||||
Unit::Kibibyte => Ok(Value::filesize(size * 1024, span)),
|
||||
Unit::Mebibyte => Ok(Value::filesize(size * 1024 * 1024, span)),
|
||||
Unit::Gibibyte => Ok(Value::filesize(size * 1024 * 1024 * 1024, span)),
|
||||
Unit::Tebibyte => Ok(Value::filesize(size * 1024 * 1024 * 1024 * 1024, span)),
|
||||
Unit::Pebibyte => Ok(Value::filesize(
|
||||
size * 1024 * 1024 * 1024 * 1024 * 1024,
|
||||
span,
|
||||
)),
|
||||
Unit::Exbibyte => Ok(Value::filesize(
|
||||
size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
|
||||
span,
|
||||
)),
|
||||
|
||||
Unit::Nanosecond => Ok(Value::duration(size, span)),
|
||||
Unit::Microsecond => Ok(Value::duration(size * 1000, span)),
|
||||
Unit::Millisecond => Ok(Value::duration(size * 1000 * 1000, span)),
|
||||
Unit::Second => Ok(Value::duration(size * 1000 * 1000 * 1000, span)),
|
||||
Unit::Minute => Ok(Value::duration(size * 1000 * 1000 * 1000 * 60, span)),
|
||||
Unit::Hour => Ok(Value::duration(size * 1000 * 1000 * 1000 * 60 * 60, span)),
|
||||
Unit::Day => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24) {
|
||||
Some(val) => Ok(Value::duration(val, span)),
|
||||
None => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "day duration too large".into(),
|
||||
msg: "day duration too large".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
},
|
||||
|
||||
Unit::Week => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24 * 7) {
|
||||
Some(val) => Ok(Value::duration(val, span)),
|
||||
None => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "week duration too large".into(),
|
||||
msg: "week duration too large".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
Expr::Var(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "variables not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::VarDecl(..) => Err(ShellError::OutsideSpannedLabeledError {
|
||||
src: original_text.to_string(),
|
||||
error: "Error when loading".into(),
|
||||
msg: "variable declarations not supported in nuon".into(),
|
||||
span: expr.span,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
@ -15,7 +15,6 @@ pub use self::toml::ToToml;
|
||||
pub use command::To;
|
||||
pub use json::ToJson;
|
||||
pub use md::ToMd;
|
||||
pub use nuon::value_to_string;
|
||||
pub use nuon::ToNuon;
|
||||
pub use text::ToText;
|
||||
pub use tsv::ToTsv;
|
||||
|
@ -1,10 +1,4 @@
|
||||
use core::fmt::Write;
|
||||
use fancy_regex::Regex;
|
||||
use nu_engine::{command_prelude::*, get_columns};
|
||||
use nu_parser::escape_quote_string;
|
||||
use nu_protocol::Range;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::ops::Bound;
|
||||
use nu_engine::command_prelude::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ToNuon;
|
||||
@ -55,17 +49,7 @@ impl Command for ToNuon {
|
||||
let span = call.head;
|
||||
let value = input.into_value(span);
|
||||
|
||||
let nuon_result = if raw {
|
||||
value_to_string(&value, span, 0, None)
|
||||
} else if let Some(tab_count) = tabs {
|
||||
value_to_string(&value, span, 0, Some(&"\t".repeat(tab_count)))
|
||||
} else if let Some(indent) = indent {
|
||||
value_to_string(&value, span, 0, Some(&" ".repeat(indent)))
|
||||
} else {
|
||||
value_to_string(&value, span, 0, None)
|
||||
};
|
||||
|
||||
match nuon_result {
|
||||
match nuon::to_nuon(&value, raw, tabs, indent, Some(span)) {
|
||||
Ok(serde_nuon_string) => {
|
||||
Ok(Value::string(serde_nuon_string, span).into_pipeline_data())
|
||||
}
|
||||
@ -108,245 +92,6 @@ impl Command for ToNuon {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value_to_string(
|
||||
v: &Value,
|
||||
span: Span,
|
||||
depth: usize,
|
||||
indent: Option<&str>,
|
||||
) -> Result<String, ShellError> {
|
||||
let (nl, sep) = get_true_separators(indent);
|
||||
let idt = get_true_indentation(depth, indent);
|
||||
let idt_po = get_true_indentation(depth + 1, indent);
|
||||
let idt_pt = get_true_indentation(depth + 2, indent);
|
||||
|
||||
match v {
|
||||
Value::Binary { val, .. } => {
|
||||
let mut s = String::with_capacity(2 * val.len());
|
||||
for byte in val {
|
||||
if write!(s, "{byte:02X}").is_err() {
|
||||
return Err(ShellError::UnsupportedInput {
|
||||
msg: "could not convert binary to string".into(),
|
||||
input: "value originates from here".into(),
|
||||
msg_span: span,
|
||||
input_span: v.span(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(format!("0x[{s}]"))
|
||||
}
|
||||
Value::Block { .. } => Err(ShellError::UnsupportedInput {
|
||||
msg: "blocks are currently not nuon-compatible".into(),
|
||||
input: "value originates from here".into(),
|
||||
msg_span: span,
|
||||
input_span: v.span(),
|
||||
}),
|
||||
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::Bool { val, .. } => {
|
||||
if *val {
|
||||
Ok("true".to_string())
|
||||
} else {
|
||||
Ok("false".to_string())
|
||||
}
|
||||
}
|
||||
Value::CellPath { .. } => Err(ShellError::UnsupportedInput {
|
||||
msg: "cell-paths are currently not nuon-compatible".to_string(),
|
||||
input: "value originates from here".into(),
|
||||
msg_span: span,
|
||||
input_span: v.span(),
|
||||
}),
|
||||
Value::Custom { .. } => Err(ShellError::UnsupportedInput {
|
||||
msg: "custom values are currently not nuon-compatible".to_string(),
|
||||
input: "value originates from here".into(),
|
||||
msg_span: span,
|
||||
input_span: v.span(),
|
||||
}),
|
||||
Value::Date { val, .. } => Ok(val.to_rfc3339()),
|
||||
// FIXME: make durations use the shortest lossless representation.
|
||||
Value::Duration { val, .. } => Ok(format!("{}ns", *val)),
|
||||
// Propagate existing errors
|
||||
Value::Error { error, .. } => Err(*error.clone()),
|
||||
// FIXME: make filesizes use the shortest lossless representation.
|
||||
Value::Filesize { val, .. } => Ok(format!("{}b", *val)),
|
||||
Value::Float { val, .. } => {
|
||||
// This serialises these as 'nan', 'inf' and '-inf', respectively.
|
||||
if &val.round() == val && val.is_finite() {
|
||||
Ok(format!("{}.0", *val))
|
||||
} else {
|
||||
Ok(format!("{}", *val))
|
||||
}
|
||||
}
|
||||
Value::Int { val, .. } => Ok(format!("{}", *val)),
|
||||
Value::List { vals, .. } => {
|
||||
let headers = get_columns(vals);
|
||||
if !headers.is_empty() && vals.iter().all(|x| x.columns().eq(headers.iter())) {
|
||||
// Table output
|
||||
let headers: Vec<String> = headers
|
||||
.iter()
|
||||
.map(|string| {
|
||||
if needs_quotes(string) {
|
||||
format!("{idt}\"{string}\"")
|
||||
} else {
|
||||
format!("{idt}{string}")
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let headers_output = headers.join(&format!(",{sep}{nl}{idt_pt}"));
|
||||
|
||||
let mut table_output = vec![];
|
||||
for val in vals {
|
||||
let mut row = vec![];
|
||||
|
||||
if let Value::Record { val, .. } = val {
|
||||
for val in val.values() {
|
||||
row.push(value_to_string_without_quotes(
|
||||
val,
|
||||
span,
|
||||
depth + 2,
|
||||
indent,
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
||||
table_output.push(row.join(&format!(",{sep}{nl}{idt_pt}")));
|
||||
}
|
||||
|
||||
Ok(format!(
|
||||
"[{nl}{idt_po}[{nl}{idt_pt}{}{nl}{idt_po}];{sep}{nl}{idt_po}[{nl}{idt_pt}{}{nl}{idt_po}]{nl}{idt}]",
|
||||
headers_output,
|
||||
table_output.join(&format!("{nl}{idt_po}],{sep}{nl}{idt_po}[{nl}{idt_pt}"))
|
||||
))
|
||||
} else {
|
||||
let mut collection = vec![];
|
||||
for val in vals {
|
||||
collection.push(format!(
|
||||
"{idt_po}{}",
|
||||
value_to_string_without_quotes(val, span, depth + 1, indent,)?
|
||||
));
|
||||
}
|
||||
Ok(format!(
|
||||
"[{nl}{}{nl}{idt}]",
|
||||
collection.join(&format!(",{sep}{nl}"))
|
||||
))
|
||||
}
|
||||
}
|
||||
Value::Nothing { .. } => Ok("null".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)?;
|
||||
match range.end() {
|
||||
Bound::Included(end) => Ok(format!(
|
||||
"{}..{}",
|
||||
start,
|
||||
value_to_string(&Value::float(end, span), span, depth + 1, indent)?
|
||||
)),
|
||||
Bound::Excluded(end) => Ok(format!(
|
||||
"{}..<{}",
|
||||
start,
|
||||
value_to_string(&Value::float(end, span), span, depth + 1, indent)?
|
||||
)),
|
||||
Bound::Unbounded => Ok(format!("{start}..",)),
|
||||
}
|
||||
}
|
||||
},
|
||||
Value::Record { val, .. } => {
|
||||
let mut collection = vec![];
|
||||
for (col, val) in &**val {
|
||||
collection.push(if needs_quotes(col) {
|
||||
format!(
|
||||
"{idt_po}\"{}\": {}",
|
||||
col,
|
||||
value_to_string_without_quotes(val, span, depth + 1, indent)?
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"{idt_po}{}: {}",
|
||||
col,
|
||||
value_to_string_without_quotes(val, span, depth + 1, indent)?
|
||||
)
|
||||
});
|
||||
}
|
||||
Ok(format!(
|
||||
"{{{nl}{}{nl}{idt}}}",
|
||||
collection.join(&format!(",{sep}{nl}"))
|
||||
))
|
||||
}
|
||||
Value::LazyRecord { val, .. } => {
|
||||
let collected = val.collect()?;
|
||||
value_to_string(&collected, span, depth + 1, indent)
|
||||
}
|
||||
// All strings outside data structures are quoted because they are in 'command position'
|
||||
// (could be mistaken for commands by the Nu parser)
|
||||
Value::String { val, .. } => Ok(escape_quote_string(val)),
|
||||
Value::Glob { val, .. } => Ok(escape_quote_string(val)),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_true_indentation(depth: usize, indent: Option<&str>) -> String {
|
||||
match indent {
|
||||
Some(i) => i.repeat(depth),
|
||||
None => "".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_true_separators(indent: Option<&str>) -> (String, String) {
|
||||
match indent {
|
||||
Some(_) => ("\n".to_string(), "".to_string()),
|
||||
None => ("".to_string(), " ".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn value_to_string_without_quotes(
|
||||
v: &Value,
|
||||
span: Span,
|
||||
depth: usize,
|
||||
indent: Option<&str>,
|
||||
) -> Result<String, ShellError> {
|
||||
match v {
|
||||
Value::String { val, .. } => Ok({
|
||||
if needs_quotes(val) {
|
||||
escape_quote_string(val)
|
||||
} else {
|
||||
val.clone()
|
||||
}
|
||||
}),
|
||||
_ => value_to_string(v, span, depth, indent),
|
||||
}
|
||||
}
|
||||
|
||||
// This hits, in order:
|
||||
// • Any character of []:`{}#'";()|$,
|
||||
// • Any digit (\d)
|
||||
// • Any whitespace (\s)
|
||||
// • Case-insensitive sign-insensitive float "keywords" inf, infinity and nan.
|
||||
static NEEDS_QUOTES_REGEX: Lazy<Regex> = Lazy::new(|| {
|
||||
Regex::new(r#"[\[\]:`\{\}#'";\(\)\|\$,\d\s]|(?i)^[+\-]?(inf(inity)?|nan)$"#)
|
||||
.expect("internal error: NEEDS_QUOTES_REGEX didn't compile")
|
||||
});
|
||||
|
||||
fn needs_quotes(string: &str) -> bool {
|
||||
if string.is_empty() {
|
||||
return true;
|
||||
}
|
||||
// These are case-sensitive keywords
|
||||
match string {
|
||||
// `true`/`false`/`null` are active keywords in JSON and NUON
|
||||
// `&&` is denied by the nu parser for diagnostics reasons
|
||||
// (https://github.com/nushell/nushell/pull/7241)
|
||||
// TODO: remove the extra check in the nuon codepath
|
||||
"true" | "false" | "null" | "&&" => return true,
|
||||
_ => (),
|
||||
};
|
||||
// All other cases are handled here
|
||||
NEEDS_QUOTES_REGEX.is_match(string).unwrap_or(false)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
#[test]
|
||||
|
@ -330,7 +330,7 @@ fn into_sqlite_big_insert() {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let nuon = nu_command::value_to_string(&value, Span::unknown(), 0, None).unwrap()
|
||||
let nuon = nuon::to_nuon(&value, true, None, None, Some(Span::unknown())).unwrap()
|
||||
+ &line_ending();
|
||||
|
||||
nuon_file.write_all(nuon.as_bytes()).unwrap();
|
||||
|
Reference in New Issue
Block a user