generate function
This commit is contained in:
parent
dd7f14f516
commit
667d881750
@ -11,3 +11,4 @@ docopt = "1.1.0"
|
||||
serde = "1.0.91"
|
||||
log = "0.4.6"
|
||||
pretty_env_logger = "0.3.0"
|
||||
rand = "0.6.5"
|
||||
|
49
src/main.rs
49
src/main.rs
@ -1,6 +1,10 @@
|
||||
use docopt::Docopt;
|
||||
use log::debug;
|
||||
use rand::rngs::OsRng;
|
||||
use rand::seq::IteratorRandom;
|
||||
use rand::seq::SliceRandom;
|
||||
use serde::Deserialize;
|
||||
use log::{debug};
|
||||
use std::iter::FromIterator;
|
||||
|
||||
static USAGE: &'static str = "
|
||||
foil
|
||||
@ -28,6 +32,45 @@ struct Args {
|
||||
arg_spec: Option<String>,
|
||||
}
|
||||
|
||||
fn generate(spec: &str) {
|
||||
let (len, rest) = {
|
||||
let mut separated = spec.splitn(2, ",");
|
||||
let len = separated.next().unwrap();
|
||||
let rest = separated.next().unwrap();
|
||||
(len, rest)
|
||||
};
|
||||
let mut rnd = OsRng::new().unwrap();
|
||||
|
||||
let mut password_vec: Vec<char> = rest
|
||||
.chars()
|
||||
.cycle()
|
||||
.map(|c| match c {
|
||||
'a' => "abcdefghijklmnopqrstuvwxyz"
|
||||
.chars()
|
||||
.choose(&mut rnd)
|
||||
.expect("Could not get random element"),
|
||||
'A' => "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
.chars()
|
||||
.choose(&mut rnd)
|
||||
.expect("Could not get random element"),
|
||||
'1' => "1234567890"
|
||||
.chars()
|
||||
.choose(&mut rnd)
|
||||
.expect("Could not get random element"),
|
||||
'.' => ".,/?-=#%^"
|
||||
.chars()
|
||||
.choose(&mut rnd)
|
||||
.expect("Could not get random element"),
|
||||
_ => panic!("Unknown character spec: {}", c),
|
||||
})
|
||||
.take(len.parse().unwrap())
|
||||
.collect();
|
||||
|
||||
password_vec.shuffle(&mut rnd);
|
||||
let shuffled: String = String::from_iter(password_vec.into_iter());
|
||||
println!("{}", shuffled);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
pretty_env_logger::init();
|
||||
let args: Args = Docopt::new(USAGE)
|
||||
@ -35,4 +78,8 @@ fn main() {
|
||||
.unwrap_or_else(|e| e.exit());
|
||||
debug!("{:?}", args);
|
||||
|
||||
if args.cmd_generate {
|
||||
generate(&args.arg_spec.unwrap());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user