Skip to content

Commit 1e9a8dc

Browse files
authored
Merge pull request #5 from blood-rogue/main
Performance improvements
2 parents 7ab5b02 + e98ea72 commit 1e9a8dc

File tree

5 files changed

+7250
-7234
lines changed

5 files changed

+7250
-7234
lines changed

README.md

+27-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ By default, the random words are joined by a space.
3232
You can change that using `--joiner` or `-j`.
3333
It doesn't necessarily have to be a single character.
3434

35-
If you specify `\n` in `--join`, it's treated as a newline character; `\t` is a tab character.
35+
If you specify `\n` in `--joiner`, it's treated as a newline character; `\t` is a tab character.
3636
Use `--raw` or `-r` to disable this functionality.
3737

38-
`--caps` or `-c` will CAPITALIZE every word. `--title` or `-t` will Titlecase every word.
38+
Use `--case` or `-c` to specify the case of the words. Supported values are `caps` for Capitalized words, `title` for Title case words and `lower` for lower case letters which is a default.
39+
3940

4041
## How do I install this?
4142

@@ -45,9 +46,31 @@ Have git and rust installed
4546

4647
```
4748
cargo install --git https://github.com/Axlefublr/uclanr.git
48-
uclanr 5
49+
```
50+
51+
## Usage
52+
53+
```
54+
$ uclanr --help
55+
A word generator that specializes in useful words
56+
57+
Usage: uclanr.exe [OPTIONS] [AMOUNT]
58+
59+
Arguments:
60+
[AMOUNT]
61+
Amount of random words to print [default: 1]
62+
63+
Options:
64+
-j, --joiner <JOINER>
65+
The string that joines the random words, if there are more than 1 [default: " "]
66+
-c, --case <CASE>
67+
Sets the case of every word [default: lower] [possible values: caps, title, lower]
68+
-r, --raw
69+
Disable interpreting \n as a newline and \t as a tab
70+
-h, --help
71+
Print help
4972
```
5073

5174
## Contribution
5275

53-
`git pull` the dev branch before any changes, please
76+
`git pull` the dev branch before any changes, please

src/args.rs

+47-26
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,66 @@
1-
use clap::Parser;
2-
use crate::Case;
1+
use clap::{Parser, ValueEnum};
32

4-
#[derive(Parser, Debug)]
5-
#[command(author, about, long_about = None)]
6-
#[command(next_line_help = true)]
3+
#[derive(Parser)]
4+
#[command(author, about, next_line_help = true)]
75
pub struct Args {
86
/// The string that joines the random words, if there are more than 1
97
#[arg(short, long, default_value_t = String::from(" "))]
108
joiner: String,
11-
/// Uppercase every word
12-
#[arg(short, long)]
13-
pub caps: bool,
14-
/// Titlecase every word
15-
#[arg(short, long)]
16-
pub title: bool,
9+
10+
/// Sets the case of every word
11+
#[arg(short, long, value_enum, default_value_t = Case::Lower)]
12+
case: Case,
13+
1714
/// Disable interpreting \n as a newline and \t as a tab
18-
#[arg(short, long, default_value_t = false)]
15+
#[arg(short, long)]
1916
raw: bool,
17+
2018
/// Amount of random words to print
2119
#[arg(default_value_t = 1)]
2220
pub amount: usize,
2321
}
2422

2523
impl Args {
26-
pub fn get_case(&self) -> Case {
27-
if self.caps {
28-
Case::Caps
29-
} else if self.title {
30-
Case::Title
24+
pub fn get_joiner(&self) -> String {
25+
if self.raw {
26+
self.joiner.clone()
3127
} else {
32-
Case::Lower
28+
self.joiner
29+
.clone()
30+
.replace("\\n", "\n")
31+
.replace("\\t", "\t")
3332
}
3433
}
3534

36-
pub fn joiner(&self) -> String {
37-
match self.raw {
38-
false => self.joiner
39-
.clone()
40-
.replace("\\n", "\n")
41-
.replace("\\t", "\t"),
42-
true => self.joiner.clone(),
35+
pub fn alter_case(&self, mut words: Vec<String>) -> Vec<String> {
36+
self.case.alter_case(&mut words);
37+
words.to_vec()
38+
}
39+
}
40+
41+
#[derive(ValueEnum, Clone, Copy)]
42+
pub enum Case {
43+
Caps,
44+
Title,
45+
Lower,
46+
}
47+
48+
impl Case {
49+
fn alter_case(self, words: &mut [String]) {
50+
match self {
51+
Self::Caps => {
52+
for x in words.iter_mut() {
53+
x.make_ascii_uppercase();
54+
}
55+
}
56+
57+
Self::Title => {
58+
for x in words.iter_mut() {
59+
x[0..1].make_ascii_uppercase();
60+
}
61+
}
62+
63+
Self::Lower => {}
4364
}
4465
}
45-
}
66+
}

src/main.rs

+9-39
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,20 @@
1-
use rand::seq::SliceRandom;
2-
use std::io::BufRead;
31
use clap::Parser;
2+
use rand::seq::SliceRandom;
3+
use words::WORDS;
44

55
mod args;
6+
mod words;
67

78
fn main() {
89
let args = args::Args::parse();
9-
let bytes = include_bytes!("words.txt");
10-
let words: Vec<String> = bytes
11-
.lines()
12-
.collect::<Result<_, _>>()
13-
.expect("file parsed correctly");
14-
let mut picked_words = get_random_words(&words, args.amount);
15-
alter_case(&mut picked_words, args.get_case());
16-
println!("{}", picked_words.join(&args.joiner()));
10+
let picked_words = args.alter_case(get_random_words(args.amount));
11+
12+
println!("{}", picked_words.join(&args.get_joiner()));
1713
}
1814

19-
fn get_random_words(words: &[String], amount: usize) -> Vec<String> {
20-
words
15+
fn get_random_words(amount: usize) -> Vec<String> {
16+
WORDS
2117
.choose_multiple(&mut rand::thread_rng(), amount)
22-
.cloned()
18+
.map(|word| (*word).to_string())
2319
.collect()
2420
}
25-
26-
pub enum Case {
27-
Caps,
28-
Title,
29-
Lower
30-
}
31-
32-
fn alter_case(words: &mut [String], case: Case) {
33-
match case {
34-
Case::Caps => {
35-
words
36-
.iter_mut()
37-
.for_each(|x| {
38-
x.make_ascii_uppercase();
39-
});
40-
}
41-
Case::Title => {
42-
words
43-
.iter_mut()
44-
.for_each(|x| {
45-
x[0..1].make_ascii_uppercase();
46-
});
47-
}
48-
Case::Lower => ()
49-
}
50-
}

0 commit comments

Comments
 (0)