From 946da5ea5f99465cde92a9b3c6141a8006b83354 Mon Sep 17 00:00:00 2001 From: simon-curtis Date: Fri, 8 Nov 2024 09:43:50 +0000 Subject: [PATCH] Requested changes --- crates/nu-cmd-lang/src/example_support.rs | 20 +++++++++----------- crates/nu-command/src/bytes/at.rs | 20 ++++++++------------ crates/nu-command/src/filters/skip/skip_.rs | 8 ++++---- crates/nu-command/src/filters/take/take_.rs | 8 ++++---- 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/crates/nu-cmd-lang/src/example_support.rs b/crates/nu-cmd-lang/src/example_support.rs index 62cbcb07de..c7c61a2251 100644 --- a/crates/nu-cmd-lang/src/example_support.rs +++ b/crates/nu-cmd-lang/src/example_support.rs @@ -162,17 +162,15 @@ pub fn check_example_evaluates_to_expected_output( if let Some(expected) = example.result.as_ref() { let expected = DebuggableValue(expected); let result = DebuggableValue(&result); - if result != expected { - assert_eq!( - result, - expected, - "\x1b[31mError:\x1b[0m The result of example \x1b[34m'{}'\x1b[0m for the command \x1b[34m'{}'\x1b[0m differs from the expected value.\n\nExpected: {:?}\nActual: {:?}\n", - example.description, - cmd_name, - expected, - result, - ); - } + assert_eq!( + result, + expected, + "Error: The result of example '{}' for the command '{}' differs from the expected value.\n\nExpected: {:?}\nActual: {:?}\n", + example.description, + cmd_name, + expected, + result, + ); } } diff --git a/crates/nu-command/src/bytes/at.rs b/crates/nu-command/src/bytes/at.rs index 85f6dbe4c2..784459cd9e 100644 --- a/crates/nu-command/src/bytes/at.rs +++ b/crates/nu-command/src/bytes/at.rs @@ -85,15 +85,14 @@ impl Command for BytesAt { }; if let PipelineData::ByteStream(stream, metadata) = input { - match stream.slice( + let stream = stream.slice( call.head, call.arguments_span(), args.indexes.0, args.indexes.1, - ) { - Ok(stream) => Ok(PipelineData::ByteStream(stream, metadata)), - Err(err) => Err(err), - } + )?; + + Ok(PipelineData::ByteStream(stream, metadata)) } else { operate(map_value, args, input, call.head, engine_state.signals()) } @@ -150,7 +149,7 @@ fn map_value(input: &Value, args: &Arguments, head: Span) -> Value { let range = &args.indexes; match input { Value::Binary { val, .. } => { - let (start, end) = resolve_relative_range(range, val.len()); + let (start, end) = resolve_relative_range(range, &val.len()); let iter = val.iter().copied(); let bytes: Vec = if start > end { @@ -176,17 +175,14 @@ fn map_value(input: &Value, args: &Arguments, head: Span) -> Value { } } -fn resolve_relative_range(range: &Subbytes, len: usize) -> (usize, usize) { +fn resolve_relative_range(range: &Subbytes, len: &usize) -> (usize, usize) { let start = match range.0 { - start if start < 0 => match len as isize + start { - start if start < 0 => 0, - start => start as usize, - }, + start if start < 0 => len.checked_sub(start.abs() as usize).unwrap_or(0), start => start as usize, }; let end = match range.1 { - end if end < 0 => (len as isize + end) as usize, + end if end < 0 => len.checked_sub(end.abs() as usize).unwrap_or(0), end => end as usize, }; diff --git a/crates/nu-command/src/filters/skip/skip_.rs b/crates/nu-command/src/filters/skip/skip_.rs index 183fb9872a..d4adca1455 100644 --- a/crates/nu-command/src/filters/skip/skip_.rs +++ b/crates/nu-command/src/filters/skip/skip_.rs @@ -94,10 +94,10 @@ impl Command for Skip { PipelineData::ByteStream(stream, metadata) => { if stream.type_().is_binary_coercible() { let span = stream.span(); - match stream.skip(span, n as u64) { - Ok(stream) => Ok(PipelineData::ByteStream(stream, metadata)), - Err(err) => Err(err), - } + Ok(PipelineData::ByteStream( + stream.skip(span, n as u64)?, + metadata, + )) } else { Err(ShellError::OnlySupportsThisInputType { exp_input_type: "list, binary or range".into(), diff --git a/crates/nu-command/src/filters/take/take_.rs b/crates/nu-command/src/filters/take/take_.rs index 4f48a8aa5f..a03b1825da 100644 --- a/crates/nu-command/src/filters/take/take_.rs +++ b/crates/nu-command/src/filters/take/take_.rs @@ -89,10 +89,10 @@ impl Command for Take { PipelineData::ByteStream(stream, metadata) => { if stream.type_().is_binary_coercible() { let span = stream.span(); - match stream.take(span, rows_desired as u64) { - Ok(stream) => Ok(PipelineData::ByteStream(stream, metadata)), - Err(err) => Err(err), - } + Ok(PipelineData::ByteStream( + stream.take(span, rows_desired as u64)?, + metadata, + )) } else { Err(ShellError::OnlySupportsThisInputType { exp_input_type: "list, binary or range".into(),