diff --git a/CHANGELOG.md b/CHANGELOG.md index edb8fcac..30774372 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Unreleased a given `RoomXY` position - Add static function `LocalCostMatrix::new_with_value` which returns a `LocalCostMatrix` with every position set to a given `u8` value +- Implement `JsCollectionIntoValue` and `JsCollectionFromValue` for `IntershardResourceType` and + `u32` to allow `game::resources()` return value to be used as expected 0.21.0 (2024-05-14) =================== diff --git a/src/constants/types.rs b/src/constants/types.rs index 9782f2e7..934a5e7b 100644 --- a/src/constants/types.rs +++ b/src/constants/types.rs @@ -199,6 +199,18 @@ pub enum IntershardResourceType { named_enum_serialize_deserialize!(IntershardResourceType); +impl JsCollectionIntoValue for IntershardResourceType { + fn into_value(self) -> JsValue { + self.into() + } +} + +impl JsCollectionFromValue for IntershardResourceType { + fn from_value(v: JsValue) -> IntershardResourceType { + IntershardResourceType::from_js_value(&v).expect("valid intershard resource type string") + } +} + /// Translates the values of the `RESOURCES_ALL` constant, representing all /// possible in-game (non-intershard) resources. #[wasm_bindgen] diff --git a/src/js_collections.rs b/src/js_collections.rs index c1d5ecab..37c1a514 100644 --- a/src/js_collections.rs +++ b/src/js_collections.rs @@ -320,6 +320,22 @@ impl JsCollectionFromValue for u8 { } } +impl JsCollectionIntoValue for u32 { + fn into_value(self) -> JsValue { + JsValue::from_f64(self as f64) + } +} + +impl JsCollectionFromValue for u32 { + fn from_value(val: JsValue) -> u32 { + if let Some(val) = val.as_string() { + val.parse::().expect("expected parseable u32 string") + } else { + val.as_f64().expect("expected number value") as u32 + } + } +} + impl JsCollectionFromValue for (K, V) where K: JsCollectionFromValue,