use std::sync::Arc; use indexmap::IndexMap; use crate::static_data::DataLibrary; use crate::static_data::Species; use crate::StringKey; /// A library to store all data for Pokemon species. #[derive(Debug)] #[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))] pub struct SpeciesLibrary { /// The underlying map. map: IndexMap>, } impl SpeciesLibrary { /// Instantiates a new Species Library. pub fn new(capacity: usize) -> SpeciesLibrary { SpeciesLibrary { map: IndexMap::with_capacity(capacity), } } } impl<'a> DataLibrary<'a, Species> for SpeciesLibrary { fn map(&self) -> &IndexMap> { &self.map } fn get_modify(&mut self) -> &mut IndexMap> { &mut self.map } } #[cfg(test)] pub mod tests { use hashbrown::HashSet; use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::libraries::species_library::SpeciesLibrary; use crate::static_data::Form; use crate::static_data::LearnableMoves; use crate::static_data::Species; use crate::static_data::StaticStatisticSet; fn build_species() -> Species { Species::new( 0, &"foo".into(), 0.5, &"test_growthrate".into(), 0, Form::new( &"default".into(), 0.0, 0.0, 0, Vec::new(), StaticStatisticSet::default(), Vec::new(), Vec::new(), LearnableMoves::new(), HashSet::new(), ), HashSet::new(), ) } pub fn build() -> SpeciesLibrary { let mut lib = SpeciesLibrary::new(1); let species = build_species(); // Borrow as mut so we can insert let w = &mut lib; w.add(&"foo".into(), species); // Drops borrow as mut lib } #[test] fn add_species_to_library_and_fetch() { let lib = build(); // Borrow as read so we can read let r = &lib; let mon = r.get(&"foo".into()); assert!(mon.is_some()); assert_eq!(mon.unwrap().id(), 0_u16); assert_eq!(mon.unwrap().as_ref().name(), &"foo".into()); assert_eq!(r.len(), 1); } #[test] fn add_species_to_library_then_remove() { let mut lib = build(); lib.remove(&"foo".into()); // Borrow as read so we can read let r = &lib; let mon = r.get(&"foo".into()); assert!(mon.is_none()); assert_eq!(r.len(), 0); } }