output a random number #21

Merged
PlexSheep merged 5 commits from feat/rand into devel 2024-05-23 13:25:16 +02:00
3 changed files with 36 additions and 3 deletions
Showing only changes of commit 0923bf5247 - Show all commits

View File

@ -19,4 +19,5 @@ clap = { version = "4.5.4", features = ["derive"] }
fast32 = "1.0.2" fast32 = "1.0.2"
libpt = { version = "0.5.1", features = ["bintols"], default-features = false } libpt = { version = "0.5.1", features = ["bintols"], default-features = false }
num = "0.4.3" num = "0.4.3"
rand = "0.8.5"

View File

@ -86,6 +86,11 @@ pub struct FormatOptions {
#[arg(short = 's', long)] #[arg(short = 's', long)]
/// format to base64 /// format to base64
base64: bool, base64: bool,
#[arg(short = 'r', long, default_value_t = 0)]
/// output random numbers
///
/// Add a user defined amount of cryptographically pseudorandom numbers to the number list.
rand: usize,
#[arg(short = 'z', long)] #[arg(short = 'z', long)]
/// format to base32 /// format to base32
base32: bool, base32: bool,
@ -183,6 +188,16 @@ impl FormatOptions {
pub fn push_number(&mut self, value: NumberType) { pub fn push_number(&mut self, value: NumberType) {
self.numbers.push(value) self.numbers.push(value)
} }
/// get rand
pub fn rand(&self) -> usize {
self.rand
}
/// set amount of extra random numbers manually
pub fn set_rand(&mut self, rand: usize) {
self.rand = rand;
}
} }
impl Default for FormatOptions { impl Default for FormatOptions {
@ -197,6 +212,7 @@ impl Default for FormatOptions {
base64: false, base64: false,
dec: false, dec: false,
numbers: vec![], numbers: vec![],
rand: 0
} }
} }
} }

View File

@ -10,9 +10,9 @@ use clap::{CommandFactory, Parser};
mod format; mod format;
use format::*; use format::*;
use numf::format::numf_parser; use numf::format::{numf_parser, NumberType};
fn main() { fn main() -> anyhow::Result<()> {
// try to read from stdin first, appending the numbers we read to the FormatOptions // try to read from stdin first, appending the numbers we read to the FormatOptions
let mut options = FormatOptions::parse(); let mut options = FormatOptions::parse();
let mut stdin_nums = Vec::new(); let mut stdin_nums = Vec::new();
@ -23,6 +23,7 @@ fn main() {
let whole: String = match String::from_utf8(stdin_nums) { let whole: String = match String::from_utf8(stdin_nums) {
Ok(r) => r, Ok(r) => r,
Err(e) => { Err(e) => {
eprintln!("{}", FormatOptions::command().render_usage());
eprintln!("stdin for this program only accepts text: {e:#?}"); eprintln!("stdin for this program only accepts text: {e:#?}");
exit(1); exit(1);
} }
@ -32,6 +33,7 @@ fn main() {
let number = match numf_parser(s) { let number = match numf_parser(s) {
Ok(n) => n, Ok(n) => n,
Err(e) => { Err(e) => {
eprintln!("{}", FormatOptions::command().render_usage());
eprintln!("could not parse number from stdin: {e:#?}"); eprintln!("could not parse number from stdin: {e:#?}");
exit(2); exit(2);
} }
@ -40,14 +42,27 @@ fn main() {
} }
} }
Err(e) => { Err(e) => {
eprintln!("{}", FormatOptions::command().render_usage());
eprintln!("could not read from stdin: {e:#?}"); eprintln!("could not read from stdin: {e:#?}");
exit(2); exit(2);
} }
}; };
} }
if options.rand() > 0 {
use rand::prelude::*;
let mut rand = rand::rngs::OsRng;
for _i in 0..options.rand() {
let mut by: [u8; 16] = [0; 16];
rand.fill_bytes(&mut by);
let n: NumberType = libpt::bintols::join::array_to_unsigned(&by)?;
options.push_number(n);
}
}
if options.numbers().is_empty() { if options.numbers().is_empty() {
format!("{}", FormatOptions::command().render_usage()); eprintln!("{}", FormatOptions::command().render_usage());
eprintln!("no numbers have been provided");
exit(1); exit(1);
} }
@ -59,4 +74,5 @@ fn main() {
for o in out { for o in out {
println!("{o}") println!("{o}")
} }
Ok(())
} }