85 lines
2.1 KiB
Rust
Executable File
85 lines
2.1 KiB
Rust
Executable File
use std::fmt::Debug;
|
|
use std::sync::Arc;
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use crate::static_data::DataLibrary;
|
|
use crate::static_data::MoveData;
|
|
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
|
|
|
/// A library to store all data for moves.
|
|
pub trait MoveLibrary: DataLibrary<dyn MoveData> + ValueIdentifiable + Debug {}
|
|
|
|
/// 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 MoveLibraryImpl {
|
|
/// Instantiates a new Move Library.
|
|
pub fn new(capacity: usize) -> Self {
|
|
Self {
|
|
identifier: Default::default(),
|
|
map: IndexMap::with_capacity(capacity),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl MoveLibrary for MoveLibraryImpl {}
|
|
|
|
impl DataLibrary<dyn MoveData> for MoveLibraryImpl {
|
|
fn map(&self) -> &IndexMap<StringKey, Arc<dyn MoveData>> {
|
|
&self.map
|
|
}
|
|
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn MoveData>> {
|
|
&mut self.map
|
|
}
|
|
}
|
|
|
|
impl ValueIdentifiable for MoveLibraryImpl {
|
|
fn value_identifier(&self) -> ValueIdentifier {
|
|
self.identifier
|
|
}
|
|
}
|
|
|
|
#[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::move_library::MoveLibrary;
|
|
use crate::static_data::{MoveCategory, MoveDataImpl, MoveLibraryImpl, MoveTarget};
|
|
use crate::StringKey;
|
|
|
|
fn build_move() -> MoveDataImpl {
|
|
MoveDataImpl::new(
|
|
&"foo".into(),
|
|
0.into(),
|
|
MoveCategory::Physical,
|
|
100,
|
|
100,
|
|
30,
|
|
MoveTarget::Any,
|
|
0,
|
|
None,
|
|
HashSet::new(),
|
|
)
|
|
}
|
|
|
|
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
|
|
|
|
Box::new(lib)
|
|
}
|
|
}
|