mirror of
https://github.com/nushell/nushell.git
synced 2025-02-18 03:21:05 +01:00
Remove panics from random integer and make the constraint more idiomatic (#2578)
* Remove panics from random integer and make the constraint more idiomatic * Add open intervals to NumericRange
This commit is contained in:
parent
193c4cc6d5
commit
798766b4b5
@ -1,16 +1,17 @@
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
|
use crate::deserializer::NumericRange;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
use rand::prelude::{thread_rng, Rng};
|
use rand::prelude::{thread_rng, Rng};
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
pub struct SubCommand;
|
pub struct SubCommand;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct IntegerArgs {
|
pub struct IntegerArgs {
|
||||||
min: Option<Tagged<u64>>,
|
range: Option<Tagged<NumericRange>>,
|
||||||
max: Option<Tagged<u64>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@ -20,13 +21,11 @@ impl WholeStreamCommand for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("random integer")
|
Signature::build("random integer").optional("range", SyntaxShape::Range, "Range of values")
|
||||||
.named("min", SyntaxShape::Int, "Minimum value", Some('m'))
|
|
||||||
.named("max", SyntaxShape::Int, "Maximum value", Some('x'))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Generate a random integer [--min <m>] [--max <x>]"
|
"Generate a random integer [min..max]"
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run(
|
async fn run(
|
||||||
@ -46,17 +45,17 @@ impl WholeStreamCommand for SubCommand {
|
|||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Generate a random integer less than or equal to 500",
|
description: "Generate a random integer less than or equal to 500",
|
||||||
example: "random integer --max 500",
|
example: "random integer ..500",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Generate a random integer greater than or equal to 100000",
|
description: "Generate a random integer greater than or equal to 100000",
|
||||||
example: "random integer --min 100000",
|
example: "random integer 100000..",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Generate a random integer between 1 and 10",
|
description: "Generate a random integer between 1 and 10",
|
||||||
example: "random integer --min 1 --max 10",
|
example: "random integer 1..10",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@ -67,26 +66,37 @@ pub async fn integer(
|
|||||||
args: CommandArgs,
|
args: CommandArgs,
|
||||||
registry: &CommandRegistry,
|
registry: &CommandRegistry,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let (IntegerArgs { min, max }, _) = args.process(®istry).await?;
|
let (IntegerArgs { range }, _) = args.process(®istry).await?;
|
||||||
|
|
||||||
let min = if let Some(min_tagged) = min {
|
let (min, max) = if let Some(range) = &range {
|
||||||
*min_tagged
|
(range.item.min(), range.item.max())
|
||||||
} else {
|
} else {
|
||||||
0
|
(0, u64::MAX)
|
||||||
};
|
};
|
||||||
|
|
||||||
let max = if let Some(max_tagged) = max {
|
match min.cmp(&max) {
|
||||||
*max_tagged
|
Ordering::Greater => Err(ShellError::labeled_error(
|
||||||
} else {
|
format!("Invalid range {}..{}", min, max),
|
||||||
u64::MAX
|
"expected a valid range",
|
||||||
};
|
range
|
||||||
|
.expect("Unexpected ordering error in random integer")
|
||||||
|
.span(),
|
||||||
|
)),
|
||||||
|
Ordering::Equal => {
|
||||||
|
let untagged_result = UntaggedValue::int(min).into_value(Tag::unknown());
|
||||||
|
Ok(OutputStream::one(ReturnSuccess::value(untagged_result)))
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let mut thread_rng = thread_rng();
|
||||||
|
// add 1 to max, because gen_range is right-exclusive
|
||||||
|
let max = max.saturating_add(1);
|
||||||
|
let result: u64 = thread_rng.gen_range(min, max);
|
||||||
|
|
||||||
let mut thread_rng = thread_rng();
|
let untagged_result = UntaggedValue::int(result).into_value(Tag::unknown());
|
||||||
let result: u64 = thread_rng.gen_range(min, max);
|
|
||||||
|
|
||||||
let untagged_result = UntaggedValue::int(result).into_value(Tag::unknown());
|
Ok(OutputStream::one(ReturnSuccess::value(untagged_result)))
|
||||||
|
}
|
||||||
Ok(OutputStream::one(ReturnSuccess::value(untagged_result)))
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -3,7 +3,7 @@ use crate::context::CommandRegistry;
|
|||||||
use crate::deserializer::NumericRange;
|
use crate::deserializer::NumericRange;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape};
|
use nu_protocol::{RangeInclusion, ReturnSuccess, Signature, SyntaxShape};
|
||||||
use nu_source::Tagged;
|
use nu_source::Tagged;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -44,11 +44,23 @@ async fn range(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputSt
|
|||||||
let registry = registry.clone();
|
let registry = registry.clone();
|
||||||
let (RangeArgs { area }, input) = args.process(®istry).await?;
|
let (RangeArgs { area }, input) = args.process(®istry).await?;
|
||||||
let range = area.item;
|
let range = area.item;
|
||||||
let (from, _) = range.from;
|
let (from, left_inclusive) = range.from;
|
||||||
let (to, _) = range.to;
|
let (to, right_inclusive) = range.to;
|
||||||
|
let from = from.map(|from| *from as usize).unwrap_or(0).saturating_add(
|
||||||
let from = *from as usize;
|
if left_inclusive == RangeInclusion::Inclusive {
|
||||||
let to = *to as usize;
|
0
|
||||||
|
} else {
|
||||||
|
1
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let to = to
|
||||||
|
.map(|to| *to as usize)
|
||||||
|
.unwrap_or(usize::MAX)
|
||||||
|
.saturating_sub(if right_inclusive == RangeInclusion::Inclusive {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
1
|
||||||
|
});
|
||||||
|
|
||||||
Ok(input
|
Ok(input
|
||||||
.skip(from)
|
.skip(from)
|
||||||
|
@ -12,8 +12,31 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
#[derive(Copy, Clone, Deserialize, Serialize)]
|
#[derive(Copy, Clone, Deserialize, Serialize)]
|
||||||
pub struct NumericRange {
|
pub struct NumericRange {
|
||||||
pub from: (Spanned<u64>, RangeInclusion),
|
pub from: (Option<Spanned<u64>>, RangeInclusion),
|
||||||
pub to: (Spanned<u64>, RangeInclusion),
|
pub to: (Option<Spanned<u64>>, RangeInclusion),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NumericRange {
|
||||||
|
pub fn min(self) -> u64 {
|
||||||
|
match self.from.1 {
|
||||||
|
RangeInclusion::Inclusive => self.from.0.map(|from| *from).unwrap_or(0),
|
||||||
|
RangeInclusion::Exclusive => {
|
||||||
|
self.from.0.map(|from| *from).unwrap_or(0).saturating_add(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn max(self) -> u64 {
|
||||||
|
match self.to.1 {
|
||||||
|
RangeInclusion::Inclusive => self.to.0.map(|to| *to).unwrap_or(u64::MAX),
|
||||||
|
RangeInclusion::Exclusive => self
|
||||||
|
.to
|
||||||
|
.0
|
||||||
|
.map(|to| *to)
|
||||||
|
.unwrap_or(u64::MAX)
|
||||||
|
.saturating_sub(1),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -443,12 +466,21 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> {
|
|||||||
let left_span = left.span;
|
let left_span = left.span;
|
||||||
let right_span = right.span;
|
let right_span = right.span;
|
||||||
|
|
||||||
let left = left.as_u64(left_span)?;
|
let left = match left.item {
|
||||||
let right = right.as_u64(right_span)?;
|
Primitive::Nothing => None,
|
||||||
|
_ => Some(left.as_u64(left_span)?),
|
||||||
|
};
|
||||||
|
let right = match right.item {
|
||||||
|
Primitive::Nothing => None,
|
||||||
|
_ => Some(right.as_u64(right_span)?),
|
||||||
|
};
|
||||||
|
|
||||||
let numeric_range = NumericRange {
|
let numeric_range = NumericRange {
|
||||||
from: (left.spanned(left_span), left_inclusion),
|
from: (left.map(|left| left.spanned(left_span)), left_inclusion),
|
||||||
to: (right.spanned(right_span), right_inclusion),
|
to: (
|
||||||
|
right.map(|right| right.spanned(right_span)),
|
||||||
|
right_inclusion,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
visit::<Tagged<NumericRange>, _>(numeric_range.tagged(tag), name, fields, visitor)
|
visit::<Tagged<NumericRange>, _>(numeric_range.tagged(tag), name, fields, visitor)
|
||||||
|
@ -5,9 +5,33 @@ fn generates_an_integer() {
|
|||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: ".", pipeline(
|
cwd: ".", pipeline(
|
||||||
r#"
|
r#"
|
||||||
random integer --min 42 --max 43
|
random integer 42..43
|
||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
|
||||||
assert!(actual.out.contains("42") || actual.out.contains("43"));
|
assert!(actual.out.contains("42") || actual.out.contains("43"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn generates_55() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
random integer 55..55
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(actual.out.contains("55"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn generates_0() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
random integer ..<1
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(actual.out.contains("0"));
|
||||||
|
}
|
||||||
|
@ -106,16 +106,16 @@ true
|
|||||||
```
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
> random integer --min 5000
|
> random integer 5000..
|
||||||
8700890823
|
8700890823
|
||||||
```
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
> random integer --max 100
|
> random integer ..100
|
||||||
73
|
73
|
||||||
```
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
> random integer --min 100000 --max 200000
|
> random integer 100000..200000
|
||||||
173400
|
173400
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user