Skip to content

Commit

Permalink
Fix custom converters with save (nushell#12833)
Browse files Browse the repository at this point in the history
# Description
Fixes nushell#10429 where `save` fails if a custom command is used as the file
format converter.

# Tests + Formatting
Added a test.
  • Loading branch information
IanManske authored and FilipAndersson245 committed May 18, 2024
1 parent f9203db commit 332513a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
41 changes: 19 additions & 22 deletions crates/nu-command/src/filesystem/save.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::progress_bar;
use nu_engine::get_eval_block;
#[allow(deprecated)]
use nu_engine::{command_prelude::*, current_dir};
use nu_path::expand_path_with;
Expand Down Expand Up @@ -311,12 +312,13 @@ fn input_to_bytes(
.map(|name| name.to_string_lossy().to_string())
};

if let Some(ext) = ext {
convert_to_extension(engine_state, &ext, stack, input, span)
let input = if let Some(ext) = ext {
convert_to_extension(engine_state, &ext, stack, input, span)?
} else {
let value = input.into_value(span);
value_to_bytes(value)
}
input
};

value_to_bytes(input.into_value(span))
}

/// Convert given data into content of file of specified extension if
Expand All @@ -328,24 +330,19 @@ fn convert_to_extension(
stack: &mut Stack,
input: PipelineData,
span: Span,
) -> Result<Vec<u8>, ShellError> {
let converter = engine_state.find_decl(format!("to {extension}").as_bytes(), &[]);

let output = match converter {
Some(converter_id) => {
let output = engine_state.get_decl(converter_id).run(
engine_state,
stack,
&Call::new(span),
input,
)?;

output.into_value(span)
) -> Result<PipelineData, ShellError> {
if let Some(decl_id) = engine_state.find_decl(format!("to {extension}").as_bytes(), &[]) {
let decl = engine_state.get_decl(decl_id);
if let Some(block_id) = decl.get_block_id() {
let block = engine_state.get_block(block_id);
let eval_block = get_eval_block(engine_state);
eval_block(engine_state, stack, block, input)
} else {
decl.run(engine_state, stack, &Call::new(span), input)
}
None => input.into_value(span),
};

value_to_bytes(output)
} else {
Ok(input)
}
}

/// Convert [`Value::String`] [`Value::Binary`] or [`Value::List`] into [`Vec`] of bytes
Expand Down
17 changes: 17 additions & 0 deletions crates/nu-command/tests/commands/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,20 @@ fn save_same_file_without_extension_pipeline() {
.contains("pipeline input and output are the same file"));
})
}

#[test]
fn save_with_custom_converter() {
Playground::setup("save_with_custom_converter", |dirs, _| {
let file = dirs.test().join("test.ndjson");

nu!(cwd: dirs.test(), pipeline(
r#"
def "to ndjson" []: any -> string { each { to json --raw } | to text } ;
{a: 1, b: 2} | save test.ndjson
"#
));

let actual = file_contents(file);
assert_eq!(actual, r#"{"a":1,"b":2}"#);
})
}

0 comments on commit 332513a

Please sign in to comment.