make symbols optional

This commit is contained in:
notohh 2024-06-03 11:29:20 -04:00
parent 4b19fbfed6
commit 4a01bd5faa
Signed by: notohh
GPG key ID: BD47506D475EE86D
2 changed files with 14 additions and 4 deletions

View file

@ -5,6 +5,8 @@ pub struct Args {
#[arg(short, default_value_t = 16)]
pub length: usize,
#[arg(short)]
pub symbols: bool,
#[arg(short)]
pub write_to_file: bool,
#[arg(short, default_value_t = String::from("generated_pwd.txt"))]
pub file: String,

View file

@ -1,5 +1,5 @@
use clap::Parser;
use rand::Rng;
use rand::{distributions::Alphanumeric, Rng};
use std::iter;
use crate::Args;
@ -15,8 +15,16 @@ impl RandomString {
let mut rng = rand::thread_rng();
let char = || CHARSET[rng.gen_range(0..CHARSET.len())] as char;
let s: String = iter::repeat_with(char).take(args.length).collect();
s
if args.symbols {
let s: String = iter::repeat_with(char).take(args.length).collect();
s
} else {
let s: String = rng
.sample_iter(&Alphanumeric)
.take(args.length)
.map(char::from)
.collect();
s
}
}
}