Skip to content

Commit

Permalink
Implement JsCollection traits for IntershardResourceType (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden authored Jun 25, 2024
1 parent bb74f6d commit 12aed92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
===================
Expand Down
12 changes: 12 additions & 0 deletions src/constants/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 16 additions & 0 deletions src/js_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>().expect("expected parseable u32 string")
} else {
val.as_f64().expect("expected number value") as u32
}
}
}

impl<K, V> JsCollectionFromValue for (K, V)
where
K: JsCollectionFromValue,
Expand Down

0 comments on commit 12aed92

Please sign in to comment.