Remove varibles for readers and create them inlign

This commit is contained in:
James Musselman 2024-11-05 16:53:45 -06:00
parent 7e7e2d10e4
commit 03d63a7906
Signed by: Musselman
GPG key ID: 1DAEFF35ECB5D6DB

View file

@ -2,23 +2,26 @@
// Released under AGPL-v3.0-or-later
// This program generates a version name using a random alliterative animal and adjective from their respective files.
use inflector::Inflector;
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::fs::File;
use std::io::{self, BufRead};
use inflector::Inflector;
use rand::thread_rng;
use rand::seq::SliceRandom;
fn main() -> io::Result<()> {
// Open the files
let animal_file = File::open("animals.txt")?;
let adj_file = File::open("adj.txt")?;
let animal_reader = io::BufReader::new(animal_file);
let adj_reader = io::BufReader::new(adj_file);
// Read each line into a vector
let animal_lines: Vec<String> = animal_reader.lines().filter_map(|line| line.ok()).collect();
let adj_lines: Vec<String> = adj_reader.lines().filter_map(|line| line.ok()).collect();
let animal_lines: Vec<String> = io::BufReader::new(animal_file)
.lines()
.map_while(Result::ok)
.collect();
let adj_lines: Vec<String> = io::BufReader::new(adj_file)
.lines()
.map_while(Result::ok)
.collect();
// Check if the animal file is empty
if animal_lines.is_empty() {
@ -34,7 +37,6 @@ fn main() -> io::Result<()> {
let mut rng = thread_rng();
loop {
// Generate random lines
let animal = animal_lines.choose(&mut rng).unwrap();
@ -44,20 +46,21 @@ fn main() -> io::Result<()> {
if animal.chars().next() == adjective.chars().next() {
// Print the matched version name
println!("\n{} {}\n", adjective, animal);
println!("Are you satisfied? (y/n)");
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
// Trim leading/trailing whitespaces
let input = input.trim();
if input.eq_ignore_ascii_case("y") {
break;
} else if input.eq_ignore_ascii_case("n") {
println!("Generating new output...")
} else {
println!("Invalid input. Please enter 'y' or 'n'.");
continue;