Skip to content

Commit

Permalink
Merge pull request #235 from rustyscreeps/rustify-macro-invocation-sy…
Browse files Browse the repository at this point in the history
…ntax

Have internal macro syntax more closely resemble the rust code it produces
  • Loading branch information
daboross authored Aug 23, 2019
2 parents b2f11d2 + 83dd620 commit f36fad4
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 196 deletions.
78 changes: 39 additions & 39 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,32 +252,32 @@ pub mod find {
}

typesafe_find_constants! {
CREEPS, 101, Creep;
MY_CREEPS, 102, Creep;
HOSTILE_CREEPS, 103, Creep;
SOURCES_ACTIVE, 104, Source;
SOURCES, 105, Source;
DROPPED_RESOURCES, 106, Resource;
STRUCTURES, 107, Structure;
MY_STRUCTURES, 108, OwnedStructure;
HOSTILE_STRUCTURES, 109, OwnedStructure;
FLAGS, 110, Flag;
CONSTRUCTION_SITES, 111, ConstructionSite;
MY_SPAWNS, 112, StructureSpawn;
HOSTILE_SPAWNS, 113, StructureSpawn;
MY_CONSTRUCTION_SITES, 114, ConstructionSite;
HOSTILE_CONSTRUCTION_SITES, 115, ConstructionSite;
MINERALS, 116, Mineral;
NUKES, 117, Nuke;
TOMBSTONES, 118, Tombstone;
POWER_CREEPS, 119, PowerCreep;
MY_POWER_CREEPS, 120, PowerCreep;
HOSTILE_POWER_CREEPS, 121, PowerCreep;
EXIT_TOP, Exit::Top as i16, Position;
EXIT_RIGHT, Exit::Right as i16, Position;
EXIT_BOTTOM, Exit::Bottom as i16, Position;
EXIT_LEFT, Exit::Left as i16, Position;
EXIT, Exit::All as i16, Position;
pub struct CREEPS = (101, Creep);
pub struct MY_CREEPS = (102, Creep);
pub struct HOSTILE_CREEPS = (103, Creep);
pub struct SOURCES_ACTIVE = (104, Source);
pub struct SOURCES = (105, Source);
pub struct DROPPED_RESOURCES = (106, Resource);
pub struct STRUCTURES = (107, Structure);
pub struct MY_STRUCTURES = (108, OwnedStructure);
pub struct HOSTILE_STRUCTURES = (109, OwnedStructure);
pub struct FLAGS = (110, Flag);
pub struct CONSTRUCTION_SITES = (111, ConstructionSite);
pub struct MY_SPAWNS = (112, StructureSpawn);
pub struct HOSTILE_SPAWNS = (113, StructureSpawn);
pub struct MY_CONSTRUCTION_SITES = (114, ConstructionSite);
pub struct HOSTILE_CONSTRUCTION_SITES = (115, ConstructionSite);
pub struct MINERALS = (116, Mineral);
pub struct NUKES = (117, Nuke);
pub struct TOMBSTONES = (118, Tombstone);
pub struct POWER_CREEPS = (119, PowerCreep);
pub struct MY_POWER_CREEPS = (120, PowerCreep);
pub struct HOSTILE_POWER_CREEPS = (121, PowerCreep);
pub struct EXIT_TOP = (Exit::Top as i16, Position);
pub struct EXIT_RIGHT = (Exit::Right as i16, Position);
pub struct EXIT_BOTTOM = (Exit::Bottom as i16, Position);
pub struct EXIT_LEFT = (Exit::Left as i16, Position);
pub struct EXIT = (Exit::All as i16, Position);
}
}

Expand Down Expand Up @@ -529,19 +529,19 @@ pub mod look {
};

typesafe_look_constants! {
CREEPS, Look::Creeps, Creep, IntoExpectedType::into_expected_type;
ENERGY, Look::Energy, Resource, IntoExpectedType::into_expected_type;
RESOURCES, Look::Resources, Resource, IntoExpectedType::into_expected_type;
SOURCES, Look::Sources, Source, IntoExpectedType::into_expected_type;
MINERALS, Look::Minerals, Mineral, IntoExpectedType::into_expected_type;
STRUCTURES, Look::Structures, Structure, IntoExpectedType::into_expected_type;
FLAGS, Look::Flags, Flag, IntoExpectedType::into_expected_type;
CONSTRUCTION_SITES, Look::ConstructionSites, ConstructionSite,
IntoExpectedType::into_expected_type;
NUKES, Look::Nukes, Nuke, IntoExpectedType::into_expected_type;
TERRAIN, Look::Terrain, Terrain, TryInto::try_into;
TOMBSTONES, Look::Tombstones, Tombstone, IntoExpectedType::into_expected_type;
POWER_CREEPS, Look::PowerCreeps, PowerCreep, IntoExpectedType::into_expected_type;
pub struct CREEPS = (Look::Creeps, Creep, IntoExpectedType::into_expected_type);
pub struct ENERGY = (Look::Energy, Resource, IntoExpectedType::into_expected_type);
pub struct RESOURCES = (Look::Resources, Resource, IntoExpectedType::into_expected_type);
pub struct SOURCES = (Look::Sources, Source, IntoExpectedType::into_expected_type);
pub struct MINERALS = (Look::Minerals, Mineral, IntoExpectedType::into_expected_type);
pub struct STRUCTURES = (Look::Structures, Structure, IntoExpectedType::into_expected_type);
pub struct FLAGS = (Look::Flags, Flag, IntoExpectedType::into_expected_type);
pub struct CONSTRUCTION_SITES = (Look::ConstructionSites, ConstructionSite,
IntoExpectedType::into_expected_type);
pub struct NUKES = (Look::Nukes, Nuke, IntoExpectedType::into_expected_type);
pub struct TERRAIN = (Look::Terrain, Terrain, TryInto::try_into);
pub struct TOMBSTONES = (Look::Tombstones, Tombstone, IntoExpectedType::into_expected_type);
pub struct POWER_CREEPS = (Look::PowerCreeps, PowerCreep, IntoExpectedType::into_expected_type);
}
}

Expand Down
77 changes: 50 additions & 27 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ macro_rules! reference_wrappers {
(
$(
$(#[ $attr:meta ])*
$name:ident
),* $(,)*
$vis:vis struct $name:ident(...);
)+
) => {
$(
#[derive(Clone, ReferenceType)]
$(
#[$attr]
)*
pub struct $name(Reference);
$vis struct $name(Reference);

impl crate::traits::FromExpectedType<Reference> for $name {
fn from_expected_type(reference: Reference) -> Result<Self, ConversionError> {
Expand All @@ -156,18 +156,23 @@ macro_rules! reference_wrappers {
/// Method Syntax:
///
/// ```ignore
/// simple_accessor! {
/// $struct_name;
/// ($rust_method_name1 -> $js_field_name1 -> $rust_type1),
/// ($rust_method_name2 -> $js_field_name2 -> $rust_type2),
/// ...
/// simple_accessors! {
/// impl $struct_name {
/// pub fn $rust_method_name1() -> $rust_type1 = $js_field_name1;
/// pub fn $rust_method_name2() -> $rust_type2 = $js_field_name2;
/// ...
/// }
/// }
/// ```
macro_rules! simple_accessors {
($struct_name:ident; $(($method:ident -> $prop:ident -> $ret:ty)),* $(,)*) => (
(impl $struct_name:ident {
$(
$vis:vis fn $method:ident () -> $ret:ty = $prop:ident;
)+
}) => (
impl $struct_name {
$(
pub fn $method(&self) -> $ret {
$vis fn $method(&self) -> $ret {
js_unwrap!(@{self.as_ref()}.$prop)
}
)*
Expand Down Expand Up @@ -238,10 +243,12 @@ macro_rules! impl_has_id {
///
/// Macro Syntax:
/// ```ignore
/// creep_simple_generic_action!{
/// ($rust_method_name1($action_target_trait1) -> js_method_name1),
/// ($rust_method_name2($action_target_trait2) -> js_method_name2),
/// ...
/// creep_simple_generic_action! {
/// impl Creep {
/// pub fn $rust_method_name1($action_target_trait1) = js_method_name1();
/// pub fn $rust_method_name2($action_target_trait2) = js_method_name2();
/// ...
/// }
/// }
/// ```
///
Expand All @@ -250,10 +257,16 @@ macro_rules! impl_has_id {
/// The generic comes from the fact that this implements the method to be able
/// to target any object that conforms to the `action_target_trait` trait.
macro_rules! creep_simple_generic_action {
($(($method:ident($trait:ident) -> $js_name:ident)),* $(,)*) => (
impl Creep {
(
impl $struct_name:ident {
$(
$vis:vis fn $method:ident($trait:ident) = $js_name:ident ();
)+
}
) => (
impl $struct_name {
$(
pub fn $method<T>(&self, target: &T) -> ReturnCode
$vis fn $method<T>(&self, target: &T) -> ReturnCode
where
T: ?Sized + $trait,
{
Expand All @@ -271,10 +284,12 @@ macro_rules! creep_simple_generic_action {
///
/// Macro Syntax:
/// ```ignore
/// creep_simple_generic_action!{
/// ($rust_method_name1($target_type1) -> js_method_name1),
/// ($rust_method_name2($target_type2) -> js_method_name2),
/// ...
/// creep_simple_generic_action! {
/// impl Creep {
/// pub fn $rust_method_name1($target_type1) = js_method_name1();
/// pub fn $rust_method_name2($target_type2) = js_method_name2();
/// ...
/// }
/// }
/// ```
///
Expand All @@ -283,10 +298,16 @@ macro_rules! creep_simple_generic_action {
/// The concrete comes from the fact that this implements the method to be able
/// to target only the `type` given.
macro_rules! creep_simple_concrete_action {
($(($method:ident($type:ty) -> $js_name:ident)),* $(,)*) => (
(
impl $struct_name:ident {
$(
$vis:vis fn $method:ident($type:ty) = $js_name:ident ();
)+
}
) => (
impl Creep {
$(
pub fn $method(&self, target: &$type) -> ReturnCode {
$vis fn $method(&self, target: &$type) -> ReturnCode {
js_unwrap!(@{self.as_ref()}.$js_name(@{target.as_ref()}))
}
)*
Expand Down Expand Up @@ -314,7 +335,7 @@ macro_rules! calculated_doc {
macro_rules! typesafe_find_constants {
(
$(
$constant_name:ident, $value:expr, $result:path;
$vis:vis struct $constant_name:ident = ($value:expr, $result:path);
)*
) => (
$(
Expand All @@ -326,7 +347,7 @@ macro_rules! typesafe_find_constants {
)]
#[allow(bad_style)]
#[derive(Copy, Clone, Debug, Default)]
pub struct $constant_name;
$vis struct $constant_name;
}
unsafe impl FindConstant for $constant_name {
type Item = $result;
Expand All @@ -342,11 +363,13 @@ macro_rules! typesafe_find_constants {

macro_rules! typesafe_look_constants {
(
$($constant_name:ident, $value:expr, $result:path, $conversion_method:expr;)*
$(
$vis:vis struct $constant_name:ident = ($value:expr, $result:path, $conversion_method:expr);
)*
) => (
$(
#[allow(bad_style)]
pub struct $constant_name;
$vis struct $constant_name;
unsafe impl LookConstant for $constant_name {
type Item = $result;

Expand Down
72 changes: 36 additions & 36 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,77 +37,77 @@ pub use self::{
structure::Structure,
};

reference_wrappers!(
reference_wrappers! {
#[reference(instance_of = "ConstructionSite")]
ConstructionSite,
pub struct ConstructionSite(...);
#[reference(instance_of = "Creep")]
Creep,
pub struct Creep(...);
#[reference(instance_of = "Flag")]
Flag,
pub struct Flag(...);
#[reference(instance_of = "Mineral")]
Mineral,
pub struct Mineral(...);
#[reference(instance_of = "Nuke")]
Nuke,
pub struct Nuke(...);
#[reference(instance_of = "OwnedStructure")]
OwnedStructure,
pub struct OwnedStructure(...);
#[reference(instance_of = "Resource")]
Resource,
pub struct Resource(...);
#[reference(instance_of = "Room")]
Room,
pub struct Room(...);
#[reference(instance_of = "RoomObject")]
RoomObject,
pub struct RoomObject(...);
#[reference(instance_of = "Room.Terrain")]
RoomTerrain,
pub struct RoomTerrain(...);
#[reference(instance_of = "Source")]
Source,
pub struct Source(...);
#[reference(instance_of = "StructureContainer")]
StructureContainer,
pub struct StructureContainer(...);
#[reference(instance_of = "StructureController")]
StructureController,
pub struct StructureController(...);
#[reference(instance_of = "StructureExtension")]
StructureExtension,
pub struct StructureExtension(...);
#[reference(instance_of = "StructureExtractor")]
StructureExtractor,
pub struct StructureExtractor(...);
#[reference(instance_of = "StructureKeeperLair")]
StructureKeeperLair,
pub struct StructureKeeperLair(...);
#[reference(instance_of = "StructureLab")]
StructureLab,
pub struct StructureLab(...);
#[reference(instance_of = "StructureLink")]
StructureLink,
pub struct StructureLink(...);
#[reference(instance_of = "StructureNuker")]
StructureNuker,
pub struct StructureNuker(...);
#[reference(instance_of = "StructureObserver")]
StructureObserver,
pub struct StructureObserver(...);
#[reference(instance_of = "StructurePowerBank")]
StructurePowerBank,
pub struct StructurePowerBank(...);
#[reference(instance_of = "StructurePowerSpawn")]
StructurePowerSpawn,
pub struct StructurePowerSpawn(...);
#[reference(instance_of = "StructurePortal")]
StructurePortal,
pub struct StructurePortal(...);
#[reference(instance_of = "StructureRampart")]
StructureRampart,
pub struct StructureRampart(...);
#[reference(instance_of = "StructureRoad")]
StructureRoad,
pub struct StructureRoad(...);
#[reference(instance_of = "StructureSpawn")]
StructureSpawn,
pub struct StructureSpawn(...);
#[reference(instance_of = "Spawning")]
Spawning,
pub struct Spawning(...);
#[reference(instance_of = "StructureStorage")]
StructureStorage,
pub struct StructureStorage(...);
#[reference(instance_of = "StructureTerminal")]
StructureTerminal,
pub struct StructureTerminal(...);
#[reference(instance_of = "StructureTower")]
StructureTower,
pub struct StructureTower(...);
#[reference(instance_of = "StructureWall")]
StructureWall,
pub struct StructureWall(...);
// this is implemented later
// #[reference(instance_of = "Structure")]
// Structure,
// pub struct Structure,
#[reference(instance_of = "Tombstone")]
Tombstone,
pub struct Tombstone(...);
#[reference(instance_of = "PowerCreep")]
PowerCreep,
);
pub struct PowerCreep(...);
}

/// Trait for things which have positions in the Screeps world.
///
Expand Down
9 changes: 5 additions & 4 deletions src/objects/impls/construction_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use crate::{
};

simple_accessors! {
ConstructionSite;
(my -> my -> bool),
(progress -> progress -> u32),
(progress_total -> progressTotal -> u32),
impl ConstructionSite {
pub fn my() -> bool = my;
pub fn progress() -> u32 = progress;
pub fn progress_total() -> u32 = progressTotal;
}
}

impl ConstructionSite {
Expand Down
5 changes: 3 additions & 2 deletions src/objects/impls/container.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{macros::*, objects::StructureContainer};

simple_accessors! {
StructureContainer;
(ticks_to_decay -> ticksToDecay -> u32),
impl StructureContainer {
pub fn ticks_to_decay() -> u32 = ticksToDecay;
}
}
Loading

0 comments on commit f36fad4

Please sign in to comment.