Moves a bunch of libraries to traits
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-12-24 12:00:50 +01:00
parent bce636b97e
commit 47df85e8d3
47 changed files with 730 additions and 358 deletions

View File

@@ -1,3 +1,4 @@
use std::fmt::Debug;
use std::sync::Arc;
use indexmap::IndexMap;
@@ -7,26 +8,30 @@ use crate::static_data::Item;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A library to store all items.
#[derive(Debug)]
pub trait ItemLibrary: DataLibrary<dyn Item> + ValueIdentifiable + Debug {}
pub struct ItemLibrary {
/// A library to store all items.
#[derive(Debug)]
pub struct ItemLibraryImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying data structure.
map: IndexMap<StringKey, Arc<dyn Item>>,
}
impl ItemLibrary {
impl ItemLibraryImpl {
/// Instantiates a new Item Library.
pub fn new(capacity: usize) -> ItemLibrary {
ItemLibrary {
pub fn new(capacity: usize) -> Self {
Self {
identifier: Default::default(),
map: IndexMap::with_capacity(capacity),
}
}
}
impl DataLibrary<dyn Item> for ItemLibrary {
impl ItemLibrary for ItemLibraryImpl {}
impl DataLibrary<dyn Item> for ItemLibraryImpl {
fn map(&self) -> &IndexMap<StringKey, Arc<dyn Item>> {
&self.map
}
@@ -36,7 +41,7 @@ impl DataLibrary<dyn Item> for ItemLibrary {
}
}
impl ValueIdentifiable for ItemLibrary {
impl ValueIdentifiable for ItemLibraryImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
@@ -49,8 +54,8 @@ pub mod tests {
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::item_library::ItemLibrary;
use crate::static_data::ItemImpl;
use crate::static_data::{BattleItemCategory, ItemCategory};
use crate::static_data::{ItemImpl, ItemLibraryImpl};
fn build_item() -> ItemImpl {
ItemImpl::new(
@@ -62,14 +67,14 @@ pub mod tests {
)
}
pub fn build() -> ItemLibrary {
let mut lib = ItemLibrary::new(1);
pub fn build() -> Box<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
lib
Box::new(lib)
}
}