PkmnLib_rs/src/static_data/libraries/move_library.rs

71 lines
1.6 KiB
Rust
Raw Normal View History

use hashbrown::HashMap;
2022-06-11 18:51:37 +00:00
use crate::static_data::DataLibrary;
use crate::static_data::MoveData;
use crate::StringKey;
#[derive(Debug)]
pub struct MoveLibrary {
map: HashMap<StringKey, MoveData>,
list: Vec<StringKey>,
}
impl MoveLibrary {
pub fn new(capacity: usize) -> MoveLibrary {
MoveLibrary {
map: HashMap::with_capacity(capacity),
list: Vec::with_capacity(capacity),
}
}
}
impl DataLibrary<'_, MoveData> for MoveLibrary {
fn map(&self) -> &HashMap<StringKey, MoveData> {
&self.map
}
fn list_values(&self) -> &Vec<StringKey> {
&self.list
}
fn get_modify(&mut self) -> (&mut HashMap<StringKey, MoveData>, &mut Vec<StringKey>) {
(&mut self.map, &mut self.list)
}
}
#[cfg(test)]
pub mod tests {
use hashbrown::HashSet;
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::move_library::MoveLibrary;
use crate::static_data::{MoveCategory, MoveData, MoveTarget};
use crate::StringKey;
fn build_move() -> MoveData {
MoveData::new(
&"foo".into(),
0,
MoveCategory::Physical,
100,
100,
30,
MoveTarget::Any,
0,
None,
HashSet::new(),
)
}
pub fn build() -> MoveLibrary {
let mut lib = MoveLibrary::new(1);
let m = build_move();
// Borrow as mut so we can insert
let w = &mut lib;
w.add(&StringKey::new("foo"), m);
// Drops borrow as mut
lib
}
}