diff --git a/crates/nu-command/src/filesystem/save.rs b/crates/nu-command/src/filesystem/save.rs index da85fcc601..de87cda5a9 100644 --- a/crates/nu-command/src/filesystem/save.rs +++ b/crates/nu-command/src/filesystem/save.rs @@ -108,6 +108,20 @@ impl Command for Save { res } } + PipelineData::ListStream(ls, _) + if raw || prepare_path(&path, append, force)?.0.extension().is_none() => + { + let (mut file, _) = get_files(&path, &stderr_path, append, force)?; + for val in ls { + file.write_all(&value_to_bytes(val)?) + .map_err(|err| ShellError::IOError(err.to_string()))?; + file.write_all("\n".as_bytes()) + .map_err(|err| ShellError::IOError(err.to_string()))?; + } + file.flush()?; + + Ok(PipelineData::empty()) + } input => { let bytes = input_to_bytes(input, Path::new(&path.item), raw, engine_state, stack, span)?; @@ -182,7 +196,7 @@ fn input_to_bytes( convert_to_extension(engine_state, &ext, stack, input, span) } else { let value = input.into_value(span); - string_binary_list_value_to_bytes(value, span) + value_to_bytes(value) } } @@ -212,13 +226,13 @@ fn convert_to_extension( None => input.into_value(span), }; - string_binary_list_value_to_bytes(output, span) + value_to_bytes(output) } /// Convert [`Value::String`] [`Value::Binary`] or [`Value::List`] into [`Vec`] of bytes /// /// Propagates [`Value::Error`] and creates error otherwise -fn string_binary_list_value_to_bytes(value: Value, span: Span) -> Result, ShellError> { +fn value_to_bytes(value: Value) -> Result, ShellError> { match value { Value::String { val, .. } => Ok(val.into_bytes()), Value::Binary { val, .. } => Ok(val), @@ -234,13 +248,7 @@ fn string_binary_list_value_to_bytes(value: Value, span: Span) -> Result } // Propagate errors by explicitly matching them before the final case. Value::Error { error } => Err(error), - other => Err(ShellError::OnlySupportsThisInputType( - "string, binary or list".into(), - other.get_type().to_string(), - span, - // This line requires the Value::Error match above. - other.expect_span(), - )), + other => Ok(other.as_string()?.into_bytes()), } } diff --git a/crates/nu-command/tests/commands/save.rs b/crates/nu-command/tests/commands/save.rs index 40e3acabf1..2c42b07782 100644 --- a/crates/nu-command/tests/commands/save.rs +++ b/crates/nu-command/tests/commands/save.rs @@ -264,3 +264,20 @@ fn save_override_works_stderr() { assert_eq!(actual, "New Err\n"); }) } + +#[test] +fn save_list_stream() { + Playground::setup("save_test_13", |dirs, sandbox| { + sandbox.with_files(vec![]); + + let expected_file = dirs.test().join("list_sample.txt"); + + nu!( + cwd: dirs.root(), + r#"[a b c d] | each {|i| $i} | save -r save_test_13/list_sample.txt"#, + ); + + let actual = file_contents(expected_file); + assert_eq!(actual, "a\nb\nc\nd\n") + }) +}