Convert the rest of "random" subcommands to engine-p (#3399)

* Convert "random bool" to engine-p

Also implements FromValue for Tagged<BigDecimal> and Tagged<f64>.

* Convert "random dice" to engine-p

* Convert "random uuid" to engine-p

* Covert "random chars" to engine-p

* Convert "random" command to engine-p
This commit is contained in:
Jakub Žádník 2021-05-10 10:07:57 +03:00 committed by GitHub
parent 75156ab0c9
commit 7a583083b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 31 deletions

View File

@ -1,13 +1,12 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
use rand::prelude::{thread_rng, Rng};
pub struct SubCommand;
#[derive(Deserialize)]
pub struct BoolArgs {
bias: Option<Tagged<f64>>,
}
@ -30,7 +29,7 @@ impl WholeStreamCommand for SubCommand {
"Generate a random boolean value"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
bool_command(args)
}
@ -50,12 +49,15 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn bool_command(args: CommandArgs) -> Result<ActionStream, ShellError> {
let (BoolArgs { bias }, _) = args.process()?;
pub fn bool_command(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once()?;
let cmd_args = BoolArgs {
bias: args.get_flag("bias")?,
};
let mut probability = 0.5;
if let Some(prob) = bias {
if let Some(prob) = cmd_args.bias {
probability = *prob as f64;
let probability_is_valid = (0.0..=1.0).contains(&probability);
@ -71,9 +73,8 @@ pub fn bool_command(args: CommandArgs) -> Result<ActionStream, ShellError> {
let mut rng = thread_rng();
let bool_result: bool = rng.gen_bool(probability);
let bool_untagged_value = UntaggedValue::boolean(bool_result);
Ok(ActionStream::one(ReturnSuccess::value(bool_untagged_value)))
Ok(OutputStream::one(UntaggedValue::boolean(bool_result)))
}
#[cfg(test)]

View File

@ -1,14 +1,13 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
use rand::distributions::Alphanumeric;
use rand::prelude::{thread_rng, Rng};
pub struct SubCommand;
#[derive(Deserialize)]
pub struct CharsArgs {
length: Option<Tagged<u32>>,
}
@ -33,7 +32,7 @@ impl WholeStreamCommand for SubCommand {
"Generate random chars"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
chars(args)
}
@ -53,18 +52,20 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn chars(args: CommandArgs) -> Result<ActionStream, ShellError> {
let (CharsArgs { length }, _) = args.process()?;
pub fn chars(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once()?;
let cmd_args = CharsArgs {
length: args.get_flag("length")?,
};
let chars_length = length.map_or(DEFAULT_CHARS_LENGTH, |l| l.item);
let chars_length = cmd_args.length.map_or(DEFAULT_CHARS_LENGTH, |l| l.item);
let random_string: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(chars_length as usize)
.collect();
let result = UntaggedValue::string(random_string);
Ok(ActionStream::one(ReturnSuccess::value(result)))
Ok(OutputStream::one(UntaggedValue::string(random_string)))
}
#[cfg(test)]

View File

@ -1,7 +1,7 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
use nu_protocol::{Signature, UntaggedValue};
pub struct Command;
@ -18,9 +18,10 @@ impl WholeStreamCommand for Command {
"Generate random values."
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
Ok(ActionStream::one(Ok(ReturnSuccess::Value(
UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()),
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(UntaggedValue::string(get_full_help(
&Command,
args.scope(),
))))
}
}

View File

@ -7,7 +7,6 @@ use rand::prelude::{thread_rng, Rng};
pub struct SubCommand;
#[derive(Deserialize)]
pub struct DiceArgs {
dice: Option<Tagged<u32>>,
sides: Option<Tagged<u32>>,
@ -38,7 +37,7 @@ impl WholeStreamCommand for SubCommand {
"Generate a random dice roll"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
dice(args)
}
@ -58,17 +57,22 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn dice(args: CommandArgs) -> Result<ActionStream, ShellError> {
pub fn dice(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (DiceArgs { dice, sides }, _) = args.process()?;
let dice = if let Some(dice_tagged) = dice {
let args = args.evaluate_once()?;
let cmd_args = DiceArgs {
dice: args.get_flag("dice")?,
sides: args.get_flag("sides")?,
};
let dice = if let Some(dice_tagged) = cmd_args.dice {
*dice_tagged
} else {
1
};
let sides = if let Some(sides_tagged) = sides {
let sides = if let Some(sides_tagged) = cmd_args.sides {
*sides_tagged
} else {
6
@ -79,7 +83,7 @@ pub fn dice(args: CommandArgs) -> Result<ActionStream, ShellError> {
UntaggedValue::int(thread_rng.gen_range(1, sides + 1)).into_value(tag.clone())
});
Ok((iter).to_action_stream())
Ok((iter).to_output_stream())
}
#[cfg(test)]

View File

@ -1,7 +1,7 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature};
use nu_protocol::Signature;
use uuid_crate::Uuid;
pub struct SubCommand;
@ -19,7 +19,7 @@ impl WholeStreamCommand for SubCommand {
"Generate a random uuid4 string"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
uuid(args)
}
@ -32,10 +32,10 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn uuid(_args: CommandArgs) -> Result<ActionStream, ShellError> {
pub fn uuid(_args: CommandArgs) -> Result<OutputStream, ShellError> {
let uuid_4 = Uuid::new_v4().to_hyphenated().to_string();
Ok(ActionStream::one(ReturnSuccess::value(uuid_4)))
Ok(OutputStream::one(uuid_4))
}
#[cfg(test)]

View File

@ -1,5 +1,6 @@
use std::path::PathBuf;
use bigdecimal::ToPrimitive;
use chrono::{DateTime, FixedOffset};
use nu_errors::ShellError;
use nu_protocol::{
@ -116,6 +117,36 @@ impl FromValue for bigdecimal::BigDecimal {
}
}
impl FromValue for Tagged<bigdecimal::BigDecimal> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let tag = v.tag.clone();
match &v.value {
UntaggedValue::Primitive(Primitive::Decimal(d)) => Ok(d.clone().tagged(tag)),
_ => Err(ShellError::labeled_error(
"Can't convert to decimal",
"can't convert to decimal",
tag.span,
)),
}
}
}
impl FromValue for Tagged<f64> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
let tag = v.tag.clone();
let decimal: bigdecimal::BigDecimal = FromValue::from_value(v)?;
match decimal.to_f64() {
Some(d) => Ok(d.tagged(tag)),
None => Err(ShellError::labeled_error(
"Can't convert to decimal",
"can't convert to decimal",
tag.span,
)),
}
}
}
impl FromValue for String {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {