Gen7ScriptsRs/pkmn_lib_interface/src/app_interface/dynamic_data/battle_party.rs

68 lines
1.9 KiB
Rust
Executable File

use crate::app_interface::Party;
use alloc::rc::Rc;
#[cfg_attr(feature = "mock_data", mockall::automock)]
pub trait BattlePartyTrait {
fn party(&self) -> Party;
}
pub type BattleParty = Rc<dyn BattlePartyTrait>;
#[cfg(feature = "mock_data")]
pub type MockBattleParty = MockBattlePartyTrait;
#[cfg(not(feature = "mock_data"))]
mod implementation {
use crate::app_interface::{BattlePartyTrait, Party, PartyImpl};
use crate::handling::cached_value::CachedValue;
use crate::handling::extern_ref::{ExternRef, ExternalReferenceType};
use crate::handling::Cacheable;
use crate::{cached_value, cached_value_getters};
use alloc::rc::Rc;
struct BattlePartyInner {
reference: ExternRef<BattlePartyImpl>,
party: CachedValue<Rc<PartyImpl>>,
}
#[derive(Clone)]
pub struct BattlePartyImpl {
inner: Rc<BattlePartyInner>,
}
impl BattlePartyImpl {
pub fn new(reference: ExternRef<BattlePartyImpl>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(BattlePartyInner {
reference,
party: cached_value!({
Rc::new(battle_party_get_party(reference).get_value().unwrap())
}),
}),
})
}
}
impl BattlePartyTrait for BattlePartyImpl {
cached_value_getters! {
fn party(&self) -> Party;
}
}
crate::handling::cacheable::cacheable!(BattlePartyImpl);
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for BattlePartyImpl {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn battle_party_get_party(r: ExternRef<BattlePartyImpl>) -> ExternRef<PartyImpl>;
}
}
#[cfg(not(feature = "mock_data"))]
pub use implementation::*;