Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions crates/nu-command/src/formats/to/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ impl Command for ToYamlLike {
)
.param(
Flag::new("non-roundtrip")
.arg(SyntaxShape::String)
.arg(SyntaxShape::OneOf(vec![
SyntaxShape::String,
SyntaxShape::Nothing,
]))
.desc("How to handle values that are non-roundtrippable.")
.completion(Completion::new_list(&["error", "null", "lossy"])),
)
Expand Down Expand Up @@ -104,17 +107,19 @@ impl Command for ToYamlLike {
let compact_list_indent = call.get_flag(engine_state, stack, "compact-list-indent")?;
let quote_style = call.get_flag(engine_state, stack, "quote")?;
let non_roundtrip =
call.get_flag::<Spanned<String>>(engine_state, stack, "non-roundtrip")?;
call.get_flag::<Spanned<Option<String>>>(engine_state, stack, "non-roundtrip")?;
let non_roundtrip = match (
call.has_flag(engine_state, stack, "serialize")?,
non_roundtrip.as_ref().map(|nr| nr.item.as_ref()),
non_roundtrip
.as_ref()
.map(|nr| nr.item.as_ref().map(|nr| nr.as_ref())),
) {
// matching the spanned is way less comprehendible here, so we expect spans instead
(false, None | Some("error")) => NonRoundtrip::Error,
(true, None | Some("lossy")) => NonRoundtrip::Lossy {
(false, None | Some(Some("error"))) => NonRoundtrip::Error,
(true, None | Some(Some("lossy"))) => NonRoundtrip::Lossy {
engine_state: Box::new(engine_state.clone()),
},
(false, Some("null")) => NonRoundtrip::Null,
(false, Some(Some("null") | None)) => NonRoundtrip::Null,
(false, Some(_)) => {
return Err(ShellError::IncompatibleParametersSingle {
msg: "expected `error`, `null` or `lossy`".into(),
Expand Down
10 changes: 10 additions & 0 deletions crates/nu-command/tests/format_conversions/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,13 @@ fn convert_keys_are_quoted_only_when_required(#[case] input: &str, #[case] quote
};
Ok(())
}

#[rstest]
#[case::raw_null("null")]
#[case::string_null("'null'")]
#[nu_test_support::test]
fn null_as_non_roundtrip_value(#[case] null: &str) -> Result {
test()
.run(format!("{{|| version}} | to yaml --non-roundtrip {null}"))
.expect_value_eq("null\n")
}
Loading