diff --git a/src/main.rs b/src/main.rs index 7d04dec..5eef490 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = animal_reader.lines().filter_map(|line| line.ok()).collect(); - let adj_lines: Vec = adj_reader.lines().filter_map(|line| line.ok()).collect(); + let animal_lines: Vec = io::BufReader::new(animal_file) + .lines() + .map_while(Result::ok) + .collect(); + let adj_lines: Vec = 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;