From c24862dd4820805a9f45ea27f3f7e7cff6102650 Mon Sep 17 00:00:00 2001 From: GarandPLG Date: Fri, 24 Apr 2026 11:53:07 +0200 Subject: [PATCH] Extract helper methods for enum trait implementations --- .../structures/structures_enum.rs | 77 ++++++++----------- .../skirmish_states/units/units_enum.rs | 20 ++--- 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/src/app/states/skirmish_states/structures/structures_enum.rs b/src/app/states/skirmish_states/structures/structures_enum.rs index c4dc44b..8add3bc 100644 --- a/src/app/states/skirmish_states/structures/structures_enum.rs +++ b/src/app/states/skirmish_states/structures/structures_enum.rs @@ -8,52 +8,39 @@ pub enum Structures { Stone(Stone), } -impl Structure for Structures { - fn get_color(&self) -> Color { +impl Structures { + #[inline] + fn structure(&self) -> &dyn Structure { match self { - Self::Base(b) => b.get_color(), - Self::Tunnel(t) => t.get_color(), - Self::Stone(s) => s.get_color(), - } - } - - fn get_tag(&self) -> char { - match self { - Self::Base(b) => b.get_tag(), - Self::Tunnel(t) => t.get_tag(), - Self::Stone(s) => s.get_tag(), - } - } - - fn get_level(&self) -> char { - match self { - Self::Base(b) => b.get_level(), - Self::Tunnel(t) => t.get_level(), - Self::Stone(s) => s.get_level(), - } - } - - fn get_name(&self) -> &'static str { - match self { - Self::Base(b) => b.get_name(), - Self::Tunnel(t) => t.get_name(), - Self::Stone(s) => s.get_name(), - } - } - - fn get_durability(&self) -> u16 { - match self { - Self::Base(b) => b.get_durability(), - Self::Tunnel(t) => t.get_durability(), - Self::Stone(s) => s.get_durability(), - } - } - - fn get_stress(&self) -> u8 { - match self { - Self::Base(b) => b.get_stress(), - Self::Tunnel(t) => t.get_stress(), - Self::Stone(s) => s.get_stress(), + Self::Base(b) => b, + Self::Tunnel(t) => t, + Self::Stone(s) => s, } } } + +impl Structure for Structures { + fn get_color(&self) -> Color { + self.structure().get_color() + } + + fn get_tag(&self) -> char { + self.structure().get_tag() + } + + fn get_level(&self) -> char { + self.structure().get_level() + } + + fn get_name(&self) -> &'static str { + self.structure().get_name() + } + + fn get_durability(&self) -> u16 { + self.structure().get_durability() + } + + fn get_stress(&self) -> u8 { + self.structure().get_stress() + } +} diff --git a/src/app/states/skirmish_states/units/units_enum.rs b/src/app/states/skirmish_states/units/units_enum.rs index 4fef7f1..4a3036d 100644 --- a/src/app/states/skirmish_states/units/units_enum.rs +++ b/src/app/states/skirmish_states/units/units_enum.rs @@ -8,23 +8,25 @@ pub enum Units { Miner(MinerUnit), } +impl Units { + fn unit(&self) -> &dyn Unit { + match self { + Self::Miner(m) => m, + } + } +} + impl Unit for Units { fn get_tag(&self) -> char { - match self { - Self::Miner(m) => m.get_tag(), - } + self.unit().get_tag() } fn get_name(&self) -> &'static str { - match self { - Self::Miner(m) => m.get_name(), - } + self.unit().get_name() } fn get_owner(&self) -> Players { - match self { - Self::Miner(m) => m.get_owner(), - } + self.unit().get_owner() } }