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::MoveData;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// A library to store all data for moves.
#[derive(Debug)]
pub trait MoveLibrary: DataLibrary<dyn MoveData> + ValueIdentifiable + Debug {}
pub struct MoveLibrary {
/// A library to store all data for moves.
#[derive(Debug)]
pub struct MoveLibraryImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying map.
map: IndexMap<StringKey, Arc<dyn MoveData>>,
}
impl MoveLibrary {
impl MoveLibraryImpl {
/// Instantiates a new Move Library.
pub fn new(capacity: usize) -> MoveLibrary {
MoveLibrary {
pub fn new(capacity: usize) -> Self {
Self {
identifier: Default::default(),
map: IndexMap::with_capacity(capacity),
}
}
}
impl DataLibrary<dyn MoveData> for MoveLibrary {
impl MoveLibrary for MoveLibraryImpl {}
impl DataLibrary<dyn MoveData> for MoveLibraryImpl {
fn map(&self) -> &IndexMap<StringKey, Arc<dyn MoveData>> {
&self.map
}
@@ -35,7 +40,7 @@ impl DataLibrary<dyn MoveData> for MoveLibrary {
}
}
impl ValueIdentifiable for MoveLibrary {
impl ValueIdentifiable for MoveLibraryImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
@@ -48,7 +53,7 @@ pub mod tests {
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::move_library::MoveLibrary;
use crate::static_data::{MoveCategory, MoveDataImpl, MoveTarget};
use crate::static_data::{MoveCategory, MoveDataImpl, MoveLibraryImpl, MoveTarget};
use crate::StringKey;
fn build_move() -> MoveDataImpl {
@@ -66,14 +71,14 @@ pub mod tests {
)
}
pub fn build() -> MoveLibrary {
let mut lib = MoveLibrary::new(1);
pub fn build() -> Box<dyn MoveLibrary> {
let mut lib = MoveLibraryImpl::new(1);
let m = build_move();
// Borrow as mut so we can insert
let w = &mut lib;
w.add(&StringKey::new("foo"), Arc::new(m));
// Drops borrow as mut
lib
Box::new(lib)
}
}