Don't allow skip on external stream (#12559)

# Description
Close: #12514

# User-Facing Changes
`^ls | skip 1` will raise an error
```nushell
❯ ^ls | skip 1
Error: nu:🐚:only_supports_this_input_type

  × Input type not supported.
   ╭─[entry #1:1:2]
 1 │ ^ls | skip 1
   ·  ─┬   ──┬─
   ·   │     ╰── only list, binary or range input data is supported
   ·   ╰── input type: raw data
   ╰────
```

# Tests + Formatting
Sorry I can't add it because of the issue:
https://github.com/nushell/nushell/issues/12558

# After Submitting
Nan
This commit is contained in:
Wind 2024-04-19 22:54:59 +08:00 committed by GitHub
parent f2169c8022
commit 187b87c61c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 48 deletions

View File

@ -87,43 +87,12 @@ impl Command for Skip {
let ctrlc = engine_state.ctrlc.clone(); let ctrlc = engine_state.ctrlc.clone();
let input_span = input.span().unwrap_or(call.head); let input_span = input.span().unwrap_or(call.head);
match input { match input {
PipelineData::ExternalStream { PipelineData::ExternalStream { .. } => Err(ShellError::OnlySupportsThisInputType {
stdout: Some(stream), exp_input_type: "list, binary or range".into(),
span: bytes_span, wrong_type: "raw data".into(),
metadata, dst_span: call.head,
.. src_span: input_span,
} => { }),
let mut remaining = n;
let mut output = vec![];
for frame in stream {
let frame = frame?;
match frame {
Value::String { val, .. } => {
let bytes = val.as_bytes();
if bytes.len() < remaining {
remaining -= bytes.len();
//output.extend_from_slice(bytes)
} else {
output.extend_from_slice(&bytes[remaining..]);
break;
}
}
Value::Binary { val: bytes, .. } => {
if bytes.len() < remaining {
remaining -= bytes.len();
} else {
output.extend_from_slice(&bytes[remaining..]);
break;
}
}
_ => unreachable!("Raw streams are either bytes or strings"),
}
}
Ok(Value::binary(output, bytes_span).into_pipeline_data_with_metadata(metadata))
}
PipelineData::Value(Value::Binary { val, .. }, metadata) => { PipelineData::Value(Value::Binary { val, .. }, metadata) => {
let bytes = val.into_iter().skip(n).collect::<Vec<_>>(); let bytes = val.into_iter().skip(n).collect::<Vec<_>>();

View File

@ -1,18 +1,13 @@
use nu_test_support::{nu, pipeline}; use nu_test_support::nu;
#[test] #[test]
fn binary_skip() { fn binary_skip_will_raise_error() {
let actual = nu!( let actual = nu!(
cwd: "tests/fixtures/formats", pipeline( cwd: "tests/fixtures/formats",
r#" "open sample_data.ods --raw | skip 2"
open sample_data.ods --raw | );
skip 2 |
take 2 |
into int --endian big
"#
));
assert_eq!(actual.out, "772"); assert!(actual.err.contains("only_supports_this_input_type"));
} }
#[test] #[test]