~chee/binaries

4570a8e314a643534226365fd65bcaf3e547d8c7 — chee 2 years ago 1c477f6
add getseq and getpassword
4 files changed, 78 insertions(+), 23 deletions(-)

A src/bin/getpassword.rs
M src/bin/getseq.rs
A src/lib.rs
D src/main.rs
A src/bin/getpassword.rs => src/bin/getpassword.rs +20 -0
@@ 0,0 1,20 @@
use binaries::Digit;
use binaries::Letter;
use rand::Rng;

fn main() {
    let mut rng = rand::thread_rng();
    let position = rng.gen_range(0, 4);
    for step in 0..4 {
        if step > 0 {
            print!("-");
        }
        let word = if step == position {
            format!("{}{}", rng.sample(Digit), rng.sample(Letter))
        } else {
            binaries::word()
        };
        print!("{}", word);
    }
    print!("\n");
}

M src/bin/getseq.rs => src/bin/getseq.rs +1 -20
@@ 1,25 1,6 @@
use rand::seq::SliceRandom;
use binaries::word;
use structopt::StructOpt;

const CONSONANT: [char; 21] = [
    'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x',
    'y', 'z',
];
const VOWEL: [char; 5] = ['a', 'e', 'i', 'o', 'u'];

fn word() -> String {
    let mut rng = rand::thread_rng();
    [
        CONSONANT.choose(&mut rng).unwrap().to_owned(),
        VOWEL.choose(&mut rng).unwrap().to_owned(),
        CONSONANT.choose(&mut rng).unwrap().to_owned(),
        VOWEL.choose(&mut rng).unwrap().to_owned(),
        CONSONANT.choose(&mut rng).unwrap().to_owned(),
    ]
    .iter()
    .collect()
}

#[derive(StructOpt)]
struct Options {
    times: Option<u16>,

A src/lib.rs => src/lib.rs +57 -0
@@ 0,0 1,57 @@
use rand::distributions::{Distribution, Uniform};
use rand::Rng;
use std::iter;

pub struct Consonant;
pub struct Vowel;
pub struct Digit;
pub struct Letter;

const CONSONANT: [char; 21] = [
    'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x',
    'y', 'z',
];

const VOWEL: [char; 5] = ['a', 'e', 'i', 'o', 'u'];

impl Distribution<char> for Consonant {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char {
        let range = Uniform::new(0, 21);
        CONSONANT[range.sample(rng)]
    }
}

impl Distribution<char> for Vowel {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char {
        let range = Uniform::new(0, 5);
        VOWEL[range.sample(rng)]
    }
}

impl Distribution<char> for Letter {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char {
        Uniform::new(b'A', b'Z').sample(rng).into()
    }
}

impl Distribution<usize> for Digit {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize {
        Uniform::new(0, 9).sample(rng)
    }
}

pub fn word() -> String {
    let mut crng = rand::thread_rng();
    let mut vrng = rand::thread_rng();
    let mut cons = iter::repeat(()).map(|()| crng.sample(Consonant));
    let mut vows = iter::repeat(()).map(|()| vrng.sample(Vowel));
    [
        cons.next().unwrap(),
        vows.next().unwrap(),
        cons.next().unwrap(),
        vows.next().unwrap(),
        cons.next().unwrap(),
    ]
    .iter()
    .collect()
}

D src/main.rs => src/main.rs +0 -3
@@ 1,3 0,0 @@
fn main() {
    println!("Hello, world!");
}