mindustry logic execution, map- and schematic- parsing and rendering
Reimplement UnitCommand with numeric_enum
KosmosPrime 2023-01-20
parent a204308 · commit f35603e
-rw-r--r--src/data/command.rs9
-rw-r--r--src/data/dynamic.rs51
-rw-r--r--src/data/mod.rs1
3 files changed, 15 insertions, 46 deletions
diff --git a/src/data/command.rs b/src/data/command.rs
new file mode 100644
index 0000000..66d9822
--- /dev/null
+++ b/src/data/command.rs
@@ -0,0 +1,9 @@
+use crate::content::numeric_enum;
+
+numeric_enum!
+{
+ pub enum UnitCommand for u8 | TryFromU8Error
+ {
+ Attack, Rally, Idle
+ }
+}
diff --git a/src/data/dynamic.rs b/src/data/dynamic.rs
index f87c1ac..05bd02e 100644
--- a/src/data/dynamic.rs
+++ b/src/data/dynamic.rs
@@ -2,51 +2,10 @@ use std::error::Error;
use std::fmt;
use crate::data::{self, DataRead, DataWrite, GridPos, Serializer};
+use crate::data::command::{self, UnitCommand};
use crate::logic::LogicField;
use crate::team::Team;
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
-pub enum UnitCommand
-{
- Attack, Rally, Idle
-}
-
-impl UnitCommand
-{
- pub fn of(id: u8) -> Option<Self>
- {
- match id
- {
- 0 => Some(Self::Attack),
- 1 => Some(Self::Rally),
- 2 => Some(Self::Idle),
- _ => None
- }
- }
-}
-
-impl TryFrom<u8> for UnitCommand
-{
- type Error = u8;
-
- fn try_from(value: u8) -> Result<Self, Self::Error>
- {
- match Self::of(value)
- {
- None => Err(value),
- Some(c) => Ok(c),
- }
- }
-}
-
-impl From<UnitCommand> for u8
-{
- fn from(value: UnitCommand) -> u8
- {
- value as u8
- }
-}
-
#[derive(Clone, Debug, PartialEq)]
pub enum DynData
{
@@ -194,10 +153,10 @@ impl Serializer<DynData> for DynSerializer
15 =>
{
let id = buff.read_u8()?;
- match UnitCommand::of(id)
+ match UnitCommand::try_from(id)
{
- None => Err(ReadError::UnitCommand(id)),
- Some(f) => Ok(DynData::UnitCommand(f)),
+ Ok(f) => Ok(DynData::UnitCommand(f)),
+ Err(e) => Err(ReadError::UnitCommand(e)),
}
},
16 =>
@@ -429,7 +388,7 @@ pub enum ReadError
Point2ArrayLen(i8),
LogicField(u8),
ByteArrayLen(i32),
- UnitCommand(u8),
+ UnitCommand(command::TryFromU8Error),
BoolArrayLen(i32),
Vec2ArrayLen(i16),
}
diff --git a/src/data/mod.rs b/src/data/mod.rs
index cdf1b30..845890b 100644
--- a/src/data/mod.rs
+++ b/src/data/mod.rs
@@ -3,6 +3,7 @@ use std::fmt;
use std::str::Utf8Error;
pub mod base64;
+pub mod command;
pub mod dynamic;
pub mod schematic;