mindustry logic execution, map- and schematic- parsing and rendering
Reimplement LogicField with numeric_enum
KosmosPrime 2023-01-20
parent d078cf6 · commit 7b319bd
-rw-r--r--src/data/dynamic.rs6
-rw-r--r--src/logic/mod.rs61
2 files changed, 13 insertions, 54 deletions
diff --git a/src/data/dynamic.rs b/src/data/dynamic.rs
index 7de97ff..4a8ccde 100644
--- a/src/data/dynamic.rs
+++ b/src/data/dynamic.rs
@@ -174,10 +174,10 @@ impl Serializer<DynData> for DynSerializer
13 =>
{
let id = buff.read_u8()?;
- match LogicField::of(id)
+ match LogicField::try_from(id)
{
- None => Err(ReadError::LogicField(id)),
- Some(f) => Ok(DynData::LogicField(f)),
+ Ok(f) => Ok(DynData::LogicField(f)),
+ Err(..) => Err(ReadError::LogicField(id)),
}
},
14 =>
diff --git a/src/logic/mod.rs b/src/logic/mod.rs
index 57d0d67..1ac1e3b 100644
--- a/src/logic/mod.rs
+++ b/src/logic/mod.rs
@@ -1,9 +1,14 @@
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
-pub enum LogicField
+use crate::content::numeric_enum;
+
+numeric_enum!
{
- TotalItems, FirstItem, TotalLiquids, TotalPower, ItemCapacity, LiquidCapacity, PowerCapacity, PowerNetCapacity, PowerNetStored, PowerNetIn, PowerNetOut,
- Ammo, AmmoCapacity, Health, MaxHealth, Heat, Efficiency, Progress, Timescale, Rotation, PosX, PosY, ShootX, ShootY, Size, Dead, Range, Shooting, Boosting,
- MineX, MineY, Mining, Speed, Team, Type, Flag, Controlled, Controller, Name, PayloadCount, PayloadType, Enabled, Shoot, ShootP, Config, Color
+ pub enum LogicField for u8 | TryFromU8Error
+ {
+ TotalItems, FirstItem, TotalLiquids, TotalPower, ItemCapacity, LiquidCapacity, PowerCapacity, PowerNetCapacity, PowerNetStored, PowerNetIn,
+ PowerNetOut, Ammo, AmmoCapacity, Health, MaxHealth, Heat, Efficiency, Progress, Timescale, Rotation, PosX, PosY, ShootX, ShootY, Size, Dead, Range,
+ Shooting, Boosting, MineX, MineY, Mining, Speed, Team, Type, Flag, Controlled, Controller, Name, PayloadCount, PayloadType, Enabled, Shoot, ShootP,
+ Config, Color
+ }
}
macro_rules!match_select
@@ -18,32 +23,8 @@ macro_rules!match_select
};
}
-macro_rules!map_from_enum
-{
- ($from:ident => $to:ty, $val:expr, $($name:ident),+) =>
- {
- {
- #![allow(dead_code, non_upper_case_globals)]
- $(const $name: $to = <$from>::$name as $to;)+
- match $val
- {
- $($name => Some(<$from>::$name),)+
- _ => None,
- }
- }
- };
-}
-
impl LogicField
{
- pub fn of(value: u8) -> Option<Self>
- {
- map_from_enum!(LogicField => u8, value, TotalItems, FirstItem, TotalLiquids, TotalPower, ItemCapacity, LiquidCapacity, PowerCapacity, PowerNetCapacity,
- PowerNetStored, PowerNetIn, PowerNetOut, Ammo, AmmoCapacity, Health, MaxHealth, Heat, Efficiency, Progress, Timescale, Rotation, PosX, PosY,
- ShootX, ShootY, Size, Dead, Range, Shooting, Boosting, MineX, MineY, Mining, Speed, Team, Type, Flag, Controlled, Controller, Name, PayloadCount,
- PayloadType, Enabled, Shoot, ShootP, Config, Color)
- }
-
pub fn is_readable(&self) -> bool
{
match_select!(self, LogicField, TotalItems, FirstItem, TotalLiquids, TotalPower, ItemCapacity, LiquidCapacity, PowerCapacity, PowerNetCapacity,
@@ -57,25 +38,3 @@ impl LogicField
match_select!(self, LogicField, Enabled, Shoot, ShootP, Config, Color)
}
}
-
-impl TryFrom<u8> for LogicField
-{
- type Error = u8;
-
- fn try_from(value: u8) -> Result<Self, Self::Error>
- {
- match Self::of(value)
- {
- None => Err(value),
- Some(f) => Ok(f),
- }
- }
-}
-
-impl From<LogicField> for u8
-{
- fn from(value: LogicField) -> u8
- {
- value as u8
- }
-}