112 lines
3.4 KiB
Rust
Executable File
112 lines
3.4 KiB
Rust
Executable File
use crate::app_interface::StringKey;
|
|
#[cfg(not(feature = "mock_data"))]
|
|
use crate::{handling::WasmResult, ExternRef, ExternalReferenceType, PkmnResult};
|
|
use core::fmt::{Display, Formatter};
|
|
|
|
#[repr(u8)]
|
|
#[derive(Copy, Clone)]
|
|
enum EffectParameterType {
|
|
None,
|
|
Bool,
|
|
Int,
|
|
Float,
|
|
String,
|
|
}
|
|
|
|
impl Default for EffectParameterType {
|
|
fn default() -> Self {
|
|
Self::None
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub enum EffectParameter {
|
|
None,
|
|
Bool(bool),
|
|
Int(i64),
|
|
Float(f32),
|
|
String(StringKey),
|
|
}
|
|
|
|
impl EffectParameter {
|
|
#[cfg(not(feature = "mock_data"))]
|
|
pub(crate) fn new(ptr: ExternRef<Self>) -> PkmnResult<Self> {
|
|
Ok(unsafe {
|
|
match effect_parameter_get_type(ptr).as_res()? {
|
|
EffectParameterType::None => Self::None,
|
|
EffectParameterType::Bool => Self::Bool(effect_parameter_as_bool(ptr).as_res()?),
|
|
EffectParameterType::Int => Self::Int(effect_parameter_as_int(ptr).as_res()?),
|
|
EffectParameterType::Float => Self::Float(effect_parameter_as_float(ptr).as_res()?),
|
|
EffectParameterType::String => {
|
|
Self::String(StringKey::new(effect_parameter_as_string(ptr).as_res()?))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
pub fn as_bool(&self) -> bool {
|
|
if let EffectParameter::Bool(b) = self {
|
|
return *b;
|
|
}
|
|
panic!("Unexpected effect parameter type: {self}");
|
|
}
|
|
|
|
pub fn as_int(&self) -> i64 {
|
|
if let EffectParameter::Int(i) = self {
|
|
return *i;
|
|
}
|
|
panic!("Unexpected effect parameter type: {self}");
|
|
}
|
|
|
|
pub fn as_float(&self) -> f32 {
|
|
if let EffectParameter::Float(f) = self {
|
|
return *f;
|
|
}
|
|
panic!("Unexpected effect parameter type: {self}");
|
|
}
|
|
|
|
pub fn as_string(&self) -> StringKey {
|
|
if let EffectParameter::String(s) = self {
|
|
return s.clone();
|
|
}
|
|
panic!("Unexpected effect parameter type: {self}");
|
|
}
|
|
}
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
impl ExternalReferenceType for EffectParameter {
|
|
fn from_extern_value(reference: ExternRef<Self>) -> Self
|
|
where
|
|
Self: Sized,
|
|
{
|
|
EffectParameter::new(reference).unwrap()
|
|
}
|
|
}
|
|
|
|
impl Display for EffectParameter {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
|
match self {
|
|
EffectParameter::None => f.write_str("EffectParameter::None"),
|
|
EffectParameter::Bool(b) => f.write_fmt(format_args!("EffectParameter::Bool({b})")),
|
|
EffectParameter::Int(i) => f.write_fmt(format_args!("EffectParameter::Int({i})")),
|
|
EffectParameter::Float(r) => f.write_fmt(format_args!("EffectParameter::Float({r})")),
|
|
EffectParameter::String(s) => {
|
|
f.write_fmt(format_args!("EffectParameter::String(\"{s}\")"))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
extern "wasm" {
|
|
fn effect_parameter_get_type(
|
|
ptr: ExternRef<EffectParameter>,
|
|
) -> WasmResult<EffectParameterType>;
|
|
fn effect_parameter_as_bool(ptr: ExternRef<EffectParameter>) -> WasmResult<bool>;
|
|
fn effect_parameter_as_int(ptr: ExternRef<EffectParameter>) -> WasmResult<i64>;
|
|
fn effect_parameter_as_float(ptr: ExternRef<EffectParameter>) -> WasmResult<f32>;
|
|
fn effect_parameter_as_string(
|
|
ptr: ExternRef<EffectParameter>,
|
|
) -> WasmResult<ExternRef<StringKey>>;
|
|
}
|