Gen7ScriptsRs/pkmn_lib_interface/src/app_interface/static_data/ability.rs

88 lines
2.7 KiB
Rust
Executable File

use crate::StringKey;
use alloc::rc::Rc;
#[cfg_attr(test, mockall::automock)]
pub trait AbilityTrait {
fn name(&self) -> StringKey;
fn effect(&self) -> StringKey;
}
pub type Ability = Rc<dyn AbilityTrait>;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct AbilityIndex {
pub hidden: bool,
pub index: u8,
}
#[cfg(not(feature = "mock_data"))]
mod implementation {
use super::*;
use crate::app_interface::list::{
EffectParameterImmutableList, ImmutableList, ImmutableListWasm,
};
use crate::app_interface::{EffectParameter, StringKey};
use crate::handling::cached_value::CachedValue;
use crate::handling::extern_ref::{ExternRef, ExternalReferenceType, VecExternRef};
use crate::handling::Cacheable;
use crate::{cached_value, cached_value_getters};
use alloc::rc::Rc;
struct AbilityInner {
reference: ExternRef<AbilityImpl>,
name: CachedValue<StringKey>,
effect: CachedValue<StringKey>,
parameters: CachedValue<ImmutableList<Rc<EffectParameter>>>,
}
#[derive(Clone)]
pub struct AbilityImpl {
inner: Rc<AbilityInner>,
}
impl AbilityImpl {
#[cfg(not(feature = "mock_data"))]
pub fn new(reference: ExternRef<Self>) -> Self {
Self::from_ref(reference, &|reference| Self {
inner: Rc::new(AbilityInner {
reference,
name: cached_value!({ ability_get_name(reference).get_value().unwrap() }),
effect: cached_value!({ ability_get_effect(reference).get_value().unwrap() }),
parameters: cached_value!({
Rc::new(EffectParameterImmutableList::from_ref(
ability_get_parameters(reference),
))
}),
}),
})
}
}
impl AbilityTrait for AbilityImpl {
cached_value_getters! {
fn name(&self) -> StringKey;
fn effect(&self) -> StringKey;
}
}
#[cfg(not(feature = "mock_data"))]
impl ExternalReferenceType for AbilityImpl {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
Self::new(reference)
}
}
crate::handling::cacheable::cacheable!(AbilityImpl);
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn ability_get_name(r: ExternRef<AbilityImpl>) -> ExternRef<StringKey>;
fn ability_get_effect(r: ExternRef<AbilityImpl>) -> ExternRef<StringKey>;
fn ability_get_parameters(r: ExternRef<AbilityImpl>) -> VecExternRef<EffectParameter>;
}
}
#[cfg(not(feature = "mock_data"))]
pub use implementation::*;