Skip to content

Commit

Permalink
Simplification of ID traits down to HasId and MaybeHasId (#478)
Browse files Browse the repository at this point in the history
Co-authored-by: asquared31415 <[email protected]>
  • Loading branch information
shanemadden and asquared31415 authored Dec 31, 2023
1 parent 5ca1ac7 commit 3966607
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 227 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Unreleased
==========

### Breaking:

- Remove `HasNativeId`, `MaybeHasNativeId`, `HasTypedId`, and `MaybeHasTypedId` traits, adding
their functions to the `HasId` and `MaybeHasId` traits
- Renamed `native_id`/`try_native_id` to `js_raw_id`/`try_js_raw_id` for consistency with the
other trait functions
- Remove `Resolvable` trait, moving its functionality to `MaybeHasId`
- Remove `ObjectWithId` and `ObjectWithMaybeId` enums

0.19.0 (2023-12-20)
===================

Expand Down
85 changes: 1 addition & 84 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,89 +118,6 @@ pub enum CooldownObject {
StructureTerminal,
}

#[enum_dispatch(HasId)]
pub enum ObjectWithId {
Deposit,
Mineral,
Nuke,
Resource,
Ruin,
#[cfg(feature = "score")]
ScoreCollector,
#[cfg(feature = "score")]
ScoreContainer,
Source,
StructureContainer,
StructureController,
StructureExtension,
StructureExtractor,
StructureFactory,
StructureInvaderCore,
StructureKeeperLair,
StructureLab,
StructureLink,
StructureNuker,
StructureObserver,
StructurePortal,
StructurePowerBank,
StructurePowerSpawn,
StructureRampart,
StructureRoad,
StructureSpawn,
StructureStorage,
StructureTerminal,
StructureTower,
StructureWall,
#[cfg(feature = "symbols")]
SymbolContainer,
#[cfg(feature = "symbols")]
SymbolDecoder,
Tombstone,
}

#[enum_dispatch(MaybeHasId)]
pub enum ObjectWithMaybeId {
ConstructionSite,
Creep,
Deposit,
Mineral,
Nuke,
PowerCreep,
Resource,
Ruin,
#[cfg(feature = "score")]
ScoreCollector,
#[cfg(feature = "score")]
ScoreContainer,
Source,
StructureContainer,
StructureController,
StructureExtension,
StructureExtractor,
StructureFactory,
StructureInvaderCore,
StructureKeeperLair,
StructureLab,
StructureLink,
StructureNuker,
StructureObserver,
StructurePortal,
StructurePowerBank,
StructurePowerSpawn,
StructureRampart,
StructureRoad,
StructureSpawn,
StructureStorage,
StructureTerminal,
StructureTower,
StructureWall,
#[cfg(feature = "symbols")]
SymbolContainer,
#[cfg(feature = "symbols")]
SymbolDecoder,
Tombstone,
}

#[enum_dispatch(HasPosition)]
pub enum ObjectWithPosition {
ConstructionSite,
Expand Down Expand Up @@ -273,7 +190,7 @@ pub enum StoreObject {

/// Enum used for converting a [`Structure`] into a typed object of its specific
/// structure type.
#[enum_dispatch(OwnedStructureProperties, HasId)]
#[enum_dispatch(OwnedStructureProperties)]
pub enum OwnedStructureObject {
StructureController,
StructureExtension,
Expand Down
13 changes: 5 additions & 8 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
js_collections::{JsHashMap, JsObjectId},
local::{ObjectId, RawObjectId, RoomName},
objects::{AccountPowerCreep, ConstructionSite, Creep, Flag, Room, RoomObject, StructureSpawn},
traits::MaybeHasId,
};

pub mod cpu;
Expand Down Expand Up @@ -171,11 +172,9 @@ pub fn symbols() -> JsHashMap<crate::ResourceType, u32> {
/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
pub fn get_object_by_js_id_typed<T>(id: &JsObjectId<T>) -> Option<T>
where
T: From<JsValue>,
T: MaybeHasId + JsCast,
{
Game::get_object_by_id(&id.raw)
.map(JsValue::from)
.map(Into::into)
Game::get_object_by_id(&id.raw).map(JsCast::unchecked_into)
}

/// Get the typed object represented by a given [`ObjectId`], if it's still
Expand All @@ -184,14 +183,12 @@ where
/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
pub fn get_object_by_id_typed<T>(id: &ObjectId<T>) -> Option<T>
where
T: From<JsValue>,
T: MaybeHasId + JsCast,
{
// construct a reference to a javascript string using the id data
let js_str = JsString::from(id.to_string());

Game::get_object_by_id(&js_str)
.map(JsValue::from)
.map(Into::into)
Game::get_object_by_id(&js_str).map(JsCast::unchecked_into)
}

/// Get the [`RoomObject`] represented by a given [`RawObjectId`], if it's
Expand Down
2 changes: 1 addition & 1 deletion src/js_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<T> JsObjectId<T> {
/// don't have vision for.
pub fn resolve(&self) -> Option<T>
where
T: Resolvable,
T: MaybeHasId + JsCast,
{
game::get_object_by_js_id_typed(self)
}
Expand Down
13 changes: 5 additions & 8 deletions src/local/object_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use arrayvec::ArrayString;
use serde::{Deserialize, Serialize};
use wasm_bindgen::JsCast;

use crate::{game, Resolvable, RoomObject};
use crate::{game, objects::RoomObject, traits::MaybeHasId};

mod errors;
mod raw;
Expand Down Expand Up @@ -157,9 +157,8 @@ impl<T> ObjectId<T> {
self.raw.to_array_string()
}

/// Resolves this object ID into an object.
///
/// This is a shortcut for [`game::get_object_by_id_typed(id)`][1]
/// Resolves this object ID into an object, verifying that the returned
/// object matches the expected type.
///
/// # Errors
///
Expand All @@ -168,11 +167,9 @@ impl<T> ObjectId<T> {
///
/// Will return `Ok(None)` if the object no longer exists, or is in a room
/// we don't have vision for.
///
/// [1]: crate::game::get_object_by_id_typed
pub fn try_resolve(self) -> Result<Option<T>, RoomObject>
where
T: Resolvable + JsCast,
T: MaybeHasId + JsCast,
{
match game::get_object_by_id_erased(&self.raw) {
Some(v) => v.dyn_into().map(|v| Some(v)),
Expand All @@ -189,7 +186,7 @@ impl<T> ObjectId<T> {
/// don't have vision for.
pub fn resolve(self) -> Option<T>
where
T: Resolvable,
T: MaybeHasId + JsCast,
{
game::get_object_by_id_typed(&self)
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/construction_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ impl ConstructionSite {
}
}

impl MaybeHasNativeId for ConstructionSite {
impl MaybeHasId for ConstructionSite {
/// The Object ID of the [`ConstructionSite`], or `None` if it was created
/// this tick.
///
/// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.id)
fn try_native_id(&self) -> Option<JsString> {
fn try_js_raw_id(&self) -> Option<JsString> {
self.id_internal()
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/objects/impls/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,12 @@ impl HasHits for Creep {
}
}

impl MaybeHasNativeId for Creep {
fn try_native_id(&self) -> Option<JsString> {
impl MaybeHasId for Creep {
/// The Object ID of the [`Creep`], or `None` if it began spawning this
/// tick.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Creep.id)
fn try_js_raw_id(&self) -> Option<JsString> {
self.id_internal()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl HasCooldown for Deposit {
}
}

impl HasNativeId for Deposit {
fn native_id(&self) -> JsString {
impl HasId for Deposit {
fn js_raw_id(&self) -> JsString {
self.id_internal()
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/mineral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ extern "C" {
pub fn ticks_to_regeneration(this: &Mineral) -> Option<u32>;
}

impl HasNativeId for Mineral {
fn native_id(&self) -> JsString {
impl HasId for Mineral {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/nuke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ extern "C" {
pub fn time_to_land(this: &Nuke) -> u32;
}

impl HasNativeId for Nuke {
fn native_id(&self) -> JsString {
impl HasId for Nuke {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/power_creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ impl HasHits for PowerCreep {
}
}

impl HasNativeId for PowerCreep {
fn native_id(&self) -> JsString {
impl HasId for PowerCreep {
fn js_raw_id(&self) -> JsString {
self.id_internal()
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/objects/impls/reactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ extern "C" {
pub fn my(this: &Reactor) -> bool;

/// The [`Owner`] of this reactor that contains the owner's username, or
/// `None` if it's currently not under a player's
/// control.
/// `None` if it's currently not under a player's control.
///
/// [Screeps documentation](https://docs-season.screeps.com/api/#Reactor.owner)
#[wasm_bindgen(method, getter)]
pub fn owner(this: &Reactor) -> Option<Owner>;
}

impl HasNativeId for Reactor {
fn native_id(&self) -> JsString {
impl HasId for Reactor {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ extern "C" {
pub fn resource_type(this: &Resource) -> ResourceType;
}

impl HasNativeId for Resource {
fn native_id(&self) -> JsString {
impl HasId for Resource {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/ruin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ impl CanDecay for Ruin {
}
}

impl HasNativeId for Ruin {
fn native_id(&self) -> JsString {
impl HasId for Ruin {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/score_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ extern "C" {
pub fn store(this: &ScoreCollector) -> Store;
}

impl HasNativeId for ScoreCollector {
fn native_id(&self) -> JsString {
impl HasId for ScoreCollector {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/score_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl CanDecay for ScoreContainer {
}
}

impl HasNativeId for ScoreContainer {
fn native_id(&self) -> JsString {
impl HasId for ScoreContainer {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ extern "C" {
pub fn ticks_to_regeneration(this: &Source) -> Option<u32>;
}

impl HasNativeId for Source {
fn native_id(&self) -> JsString {
impl HasId for Source {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
6 changes: 3 additions & 3 deletions src/objects/impls/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ impl Structure {
}
}

impl<T> HasNativeId for T
impl<T> HasId for T
where
T: AsRef<Structure>,
T: AsRef<Structure> + JsCast,
{
fn native_id(&self) -> JsString {
fn js_raw_id(&self) -> JsString {
Structure::id_internal(self.as_ref())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/symbol_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl CanDecay for SymbolContainer {
}
}

impl HasNativeId for SymbolContainer {
fn native_id(&self) -> JsString {
impl HasId for SymbolContainer {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/symbol_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ extern "C" {
pub fn score_multiplier(this: &SymbolDecoder) -> u32;
}

impl HasNativeId for SymbolDecoder {
fn native_id(&self) -> JsString {
impl HasId for SymbolDecoder {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Loading

0 comments on commit 3966607

Please sign in to comment.