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