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