Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom converters with save #12833

Merged
merged 3 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}"#);
})
}