From bc6d934fa130962f9b5017477eaa6e52daea3d8c Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Fri, 3 May 2024 03:25:19 +0200 Subject: [PATCH] add support for cell-paths to NUON (#12718) # Description _cell paths_ can be easily serialized back and forth to NUON with the leading `$.` syntax. # User-Facing Changes ```nushell $.foo.bar.0 | to nuon ``` and ```nushell "$.foo.bar.0" | from nuon ``` are now possible # Tests + Formatting a new `cell_path` test has been added to `nuon` # After Submitting --- crates/nuon/src/from.rs | 7 +------ crates/nuon/src/lib.rs | 20 +++++++++++++++++++- crates/nuon/src/to.rs | 7 +------ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/crates/nuon/src/from.rs b/crates/nuon/src/from.rs index e1ba37b966..457b12d5f9 100644 --- a/crates/nuon/src/from.rs +++ b/crates/nuon/src/from.rs @@ -148,12 +148,7 @@ fn convert_to_value( 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::CellPath(val) => Ok(Value::cell_path(val, span)), Expr::DateTime(dt) => Ok(Value::date(dt, span)), Expr::ExternalCall(..) => Err(ShellError::OutsideSpannedLabeledError { src: original_text.to_string(), diff --git a/crates/nuon/src/lib.rs b/crates/nuon/src/lib.rs index 22153a174f..cb86480580 100644 --- a/crates/nuon/src/lib.rs +++ b/crates/nuon/src/lib.rs @@ -14,7 +14,11 @@ pub use to::ToStyle; #[cfg(test)] mod tests { use chrono::DateTime; - use nu_protocol::{ast::RangeInclusion, engine::Closure, record, IntRange, Range, Span, Value}; + use nu_protocol::{ + ast::{CellPath, PathMember, RangeInclusion}, + engine::Closure, + record, IntRange, Range, Span, Value, + }; use crate::{from_nuon, to_nuon, ToStyle}; @@ -325,6 +329,20 @@ mod tests { ); } + #[test] + fn cell_path() { + nuon_end_to_end( + r#"$.foo.bar.0"#, + Some(Value::test_cell_path(CellPath { + members: vec![ + PathMember::string("foo".to_string(), false, Span::new(2, 5)), + PathMember::string("bar".to_string(), false, Span::new(6, 9)), + PathMember::int(0, false, Span::new(10, 11)), + ], + })), + ); + } + #[test] fn does_not_quote_strings_unnecessarily() { assert_eq!( diff --git a/crates/nuon/src/to.rs b/crates/nuon/src/to.rs index 8b69f17a87..30fe2664a2 100644 --- a/crates/nuon/src/to.rs +++ b/crates/nuon/src/to.rs @@ -97,12 +97,7 @@ fn value_to_string( 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::CellPath { val, .. } => Ok(format!("$.{}", val)), Value::Custom { .. } => Err(ShellError::UnsupportedInput { msg: "custom values are currently not nuon-compatible".to_string(), input: "value originates from here".into(),