make save stream on list stream data (#7675)

# Description

Closes: #7590

# User-Facing Changes

So the following command
```
1..100 | each { |i| sleep 400ms; $i} | save --raw -f output.txt
```

Will stream data to `output.txt`

But I'm note sure how to make a proper test for it, so I leave with no
new test cases..

Also rename from `string_binary_list_value_to_bytes ` to
`value_to_bytes` to accepts more Value type.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
WindSoilder 2023-01-16 02:54:30 +08:00 committed by GitHub
parent 92c4097f8d
commit 0353eb4a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 10 deletions

View File

@ -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<Vec<u8>, ShellError> {
fn value_to_bytes(value: Value) -> Result<Vec<u8>, 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<Vec<u8>
}
// 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()),
}
}

View File

@ -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")
})
}