Skip to content

Commit

Permalink
Fix span of invalid range (nushell#11207)
Browse files Browse the repository at this point in the history
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Try to improve the error message of invalid range.

* before 

![Screenshot from 2023-12-02
08-45-23](https://github.com/nushell/nushell/assets/15247421/4d4e3533-b6c6-42c4-9f59-d4d30e4ad5c2)


* after

![Screenshot from 2023-12-02
13-18-34](https://github.com/nushell/nushell/assets/15247421/d380dced-4b60-4b1a-9992-9e0727e22054)


# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# 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.
-->
  • Loading branch information
nibon7 committed Dec 2, 2023
1 parent 76bdda1 commit f36c055
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
26 changes: 20 additions & 6 deletions crates/nu-command/src/network/port.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::IntoPipelineData;
use nu_protocol::{IntoPipelineData, Span, Spanned};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};

use nu_protocol::{
Expand Down Expand Up @@ -67,22 +67,36 @@ fn get_free_port(
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let start_port: Option<usize> = call.opt(engine_state, stack, 0)?;
let end_port: Option<usize> = call.opt(engine_state, stack, 1)?;
let start_port: Option<Spanned<usize>> = call.opt(engine_state, stack, 0)?;
let end_port: Option<Spanned<usize>> = call.opt(engine_state, stack, 1)?;

let listener = if start_port.is_none() && end_port.is_none() {
// get free port from system.
TcpListener::bind("127.0.0.1:0")?
} else {
let start_port = start_port.unwrap_or(1024);
let end_port = end_port.unwrap_or(65535);
let (start_port, start_span) = match start_port {
Some(p) => (p.item, Some(p.span)),
None => (1024, None),
};

let (end_port, end_span) = match end_port {
Some(p) => (p.item, Some(p.span)),
None => (65535, None),
};

let range_span = match (start_span, end_span) {
(Some(start), Some(end)) => Span::new(start.start, end.end),
(Some(start), None) => start,
(None, Some(end)) => end,
(None, None) => call.head,
};

// check input range valid.
if start_port > end_port {
return Err(ShellError::InvalidRange {
left_flank: start_port.to_string(),
right_flank: end_port.to_string(),
span: call.head,
span: range_span,
});
}

Expand Down
14 changes: 9 additions & 5 deletions crates/nu-command/src/random/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, Range, ShellError, Signature, Span, SyntaxShape, Type, Value,
Category, Example, PipelineData, Range, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value,
};
use rand::prelude::{thread_rng, Rng};
use std::cmp::Ordering;
Expand Down Expand Up @@ -72,10 +73,13 @@ fn float(
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let range: Option<Range> = call.opt(engine_state, stack, 0)?;
let mut range_span = call.head;
let range: Option<Spanned<Range>> = call.opt(engine_state, stack, 0)?;

let (min, max) = if let Some(spanned_range) = range {
let r = spanned_range.item;
range_span = spanned_range.span;

let (min, max) = if let Some(r) = range {
if r.is_end_inclusive() {
(r.from.as_float()?, r.to.as_float()?)
} else if r.to.as_float()? >= 1.0 {
Expand All @@ -91,7 +95,7 @@ fn float(
Some(Ordering::Greater) => Err(ShellError::InvalidRange {
left_flank: min.to_string(),
right_flank: max.to_string(),
span,
span: range_span,
}),
Some(Ordering::Equal) => Ok(PipelineData::Value(
Value::float(min, Span::new(64, 64)),
Expand Down
12 changes: 8 additions & 4 deletions crates/nu-command/src/random/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, Range, ShellError, Signature, SyntaxShape, Type, Value,
Category, Example, PipelineData, Range, ShellError, Signature, Spanned, SyntaxShape, Type,
Value,
};
use rand::prelude::{thread_rng, Rng};
use std::cmp::Ordering;
Expand Down Expand Up @@ -73,9 +74,12 @@ fn integer(
call: &Call,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let range: Option<Range> = call.opt(engine_state, stack, 0)?;
let range: Option<Spanned<Range>> = call.opt(engine_state, stack, 0)?;

let (min, max) = if let Some(r) = range {
let mut range_span = call.head;
let (min, max) = if let Some(spanned_range) = range {
let r = spanned_range.item;
range_span = spanned_range.span;
if r.is_end_inclusive() {
(r.from.as_int()?, r.to.as_int()?)
} else if r.to.as_int()? > 0 {
Expand All @@ -91,7 +95,7 @@ fn integer(
Some(Ordering::Greater) => Err(ShellError::InvalidRange {
left_flank: min.to_string(),
right_flank: max.to_string(),
span,
span: range_span,
}),
Some(Ordering::Equal) => Ok(PipelineData::Value(Value::int(min, span), None)),
_ => {
Expand Down

0 comments on commit f36c055

Please sign in to comment.