aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main.rs
blob: e2e1b358c73fe31edbaaa18ff4066bf8d57d2622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use anyhow::{bail, Context, Result};
use rand::distributions::{Distribution, Uniform};
use rand_word_gen::Model;
use std::io::{BufWriter, Write};

struct Config {
    words: usize,
    prefix: String,
}

fn main() -> Result<()> {
    let config = parse_args()?;

    let model = Model::new();
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    let mut rng = rand::thread_rng();
    let dist = Uniform::from(3..=6);
    let first_char = config.prefix.bytes().last().map(|c| c as char);
    for _ in 0..config.words {
        let len = dist.sample(&mut rng);
        writeln!(
            out,
            "{}{}",
            config.prefix,
            model.generate(&mut rng, len, first_char)
        )?;
    }

    Ok(())
}

fn parse_args() -> Result<Config> {
    use clap::{crate_description, crate_version, value_t, App, Arg};

    let matches = App::new("rand-word-gen")
        .version(crate_version!())
        .version_short("v")
        .about(crate_description!())
        .arg(
            Arg::with_name("words")
                .short("n")
                .long("words")
                .value_name("NUMBER_OF_WORDS")
                .default_value("20")
                .help("Sets number of generated words"),
        )
        .arg(
            Arg::with_name("prefix")
                .short("p")
                .long("prefix")
                .value_name("PREFIX")
                .default_value("")
                .help("Sets prefix of generated words"),
        )
        .get_matches();

    let words = value_t!(matches, "words", usize).context("'--words' must be a number")?;
    let mut prefix =
        value_t!(matches, "prefix", String).context("Fatal error: failed to parse '--prefix'")?;
    if !prefix.bytes().all(|c| c.is_ascii_alphabetic()) {
        bail!("'--prefix' must be alphabetic");
    }
    prefix.make_ascii_lowercase();
    Ok(Config { words, prefix })
}