-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod.rs
94 lines (84 loc) · 2.96 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//!
//! Just as with the registers, module identifier constants have either the unit name in them or
//! are shared between all units at a common register id. There should not be any naming conflicts
//! when importing with a wildcard.
pub mod artillery;
pub mod base_tricycle;
pub mod constructor;
pub mod tank;
/// Holds module information that's shared between units.
pub mod common {
/// Module identifier for the clock module.
pub const MODULE_CLOCK: u32 = 0x0100;
/// Module identifier for the controller module.
pub const MODULE_CONTROLLER: u32 = 0x0200;
/// Module identifier for the objectives module.
pub const MODULE_OBJECTIVES: u32 = 0x0300;
/// Module identifier for the unit's team module.
pub const MODULE_TEAM: u32 = 0x0400;
/// Module identifier for the unit's unit module.
pub const MODULE_UNIT: u32 = 0x0500;
/// Module identifier for the unit's radio transmitter module.
pub const MODULE_RADIO_TRANSMITTER: u32 = 0x0600;
/// Module identifier for the unit's radio receiver module.
pub const MODULE_RADIO_RECEIVER: u32 = 0x0700;
/// Module identifier for the unit's gps module.
pub const MODULE_GPS: u32 = 0x1700;
/// Module identifier for the unit's draw module.
pub const MODULE_DRAW: u32 = 0x1800;
/// Module identifier for the unit's deploy module, if it has one.
pub const MODULE_DEPLOY: u32 = 0x1900;
}
/// Unit type enum to denote the unit type.
///
/// This implements [`UnitType::try_from`] to convert from u32 to the enum.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum UnitType {
Artillery,
Constructor,
Tank,
Unknown,
}
impl TryFrom<u32> for UnitType {
type Error = &'static str;
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == UnitType::Artillery as u32 => Ok(UnitType::Artillery),
x if x == UnitType::Constructor as u32 => Ok(UnitType::Constructor),
x if x == UnitType::Tank as u32 => Ok(UnitType::Tank),
x if x == UnitType::Unknown as u32 => Ok(UnitType::Unknown),
_ => Err("could not convert unit_type"),
}
}
}
/// Finally, it implements display.
impl std::fmt::Display for UnitType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
UnitType::Artillery => {
write!(f, "artillery")
}
UnitType::Constructor => {
write!(f, "constructor")
}
UnitType::Tank => {
write!(f, "tank")
}
UnitType::Unknown => {
write!(f, "unknown")
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_unit_type_conversion() {
assert_eq!(UnitType::Tank, (UnitType::Tank as u32).try_into().unwrap());
assert_eq!(
UnitType::Artillery,
(UnitType::Artillery as u32).try_into().unwrap()
);
}
}