1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; use rand::prelude::{thread_rng, Rng}; pub struct SubCommand; pub struct BoolArgs { bias: Option<Tagged<f64>>, } impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "random bool" } fn signature(&self) -> Signature { Signature::build("random bool").named( "bias", SyntaxShape::Number, "Adjusts the probability of a \"true\" outcome", Some('b'), ) } fn usage(&self) -> &str { "Generate a random boolean value" } fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> { bool_command(args) } fn examples(&self) -> Vec<Example> { vec![ Example { description: "Generate a random boolean value", example: "random bool", result: None, }, Example { description: "Generate a random boolean value with a 75% chance of \"true\"", example: "random bool --bias 0.75", result: None, }, ] } } pub fn bool_command(args: CommandArgs) -> Result<OutputStream, ShellError> { let cmd_args = BoolArgs { bias: args.get_flag("bias")?, }; let mut probability = 0.5; if let Some(prob) = cmd_args.bias { probability = *prob as f64; let probability_is_valid = (0.0..=1.0).contains(&probability); if !probability_is_valid { return Err(ShellError::labeled_error( "The probability is invalid", "invalid probability", prob.span(), )); } } let mut rng = thread_rng(); let bool_result: bool = rng.gen_bool(probability); Ok(OutputStream::one(UntaggedValue::boolean(bool_result))) } #[cfg(test)] mod tests { use super::ShellError; use super::SubCommand; #[test] fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; test_examples(SubCommand {}) } }