diff options
| author | nsfisis <nsfisis@gmail.com> | 2021-06-19 21:09:57 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2021-06-19 21:10:36 +0900 |
| commit | 4dcf3dde286bb2602f2be96077b02d0947353e18 (patch) | |
| tree | ff30b16c9ee204f27140087181b9cad84c3a6b07 | |
| parent | f9f462cd2feb811891fe4e4919534cbdb6dcba2c (diff) | |
| download | rand-word-gen-4dcf3dde286bb2602f2be96077b02d0947353e18.tar.gz rand-word-gen-4dcf3dde286bb2602f2be96077b02d0947353e18.tar.zst rand-word-gen-4dcf3dde286bb2602f2be96077b02d0947353e18.zip | |
Add '--prefix' option
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/lib.rs | 11 | ||||
| -rw-r--r-- | src/main.rs | 37 |
4 files changed, 42 insertions, 10 deletions
@@ -100,7 +100,7 @@ dependencies = [ [[package]] name = "rand-word-gen" -version = "1.0.0" +version = "1.1.0" dependencies = [ "anyhow", "clap", @@ -1,6 +1,6 @@ [package] name = "rand-word-gen" -version = "1.0.0" +version = "1.1.0" authors = ["nsfisis"] edition = "2018" description = "Generates random pseudo-English words." @@ -13,9 +13,16 @@ impl Model { Model { chars } } - pub fn generate<Rng: RngCore>(&self, rng: &mut Rng, len: usize) -> String { + pub fn generate<Rng: RngCore>( + &self, + rng: &mut Rng, + len: usize, + first_char: Option<char>, + ) -> String { let mut result = String::with_capacity(len); - let mut prefix = NUM_OF_LETTERS; + let mut prefix = first_char + .map(|c| c as usize - b'a' as usize) + .unwrap_or(NUM_OF_LETTERS); for _ in 0..len { let chars = &self.chars[prefix]; let c = select_one_char(rng, &chars); diff --git a/src/main.rs b/src/main.rs index 4ebe0fa..e2e1b35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,36 @@ -use anyhow::{Context, Result}; +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 words = parse_args()?; + 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); - for _ in 0..words { + 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, "{}", model.generate(&mut rng, len))?; + writeln!( + out, + "{}{}", + config.prefix, + model.generate(&mut rng, len, first_char) + )?; } Ok(()) } -fn parse_args() -> Result<usize> { +fn parse_args() -> Result<Config> { use clap::{crate_description, crate_version, value_t, App, Arg}; let matches = App::new("rand-word-gen") @@ -34,8 +45,22 @@ fn parse_args() -> Result<usize> { .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")?; - Ok(words) + 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 }) } |
