feat: over simplify the code
This commit is contained in:
65
src/main.rs
65
src/main.rs
@@ -1,49 +1,26 @@
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::Rng;
|
||||
use std::env;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
fn get_argument() -> usize {
|
||||
env::args()
|
||||
.nth(1)
|
||||
.and_then(|arg| arg.parse::<usize>().ok())
|
||||
.unwrap_or_else(|| 10)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let num_pseudonyms: usize = {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() > 1 {
|
||||
match args[1].parse::<usize>() {
|
||||
Ok(num) => num,
|
||||
Err(_) => {
|
||||
eprintln!("Le nombre de pseudonymes doit être un entier positif.");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
100
|
||||
}
|
||||
};
|
||||
let num_pseudonyms = get_argument();
|
||||
let adjectives: Vec<&str> = include_str!("../data/adjectives.txt").lines().collect();
|
||||
let nouns: Vec<&str> = include_str!("../data/nouns.txt").lines().collect();
|
||||
|
||||
let adjectives: Vec<String> = {
|
||||
BufReader::new(include_str!("../data/adjectives.txt").as_bytes())
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.collect()
|
||||
};
|
||||
|
||||
let nouns: Vec<String> = {
|
||||
BufReader::new(include_str!("../data/nouns.txt").as_bytes())
|
||||
.lines()
|
||||
.filter_map(Result::ok)
|
||||
.collect()
|
||||
};
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
for _ in 0..num_pseudonyms {
|
||||
let Some(adjective) = adjectives.choose(&mut rng) else {
|
||||
eprintln!("Impossible de choisir un adjectif.");
|
||||
std::process::exit(1);
|
||||
};
|
||||
let Some(noun) = nouns.choose(&mut rng) else {
|
||||
eprintln!("Impossible de choisir un nom.");
|
||||
std::process::exit(1);
|
||||
};
|
||||
let number: u32 = rng.gen_range(1..=999);
|
||||
println!("{}", format!("{}-{}-{}", adjective, noun, number));
|
||||
}
|
||||
(0..num_pseudonyms).for_each(|_| {
|
||||
println!(
|
||||
"{}",
|
||||
format!(
|
||||
"{}-{}-{}",
|
||||
&adjectives[fastrand::usize(0..adjectives.len())],
|
||||
&nouns[fastrand::usize(0..nouns.len())],
|
||||
fastrand::u32(0..1000)
|
||||
)
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user