#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::restriction)]
use std::io::Cursor;
use crate::pokemon::Region;
use bimap::BiHashMap;
pub struct List {
ids: BiHashMap<usize, String>,
names: Vec<String>,
}
impl List {
pub fn read() -> Self {
const FILE: &'static str = include_str!("../data/names.csv");
let mut reader = csv::ReaderBuilder::new()
.has_headers(false)
.from_reader(Cursor::new(FILE));
const CAPACITY: usize = 1000;
let mut ids = BiHashMap::with_capacity(CAPACITY);
let mut names = Vec::with_capacity(CAPACITY);
for (i, entry) in reader.deserialize().enumerate() {
let record: (String, String) = entry.unwrap();
ids.insert(i, record.1);
names.push(record.0);
}
Self { ids, names }
}
pub fn format_name(&self, filename: &str) -> String {
let Some(id) = self.ids.get_by_right(filename) else {
return filename.to_owned();
};
let Some(name) = self.names.get(*id) else {
return filename.to_owned();
};
name.clone()
}
pub fn get_by_id(&self, id: usize) -> Option<&String> {
self.ids.get_by_left(&id)
}
pub fn random(&self) -> String {
let idx = rand::random_range(0..self.ids.len());
self.ids.get_by_left(&idx).unwrap().clone()
}
pub fn get_by_region(&self, region: Region) -> String {
let region = match region {
Region::Kanto => 0..=151,
Region::Johto => 152..=251,
Region::Hoenn => 252..=386,
Region::Sinnoh => 387..=493,
Region::Unova => 494..=649,
Region::Kalos => 650..=721,
Region::Alola => 722..=809,
Region::Galar => 810..=905,
};
let idx = rand::random_range(region);
self.ids.get_by_left(&idx).unwrap().clone()
}
}