76 lines
2.1 KiB
Rust
Executable File
76 lines
2.1 KiB
Rust
Executable File
use std::fmt::Debug;
|
|
use std::sync::Arc;
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use crate::static_data::Ability;
|
|
use crate::static_data::DataLibrary;
|
|
use crate::StringKey;
|
|
|
|
/// A storage for all abilities that can be used in this data library.
|
|
pub trait AbilityLibrary: DataLibrary<dyn Ability> + Debug {}
|
|
|
|
/// A storage for all abilities that can be used in this data library.
|
|
#[derive(Debug)]
|
|
pub struct AbilityLibraryImpl {
|
|
/// The underlying map for the library.
|
|
map: IndexMap<StringKey, Arc<dyn Ability>>,
|
|
}
|
|
|
|
impl AbilityLibraryImpl {
|
|
/// Instantiates a new ability library.
|
|
pub fn new(capacity: usize) -> Self {
|
|
Self {
|
|
map: IndexMap::with_capacity(capacity),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AbilityLibrary for AbilityLibraryImpl {}
|
|
|
|
impl DataLibrary<dyn Ability> for AbilityLibraryImpl {
|
|
fn map(&self) -> &IndexMap<StringKey, Arc<dyn Ability>> { &self.map }
|
|
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn Ability>> { &mut self.map }
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[allow(clippy::indexing_slicing)]
|
|
#[allow(clippy::unwrap_used)]
|
|
pub mod tests {
|
|
use crate::static_data::libraries::ability_library::AbilityLibraryImpl;
|
|
use crate::static_data::AbilityImpl;
|
|
use crate::static_data::DataLibrary;
|
|
use crate::StringKey;
|
|
use hashbrown::HashMap;
|
|
use std::sync::Arc;
|
|
|
|
pub fn build() -> AbilityLibraryImpl {
|
|
let lib = AbilityLibraryImpl::new(1);
|
|
lib.add(
|
|
&StringKey::new("test_ability"),
|
|
Arc::new(AbilityImpl::new(
|
|
&"test_ability".into(),
|
|
&"test_ability".into(),
|
|
HashMap::new(),
|
|
)),
|
|
);
|
|
lib
|
|
}
|
|
|
|
#[test]
|
|
fn get_ability_library_access() {
|
|
let lib = build();
|
|
let ability = lib.get(&"test_ability".into());
|
|
assert!(ability.is_some());
|
|
assert_eq!(ability.unwrap().name(), &"test_ability".into());
|
|
}
|
|
|
|
#[test]
|
|
fn get_ability_library_direct_map_access() {
|
|
let lib = build();
|
|
let ability = lib.get(&"test_ability".into());
|
|
assert!(ability.is_some());
|
|
assert_eq!(ability.unwrap().name(), &"test_ability".into());
|
|
}
|
|
}
|