Skip to content

Commit

Permalink
Change creep_*_action internal macro syntax
Browse files Browse the repository at this point in the history
This should be in line with the change to simple_accesors!, and it
should bring these macros closer to understandable rust code. Still not
exactly rust code, but also not completely foreign and different.
  • Loading branch information
daboross committed Aug 21, 2019
1 parent 95c77ee commit 72a256b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
42 changes: 29 additions & 13 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,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 @@ -302,10 +304,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 @@ -323,10 +331,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 @@ -335,10 +345,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
34 changes: 19 additions & 15 deletions src/objects/impls/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,24 +304,28 @@ simple_accessors! {
}

creep_simple_generic_action! {
(attack(Attackable) -> attack),
(dismantle(StructureProperties) -> dismantle),
(ranged_attack(Attackable) -> rangedAttack),
(repair(StructureProperties) -> repair),
impl Creep {
pub fn attack(Attackable) = attack();
pub fn dismantle(StructureProperties) = dismantle();
pub fn ranged_attack(Attackable) = rangedAttack();
pub fn repair(StructureProperties) = repair();
}
}

creep_simple_concrete_action! {
(attack_controller(StructureController) -> attackController),
(build(ConstructionSite) -> build),
(claim_controller(StructureController) -> claimController),
(generate_safe_mode(StructureController) -> generateSafeMode),
(harvest(Source) -> harvest),
(heal(Creep) -> heal),
(pickup(Resource) -> pickup),
(pull(Creep) -> pull),
(ranged_heal(Creep) -> rangedHeal),
(reserve_controller(StructureController) -> reserveController),
(upgrade_controller(StructureController) -> upgradeController),
impl Creep {
pub fn attack_controller(StructureController) = attackController();
pub fn build(ConstructionSite) = build();
pub fn claim_controller(StructureController) = claimController();
pub fn generate_safe_mode(StructureController) = generateSafeMode();
pub fn harvest(Source) = harvest();
pub fn heal(Creep) = heal();
pub fn pickup(Resource) = pickup();
pub fn pull(Creep) = pull();
pub fn ranged_heal(Creep) = rangedHeal();
pub fn reserve_controller(StructureController) = reserveController();
pub fn upgrade_controller(StructureController) = upgradeController();
}
}

pub struct MoveToOptions<'a, F>
Expand Down

0 comments on commit 72a256b

Please sign in to comment.