-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathlib.rs
More file actions
235 lines (206 loc) · 6.4 KB
/
Copy pathlib.rs
File metadata and controls
235 lines (206 loc) · 6.4 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::{
fmt::{self, Display, Formatter},
net::Ipv4Addr,
str::FromStr,
};
use color_eyre::{
Report, Result,
eyre::{WrapErr, bail, eyre},
};
use regex::Regex;
use repository::PlayerNumber;
use robot::{Network, SystemctlAction};
pub const SYSTEMCTL_ACTION_POSSIBLE_VALUES: &[&str] =
&["disable", "enable", "restart", "start", "status", "stop"];
pub fn parse_systemctl_action(systemctl_action: &str) -> Result<SystemctlAction> {
match systemctl_action {
"disable" => Ok(SystemctlAction::Disable),
"enable" => Ok(SystemctlAction::Enable),
"restart" => Ok(SystemctlAction::Restart),
"start" => Ok(SystemctlAction::Start),
"status" => Ok(SystemctlAction::Status),
"stop" => Ok(SystemctlAction::Stop),
_ => {
bail!("unexpected systemctl action");
}
}
}
pub const NETWORK_POSSIBLE_VALUES: &[&str] = &[
"None",
"HSL_A",
"HSL_B",
"HSL_C",
"HSL_D",
"HSL_E",
"HSL_F",
"HSL_G",
"HSL_H",
"HSL_I",
"HSL_J",
"HSL_HULKs",
];
pub fn parse_network(network: &str) -> Result<Network> {
match network {
"None" => Ok(Network::None),
"HSL_A" => Ok(Network::HslA),
"HSL_B" => Ok(Network::HslB),
"HSL_C" => Ok(Network::HslC),
"HSL_D" => Ok(Network::HslD),
"HSL_E" => Ok(Network::HslE),
"HSL_F" => Ok(Network::HslF),
"HSL_G" => Ok(Network::HslG),
"HSL_H" => Ok(Network::HslH),
"HSL_I" => Ok(Network::HslI),
"HSL_J" => Ok(Network::HslJ),
"HSL_HULKs" => Ok(Network::HslHulks),
_ => {
bail!("unexpected network");
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct RobotAddress {
pub ip: Ipv4Addr,
}
impl FromStr for RobotAddress {
type Err = Report;
fn from_str(input: &str) -> Result<Self> {
let expression = Regex::new(r"^(\d+)(w?)$").unwrap();
match expression.captures(input) {
Some(captures) => {
let number = captures
.get(1)
.unwrap()
.as_str()
.parse()
.wrap_err("failed to parse RobotAddress")?;
let connection = if captures.get(2).unwrap().as_str() == "w" {
Connection::Wireless
} else {
Connection::Wired
};
let ip =
number_to_ip(number, connection).wrap_err("cannot parse from Robot number")?;
Ok(Self { ip })
}
None => Ok(Self {
ip: input.parse().wrap_err("failed to parse RobotAddress")?,
}),
}
}
}
impl Display for RobotAddress {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
self.ip.fmt(formatter)
}
}
#[derive(Debug)]
pub enum Connection {
Wireless,
Wired,
}
pub fn number_to_ip(robot_number: u8, connection: Connection) -> Result<Ipv4Addr> {
if robot_number == 0 || robot_number > 254 {
bail!("Robot number is either the network (0) or broadcast (255) which is not supported");
}
let subnet = match connection {
Connection::Wireless => 0,
Connection::Wired => 1,
};
Ok(Ipv4Addr::new(10, subnet, 24, robot_number))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct RobotNumber {
pub number: u8,
}
impl FromStr for RobotNumber {
type Err = Report;
fn from_str(input: &str) -> Result<Self> {
Ok(Self {
number: input.parse().wrap_err("failed to parse RobotNumber")?,
})
}
}
impl Display for RobotNumber {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
self.number.fmt(formatter)
}
}
impl TryFrom<RobotAddress> for RobotNumber {
type Error = Report;
fn try_from(robot_address: RobotAddress) -> Result<Self> {
if robot_address.ip.octets()[0] != 10
|| (robot_address.ip.octets()[1] != 0 && robot_address.ip.octets()[1] != 1)
|| robot_address.ip.octets()[2] != 24
{
bail!("failed to extract Robot number from IP {robot_address}");
}
Ok(Self {
number: robot_address.ip.octets()[3],
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct RobotAddressPlayerAssignment {
pub robot_address: RobotAddress,
pub player_number: PlayerNumber,
}
impl FromStr for RobotAddressPlayerAssignment {
type Err = Report;
fn from_str(input: &str) -> Result<Self> {
let (prefix, player_number) = parse_assignment(input)
.wrap_err_with(|| format!("failed to parse assignment {input}"))?;
Ok(Self {
robot_address: prefix
.parse()
.wrap_err_with(|| format!("failed to parse robot address {prefix}"))?,
player_number,
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct RobotNumberPlayerAssignment {
pub robot_number: RobotNumber,
pub player_number: PlayerNumber,
}
impl FromStr for RobotNumberPlayerAssignment {
type Err = Report;
fn from_str(input: &str) -> Result<Self> {
let (prefix, player_number) = parse_assignment(input)?;
Ok(Self {
robot_number: prefix.parse()?,
player_number,
})
}
}
impl Display for RobotNumberPlayerAssignment {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
write!(formatter, "{}:{}", self.robot_number, self.player_number)
}
}
fn parse_assignment(input: &str) -> Result<(&str, PlayerNumber)> {
let (prefix, player_number) = input.rsplit_once(':').ok_or_else(|| eyre!("missing `:`"))?;
let player_number = match player_number {
"1" => PlayerNumber::One,
"2" => PlayerNumber::Two,
"3" => PlayerNumber::Three,
"4" => PlayerNumber::Four,
"5" => PlayerNumber::Five,
_ => {
bail!("unexpected player number {player_number}");
}
};
Ok((prefix, player_number))
}
impl TryFrom<RobotAddressPlayerAssignment> for RobotNumberPlayerAssignment {
type Error = Report;
fn try_from(assignment: RobotAddressPlayerAssignment) -> Result<Self> {
Ok(Self {
robot_number: assignment
.robot_address
.try_into()
.wrap_err("failed to convert Robot address into Robot number")?,
player_number: assignment.player_number,
})
}
}