use custom charset for random string
This commit is contained in:
parent
fa2181af70
commit
c5685c48d7
1 changed files with 9 additions and 6 deletions
|
@ -1,5 +1,6 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use rand::{distributions::Alphanumeric, Rng};
|
use rand::Rng;
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
use crate::Args;
|
use crate::Args;
|
||||||
|
|
||||||
|
@ -8,12 +9,14 @@ pub struct RandomString;
|
||||||
|
|
||||||
impl RandomString {
|
impl RandomString {
|
||||||
pub fn generate_string(&self) -> String {
|
pub fn generate_string(&self) -> String {
|
||||||
|
const CHARSET: &[u8] =
|
||||||
|
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let s: String = rand::thread_rng()
|
let mut rng = rand::thread_rng();
|
||||||
.sample_iter(&Alphanumeric)
|
let char = || CHARSET[rng.gen_range(0..CHARSET.len())] as char;
|
||||||
.take(args.length)
|
|
||||||
.map(char::from)
|
let s: String = iter::repeat_with(char).take(args.length).collect();
|
||||||
.collect();
|
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue