66 lines
1.5 KiB
Rust
Executable File
66 lines
1.5 KiB
Rust
Executable File
use std::sync::Arc;
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use crate::static_data::DataLibrary;
|
|
use crate::static_data::Item;
|
|
use crate::StringKey;
|
|
|
|
/// A library to store all items.
|
|
#[derive(Debug)]
|
|
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
|
pub struct ItemLibrary {
|
|
/// The underlying data structure.
|
|
map: IndexMap<StringKey, Arc<Item>>,
|
|
}
|
|
|
|
impl ItemLibrary {
|
|
/// Instantiates a new Item Library.
|
|
pub fn new(capacity: usize) -> ItemLibrary {
|
|
ItemLibrary {
|
|
map: IndexMap::with_capacity(capacity),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl DataLibrary<'_, Item> for ItemLibrary {
|
|
fn map(&self) -> &IndexMap<StringKey, Arc<Item>> {
|
|
&self.map
|
|
}
|
|
|
|
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<Item>> {
|
|
&mut self.map
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub mod tests {
|
|
use hashbrown::HashSet;
|
|
|
|
use crate::static_data::libraries::data_library::DataLibrary;
|
|
use crate::static_data::libraries::item_library::ItemLibrary;
|
|
use crate::static_data::Item;
|
|
use crate::static_data::{BattleItemCategory, ItemCategory};
|
|
|
|
fn build_item() -> Item {
|
|
Item::new(
|
|
&"foo".into(),
|
|
ItemCategory::MiscItem,
|
|
BattleItemCategory::MiscBattleItem,
|
|
100,
|
|
HashSet::new(),
|
|
)
|
|
}
|
|
|
|
pub fn build() -> ItemLibrary {
|
|
let mut lib = ItemLibrary::new(1);
|
|
let m = build_item();
|
|
// Borrow as mut so we can insert
|
|
let w = &mut lib;
|
|
w.add(&"foo".into(), m);
|
|
// Drops borrow as mut
|
|
|
|
lib
|
|
}
|
|
}
|