author | alfadur |
Tue, 09 Apr 2019 00:06:53 +0300 | |
changeset 14782 | 8015a6e4ca3c |
parent 14780 | 09d46ab83361 |
child 14788 | b3adc030104b |
permissions | -rw-r--r-- |
13436
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13428
diff
changeset
|
1 |
/** The parsers for the chat and multiplayer protocol. The main parser is `message`. |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13428
diff
changeset
|
2 |
* # Protocol |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13428
diff
changeset
|
3 |
* All messages consist of `\n`-separated strings. The end of a message is |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13428
diff
changeset
|
4 |
* indicated by a double newline - `\n\n`. |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13428
diff
changeset
|
5 |
* |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13428
diff
changeset
|
6 |
* For example, a nullary command like PING will be actually sent as `PING\n\n`. |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13428
diff
changeset
|
7 |
* A unary command, such as `START_GAME nick` will be actually sent as `START_GAME\nnick\n\n`. |
6a818f9192f4
Implement parsing for rnd and add a little documentation
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13428
diff
changeset
|
8 |
*/ |
12138 | 9 |
use nom::*; |
14780 | 10 |
use std::{ |
11 |
num::ParseIntError, |
|
12 |
ops::Range, |
|
13 |
str, |
|
14 |
str::{FromStr, Utf8Error}, |
|
15 |
}; |
|
12138 | 16 |
|
14462 | 17 |
use super::messages::{HWProtocolMessage, HWProtocolMessage::*}; |
18 |
use crate::server::coretypes::{GameCfg, HedgehogInfo, TeamInfo, VoteType, MAX_HEDGEHOGS_PER_TEAM}; |
|
14780 | 19 |
|
20 |
#[derive(Debug, PartialEq)] |
|
21 |
pub struct HWProtocolError {} |
|
22 |
||
23 |
impl HWProtocolError { |
|
24 |
fn new() -> Self { |
|
25 |
HWProtocolError {} |
|
26 |
} |
|
27 |
} |
|
28 |
||
29 |
impl<I> ParseError<I> for HWProtocolError { |
|
30 |
fn from_error_kind(input: I, kind: ErrorKind) -> Self { |
|
31 |
HWProtocolError::new() |
|
32 |
} |
|
33 |
||
34 |
fn append(input: I, kind: ErrorKind, other: Self) -> Self { |
|
35 |
HWProtocolError::new() |
|
36 |
} |
|
37 |
} |
|
38 |
||
39 |
impl From<Utf8Error> for HWProtocolError { |
|
40 |
fn from(_: Utf8Error) -> Self { |
|
41 |
HWProtocolError::new() |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
impl From<ParseIntError> for HWProtocolError { |
|
46 |
fn from(_: ParseIntError) -> Self { |
|
47 |
HWProtocolError::new() |
|
48 |
} |
|
49 |
} |
|
50 |
||
51 |
pub type HWResult<'a, O> = IResult<&'a [u8], O, HWProtocolError>; |
|
52 |
||
53 |
fn end_of_message(input: &[u8]) -> HWResult<&[u8]> { |
|
54 |
tag("\n\n")(input) |
|
55 |
} |
|
56 |
||
57 |
fn convert_utf8(input: &[u8]) -> HWResult<&str> { |
|
58 |
match str::from_utf8(input) { |
|
59 |
Ok(str) => Ok((b"", str)), |
|
60 |
Err(utf_err) => Result::Err(Err::Failure(utf_err.into())), |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
fn convert_from_str<T>(str: &str) -> HWResult<T> |
|
65 |
where |
|
66 |
T: FromStr<Err = ParseIntError>, |
|
67 |
{ |
|
68 |
match T::from_str(str) { |
|
69 |
Ok(x) => Ok((b"", x)), |
|
70 |
Err(format_err) => Result::Err(Err::Failure(format_err.into())), |
|
71 |
} |
|
72 |
} |
|
73 |
||
74 |
fn str_line(input: &[u8]) -> HWResult<&str> { |
|
75 |
let (i, text) = not_line_ending(input)?; |
|
76 |
Ok((i, convert_utf8(text)?.1)) |
|
77 |
} |
|
78 |
||
79 |
fn a_line(input: &[u8]) -> HWResult<String> { |
|
80 |
let (i, str) = str_line(input)?; |
|
81 |
Ok((i, str.to_string())) |
|
82 |
} |
|
83 |
||
84 |
fn hw_tag<'a>(tag_str: &'a str) -> impl Fn(&'a [u8]) -> HWResult<'a, ()> { |
|
85 |
move |i| tag(tag_str)(i).map(|(i, _)| (i, ())) |
|
86 |
} |
|
87 |
||
88 |
fn hw_tag_no_case<'a>(tag_str: &'a str) -> impl Fn(&'a [u8]) -> HWResult<'a, ()> { |
|
89 |
move |i| tag_no_case(tag_str)(i).map(|(i, _)| (i, ())) |
|
90 |
} |
|
91 |
||
92 |
fn cmd_arg(input: &[u8]) -> HWResult<String> { |
|
93 |
let delimiters = b" \n"; |
|
94 |
let (i, str) = take_while(move |c| !delimiters.contains(&c))(input)?; |
|
95 |
Ok((i, convert_utf8(str)?.1.to_string())) |
|
96 |
} |
|
97 |
||
98 |
fn u8_line(input: &[u8]) -> HWResult<u8> { |
|
99 |
let (i, str) = str_line(input)?; |
|
100 |
Ok((i, convert_from_str(str)?.1)) |
|
101 |
} |
|
102 |
||
103 |
fn u16_line(input: &[u8]) -> HWResult<u16> { |
|
104 |
let (i, str) = str_line(input)?; |
|
105 |
Ok((i, convert_from_str(str)?.1)) |
|
106 |
} |
|
107 |
||
108 |
fn u32_line(input: &[u8]) -> HWResult<u32> { |
|
109 |
let (i, str) = str_line(input)?; |
|
110 |
Ok((i, convert_from_str(str)?.1)) |
|
111 |
} |
|
112 |
||
113 |
fn yes_no_line(input: &[u8]) -> HWResult<bool> { |
|
114 |
alt(( |
|
115 |
|i| tag_no_case(b"YES")(i).map(|(i, _)| (i, true)), |
|
116 |
|i| tag_no_case(b"NO")(i).map(|(i, _)| (i, false)), |
|
117 |
))(input) |
|
118 |
} |
|
119 |
||
120 |
fn opt_arg<'a>(input: &'a [u8]) -> HWResult<'a, Option<String>> { |
|
121 |
alt(( |
|
122 |
|i: &'a [u8]| peek!(i, end_of_message).map(|(i, _)| (i, None)), |
|
123 |
|i| precededc(i, hw_tag("\n"), a_line).map(|(i, v)| (i, Some(v))), |
|
124 |
))(input) |
|
125 |
} |
|
126 |
||
127 |
fn spaces(input: &[u8]) -> HWResult<&[u8]> { |
|
128 |
precededc(input, hw_tag(" "), |i| take_while(|c| c == b' ')(i)) |
|
129 |
} |
|
130 |
||
131 |
fn opt_space_arg<'a>(input: &'a [u8]) -> HWResult<'a, Option<String>> { |
|
132 |
alt(( |
|
133 |
|i: &'a [u8]| peek!(i, end_of_message).map(|(i, _)| (i, None)), |
|
134 |
|i| precededc(i, spaces, a_line).map(|(i, v)| (i, Some(v))), |
|
135 |
))(input) |
|
136 |
} |
|
12139 | 137 |
|
14780 | 138 |
fn hedgehog_array(input: &[u8]) -> HWResult<[HedgehogInfo; 8]> { |
139 |
fn hedgehog_line(input: &[u8]) -> HWResult<HedgehogInfo> { |
|
140 |
let (i, name) = terminatedc(input, a_line, eol)?; |
|
141 |
let (i, hat) = a_line(i)?; |
|
142 |
Ok((i, HedgehogInfo { name, hat })) |
|
143 |
} |
|
144 |
||
145 |
let (i, h1) = terminatedc(input, hedgehog_line, eol)?; |
|
146 |
let (i, h2) = terminatedc(i, hedgehog_line, eol)?; |
|
147 |
let (i, h3) = terminatedc(i, hedgehog_line, eol)?; |
|
148 |
let (i, h4) = terminatedc(i, hedgehog_line, eol)?; |
|
149 |
let (i, h5) = terminatedc(i, hedgehog_line, eol)?; |
|
150 |
let (i, h6) = terminatedc(i, hedgehog_line, eol)?; |
|
151 |
let (i, h7) = terminatedc(i, hedgehog_line, eol)?; |
|
152 |
let (i, h8) = hedgehog_line(i)?; |
|
153 |
||
154 |
Ok((i, [h1, h2, h3, h4, h5, h6, h7, h8])) |
|
155 |
} |
|
12138 | 156 |
|
14780 | 157 |
fn voting(input: &[u8]) -> HWResult<VoteType> { |
158 |
alt(( |
|
159 |
|i| tag_no_case("PAUSE")(i).map(|(i, _)| (i, VoteType::Pause)), |
|
160 |
|i| tag_no_case("NEWSEED")(i).map(|(i, _)| (i, VoteType::NewSeed)), |
|
161 |
|i| { |
|
162 |
precededc(i, |i| precededc(i, hw_tag_no_case("KICK"), spaces), a_line) |
|
163 |
.map(|(i, s)| (i, VoteType::Kick(s))) |
|
164 |
}, |
|
165 |
|i| { |
|
166 |
precededc( |
|
167 |
i, |
|
168 |
|i| precededc(i, hw_tag_no_case("HEDGEHOGS"), spaces), |
|
169 |
u8_line, |
|
170 |
) |
|
171 |
.map(|(i, n)| (i, VoteType::HedgehogsPerTeam(n))) |
|
172 |
}, |
|
173 |
|i| precededc(i, hw_tag_no_case("MAP"), opt_space_arg).map(|(i, v)| (i, VoteType::Map(v))), |
|
174 |
))(input) |
|
175 |
} |
|
12140 | 176 |
|
14780 | 177 |
fn no_arg_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
178 |
fn messagec<'a>( |
|
179 |
input: &'a [u8], |
|
180 |
name: &'a str, |
|
181 |
msg: HWProtocolMessage, |
|
182 |
) -> HWResult<'a, HWProtocolMessage> { |
|
183 |
tag(name)(input).map(|(i, _)| (i, msg.clone())) |
|
184 |
} |
|
185 |
||
186 |
alt(( |
|
187 |
|i| messagec(i, "PING", Ping), |
|
188 |
|i| messagec(i, "PONG", Pong), |
|
189 |
|i| messagec(i, "LIST", List), |
|
190 |
|i| messagec(i, "BANLIST", BanList), |
|
191 |
|i| messagec(i, "GET_SERVER_VAR", GetServerVar), |
|
192 |
|i| messagec(i, "TOGGLE_READY", ToggleReady), |
|
193 |
|i| messagec(i, "START_GAME", StartGame), |
|
194 |
|i| messagec(i, "TOGGLE_RESTRICT_JOINS", ToggleRestrictJoin), |
|
195 |
|i| messagec(i, "TOGGLE_RESTRICT_TEAMS", ToggleRestrictTeams), |
|
196 |
|i| messagec(i, "TOGGLE_REGISTERED_ONLY", ToggleRegisteredOnly), |
|
197 |
))(input) |
|
198 |
} |
|
12140 | 199 |
|
14780 | 200 |
fn single_arg_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
201 |
fn messagec<'a, T, F, G>( |
|
202 |
input: &'a [u8], |
|
203 |
name: &'a str, |
|
204 |
parser: F, |
|
205 |
constructor: G, |
|
206 |
) -> HWResult<'a, HWProtocolMessage> |
|
207 |
where |
|
208 |
F: Fn(&[u8]) -> HWResult<T>, |
|
209 |
G: Fn(T) -> HWProtocolMessage, |
|
210 |
{ |
|
211 |
precededc(input, hw_tag(name), parser).map(|(i, v)| (i, constructor(v))) |
|
212 |
} |
|
213 |
||
214 |
alt(( |
|
215 |
|i| messagec(i, "NICK\n", a_line, Nick), |
|
216 |
|i| messagec(i, "INFO\n", a_line, Info), |
|
217 |
|i| messagec(i, "CHAT\n", a_line, Chat), |
|
218 |
|i| messagec(i, "PART", opt_arg, Part), |
|
219 |
|i| messagec(i, "FOLLOW\n", a_line, Follow), |
|
220 |
|i| messagec(i, "KICK\n", a_line, Kick), |
|
221 |
|i| messagec(i, "UNBAN\n", a_line, Unban), |
|
222 |
|i| messagec(i, "EM\n", a_line, EngineMessage), |
|
223 |
|i| messagec(i, "TEAMCHAT\n", a_line, TeamChat), |
|
224 |
|i| messagec(i, "ROOM_NAME\n", a_line, RoomName), |
|
225 |
|i| messagec(i, "REMOVE_TEAM\n", a_line, RemoveTeam), |
|
226 |
|i| messagec(i, "ROUNDFINISHED", opt_arg, |_| RoundFinished), |
|
227 |
|i| messagec(i, "PROTO\n", u16_line, Proto), |
|
228 |
|i| messagec(i, "QUIT", opt_arg, Quit), |
|
229 |
))(input) |
|
230 |
} |
|
12140 | 231 |
|
14780 | 232 |
fn cmd_message<'a>(input: &'a [u8]) -> HWResult<'a, HWProtocolMessage> { |
233 |
fn cmdc_no_arg<'a>( |
|
234 |
input: &'a [u8], |
|
235 |
name: &'a str, |
|
236 |
msg: HWProtocolMessage, |
|
237 |
) -> HWResult<'a, HWProtocolMessage> { |
|
238 |
tag_no_case(name)(input).map(|(i, _)| (i, msg.clone())) |
|
239 |
} |
|
240 |
||
241 |
fn cmdc_single_arg<'a, T, F, G>( |
|
242 |
input: &'a [u8], |
|
243 |
name: &'a str, |
|
244 |
parser: F, |
|
245 |
constructor: G, |
|
246 |
) -> HWResult<'a, HWProtocolMessage> |
|
247 |
where |
|
248 |
F: Fn(&'a [u8]) -> HWResult<'a, T>, |
|
249 |
G: Fn(T) -> HWProtocolMessage, |
|
250 |
{ |
|
251 |
precededc(input, |i| pairc(i, hw_tag_no_case(name), spaces), parser) |
|
252 |
.map(|(i, v)| (i, constructor(v))) |
|
253 |
} |
|
254 |
||
255 |
fn cmd_no_arg_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
|
256 |
alt(( |
|
257 |
|i| cmdc_no_arg(i, "STATS", Stats), |
|
258 |
|i| cmdc_no_arg(i, "FIX", Fix), |
|
259 |
|i| cmdc_no_arg(i, "UNFIX", Unfix), |
|
260 |
|i| cmdc_no_arg(i, "REGISTERED_ONLY", ToggleServerRegisteredOnly), |
|
261 |
|i| cmdc_no_arg(i, "SUPER_POWER", SuperPower), |
|
262 |
))(input) |
|
263 |
} |
|
12140 | 264 |
|
14780 | 265 |
fn cmd_single_arg_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
266 |
alt(( |
|
267 |
|i| cmdc_single_arg(i, "RESTART_SERVER", |i| tag("YES")(i), |_| RestartServer), |
|
268 |
|i| cmdc_single_arg(i, "DELEGATE", a_line, Delegate), |
|
269 |
|i| cmdc_single_arg(i, "DELETE", a_line, Delete), |
|
270 |
|i| cmdc_single_arg(i, "SAVEROOM", a_line, SaveRoom), |
|
271 |
|i| cmdc_single_arg(i, "LOADROOM", a_line, LoadRoom), |
|
272 |
|i| cmdc_single_arg(i, "GLOBAL", a_line, Global), |
|
273 |
|i| cmdc_single_arg(i, "WATCH", a_line, Watch), |
|
274 |
|i| cmdc_single_arg(i, "GREETING", a_line, Greeting), |
|
275 |
|i| cmdc_single_arg(i, "VOTE", yes_no_line, Vote), |
|
276 |
|i| cmdc_single_arg(i, "FORCE", yes_no_line, ForceVote), |
|
277 |
|i| cmdc_single_arg(i, "INFO", a_line, Info), |
|
278 |
|i| cmdc_single_arg(i, "MAXTEAMS", u8_line, MaxTeams), |
|
279 |
|i| cmdc_single_arg(i, "CALLVOTE", |i| opt!(i, voting), CallVote), |
|
280 |
))(input) |
|
281 |
} |
|
282 |
||
283 |
precededc( |
|
284 |
input, |
|
285 |
hw_tag("CMD\n"), |
|
286 |
alt(( |
|
287 |
cmd_no_arg_message, |
|
288 |
cmd_single_arg_message, |
|
289 |
|i| precededc(i, hw_tag_no_case("PART"), opt_space_arg).map(|(i, s)| (i, Part(s))), |
|
290 |
|i| precededc(i, hw_tag_no_case("QUIT"), opt_space_arg).map(|(i, s)| (i, Quit(s))), |
|
291 |
|i| { |
|
292 |
precededc(i, hw_tag_no_case("SAVE"), |i| { |
|
293 |
pairc( |
|
294 |
i, |
|
295 |
|i| precededc(i, spaces, cmd_arg), |
|
296 |
|i| precededc(i, spaces, cmd_arg), |
|
297 |
) |
|
298 |
}) |
|
299 |
.map(|(i, (n, l))| (i, Save(n, l))) |
|
300 |
}, |
|
301 |
|i| { |
|
302 |
let (i, _) = tag_no_case("RND")(i)?; |
|
303 |
let (i, _) = alt((spaces, |i: &'a [u8]| peek!(i, end_of_message)))(i)?; |
|
304 |
let (i, v) = str_line(i)?; |
|
305 |
Ok((i, Rnd(v.split_whitespace().map(String::from).collect()))) |
|
306 |
}, |
|
307 |
)), |
|
308 |
) |
|
309 |
} |
|
310 |
||
311 |
fn config_message<'a>(input: &'a [u8]) -> HWResult<'a, HWProtocolMessage> { |
|
312 |
fn cfgc_single_arg<'a, T, F, G>( |
|
313 |
input: &'a [u8], |
|
314 |
name: &'a str, |
|
315 |
parser: F, |
|
316 |
constructor: G, |
|
317 |
) -> HWResult<'a, GameCfg> |
|
318 |
where |
|
319 |
F: Fn(&[u8]) -> HWResult<T>, |
|
320 |
G: Fn(T) -> GameCfg, |
|
321 |
{ |
|
322 |
precededc(input, |i| terminatedc(i, hw_tag(name), eol), parser) |
|
323 |
.map(|(i, v)| (i, constructor(v))) |
|
324 |
} |
|
325 |
||
326 |
let (i, cfg) = precededc( |
|
327 |
input, |
|
328 |
hw_tag("CFG\n"), |
|
329 |
alt(( |
|
330 |
|i| cfgc_single_arg(i, "THEME", a_line, GameCfg::Theme), |
|
331 |
|i| cfgc_single_arg(i, "SCRIPT", a_line, GameCfg::Script), |
|
332 |
|i| cfgc_single_arg(i, "MAP", a_line, GameCfg::MapType), |
|
333 |
|i| cfgc_single_arg(i, "MAPGEN", u32_line, GameCfg::MapGenerator), |
|
334 |
|i| cfgc_single_arg(i, "MAZE_SIZE", u32_line, GameCfg::MazeSize), |
|
335 |
|i| cfgc_single_arg(i, "TEMPLATE", u32_line, GameCfg::Template), |
|
336 |
|i| cfgc_single_arg(i, "FEATURE_SIZE", u32_line, GameCfg::FeatureSize), |
|
337 |
|i| cfgc_single_arg(i, "SEED", a_line, GameCfg::Seed), |
|
338 |
|i| cfgc_single_arg(i, "DRAWNMAP", a_line, GameCfg::DrawnMap), |
|
339 |
|i| { |
|
340 |
precededc( |
|
341 |
i, |
|
342 |
|i| terminatedc(i, hw_tag("AMMO"), eol), |
|
343 |
|i| { |
|
344 |
let (i, name) = a_line(i)?; |
|
345 |
let (i, value) = opt_arg(i)?; |
|
346 |
Ok((i, GameCfg::Ammo(name, value))) |
|
347 |
}, |
|
348 |
) |
|
349 |
}, |
|
350 |
|i| { |
|
351 |
precededc( |
|
352 |
i, |
|
353 |
|i| terminatedc(i, hw_tag("SCHEME"), eol), |
|
354 |
|i| { |
|
355 |
let (i, name) = a_line(i)?; |
|
356 |
let (i, values) = alt(( |
|
357 |
|i: &'a [u8]| peek!(i, end_of_message).map(|(i, _)| (i, None)), |
|
358 |
|i| { |
|
359 |
precededc(i, eol, |i| separated_list(eol, a_line)(i)) |
|
360 |
.map(|(i, v)| (i, Some(v))) |
|
361 |
}, |
|
362 |
))(i)?; |
|
363 |
Ok((i, GameCfg::Scheme(name, values.unwrap_or_default()))) |
|
364 |
}, |
|
365 |
) |
|
366 |
}, |
|
367 |
)), |
|
368 |
)?; |
|
369 |
Ok((i, Cfg(cfg))) |
|
370 |
} |
|
12138 | 371 |
|
14780 | 372 |
fn complex_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
373 |
alt(( |
|
374 |
|i| { |
|
375 |
precededc( |
|
376 |
i, |
|
377 |
|i| terminatedc(i, hw_tag("PASSWORD"), eol), |
|
378 |
|i| { |
|
379 |
let (i, pass) = terminatedc(i, a_line, eol)?; |
|
380 |
let (i, salt) = a_line(i)?; |
|
381 |
Ok((i, Password(pass, salt))) |
|
382 |
}, |
|
383 |
) |
|
384 |
}, |
|
385 |
|i| { |
|
386 |
precededc( |
|
387 |
i, |
|
388 |
|i| terminatedc(i, hw_tag("CHECKER"), eol), |
|
389 |
|i| { |
|
390 |
let (i, protocol) = terminatedc(i, u16_line, eol)?; |
|
391 |
let (i, name) = terminatedc(i, a_line, eol)?; |
|
392 |
let (i, pass) = a_line(i)?; |
|
393 |
Ok((i, Checker(protocol, name, pass))) |
|
394 |
}, |
|
395 |
) |
|
396 |
}, |
|
397 |
|i| { |
|
398 |
precededc( |
|
399 |
i, |
|
400 |
|i| terminatedc(i, hw_tag("CREATE_ROOM"), eol), |
|
401 |
|i| { |
|
402 |
let (i, name) = a_line(i)?; |
|
403 |
let (i, pass) = opt_arg(i)?; |
|
404 |
Ok((i, CreateRoom(name, pass))) |
|
405 |
}, |
|
406 |
) |
|
407 |
}, |
|
408 |
|i| { |
|
409 |
precededc( |
|
410 |
i, |
|
411 |
|i| terminatedc(i, hw_tag("JOIN_ROOM"), eol), |
|
412 |
|i| { |
|
413 |
let (i, name) = a_line(i)?; |
|
414 |
let (i, pass) = opt_arg(i)?; |
|
415 |
Ok((i, JoinRoom(name, pass))) |
|
416 |
}, |
|
417 |
) |
|
418 |
}, |
|
419 |
|i| { |
|
420 |
precededc( |
|
421 |
i, |
|
422 |
|i| terminatedc(i, hw_tag("ADD_TEAM"), eol), |
|
423 |
|i| { |
|
424 |
let (i, name) = terminatedc(i, a_line, eol)?; |
|
425 |
let (i, color) = terminatedc(i, u8_line, eol)?; |
|
426 |
let (i, grave) = terminatedc(i, a_line, eol)?; |
|
427 |
let (i, fort) = terminatedc(i, a_line, eol)?; |
|
428 |
let (i, voice_pack) = terminatedc(i, a_line, eol)?; |
|
429 |
let (i, flag) = terminatedc(i, a_line, eol)?; |
|
430 |
let (i, difficulty) = terminatedc(i, u8_line, eol)?; |
|
431 |
let (i, hedgehogs) = hedgehog_array(i)?; |
|
432 |
Ok(( |
|
433 |
i, |
|
434 |
AddTeam(Box::new(TeamInfo { |
|
435 |
name, |
|
436 |
color, |
|
437 |
grave, |
|
438 |
fort, |
|
439 |
voice_pack, |
|
440 |
flag, |
|
441 |
difficulty, |
|
442 |
hedgehogs, |
|
443 |
hedgehogs_number: 0, |
|
444 |
})), |
|
445 |
)) |
|
446 |
}, |
|
447 |
) |
|
448 |
}, |
|
449 |
|i| { |
|
450 |
precededc( |
|
451 |
i, |
|
452 |
|i| terminatedc(i, hw_tag("HH_NUM"), eol), |
|
453 |
|i| { |
|
454 |
let (i, name) = terminatedc(i, a_line, eol)?; |
|
455 |
let (i, count) = u8_line(i)?; |
|
456 |
Ok((i, SetHedgehogsNumber(name, count))) |
|
457 |
}, |
|
458 |
) |
|
459 |
}, |
|
460 |
|i| { |
|
461 |
precededc( |
|
462 |
i, |
|
463 |
|i| terminatedc(i, hw_tag("TEAM_COLOR"), eol), |
|
464 |
|i| { |
|
465 |
let (i, name) = terminatedc(i, a_line, eol)?; |
|
466 |
let (i, color) = u8_line(i)?; |
|
467 |
Ok((i, SetTeamColor(name, color))) |
|
468 |
}, |
|
469 |
) |
|
470 |
}, |
|
471 |
|i| { |
|
472 |
precededc( |
|
473 |
i, |
|
474 |
|i| terminatedc(i, hw_tag("BAN"), eol), |
|
475 |
|i| { |
|
476 |
let (i, n) = terminatedc(i, a_line, eol)?; |
|
477 |
let (i, r) = terminatedc(i, a_line, eol)?; |
|
478 |
let (i, t) = u32_line(i)?; |
|
479 |
Ok((i, Ban(n, r, t))) |
|
480 |
}, |
|
481 |
) |
|
482 |
}, |
|
483 |
|i| { |
|
484 |
precededc( |
|
485 |
i, |
|
486 |
|i| terminatedc(i, hw_tag("BAN_IP"), eol), |
|
487 |
|i| { |
|
488 |
let (i, n) = terminatedc(i, a_line, eol)?; |
|
489 |
let (i, r) = terminatedc(i, a_line, eol)?; |
|
490 |
let (i, t) = u32_line(i)?; |
|
491 |
Ok((i, BanIP(n, r, t))) |
|
492 |
}, |
|
493 |
) |
|
494 |
}, |
|
495 |
|i| { |
|
496 |
precededc( |
|
497 |
i, |
|
498 |
|i| terminatedc(i, hw_tag("BAN_NICK"), eol), |
|
499 |
|i| { |
|
500 |
let (i, n) = terminatedc(i, a_line, eol)?; |
|
501 |
let (i, r) = terminatedc(i, a_line, eol)?; |
|
502 |
let (i, t) = u32_line(i)?; |
|
503 |
Ok((i, BanNick(n, r, t))) |
|
504 |
}, |
|
505 |
) |
|
506 |
}, |
|
507 |
))(input) |
|
508 |
} |
|
13427 | 509 |
|
14780 | 510 |
fn empty_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
511 |
let (i, _) = alt((end_of_message, eol))(input)?; |
|
512 |
Ok((i, Empty)) |
|
513 |
} |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
514 |
|
14780 | 515 |
fn malformed_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
516 |
let (i, _) = separated_listc(input, eol, a_line)?; |
|
517 |
Ok((i, Malformed)) |
|
518 |
} |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
519 |
|
14780 | 520 |
fn message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
521 |
alt(( |
|
522 |
|i| { |
|
523 |
terminatedc( |
|
524 |
i, |
|
525 |
alt(( |
|
526 |
no_arg_message, |
|
527 |
single_arg_message, |
|
528 |
cmd_message, |
|
529 |
config_message, |
|
530 |
complex_message, |
|
531 |
)), |
|
532 |
end_of_message, |
|
533 |
) |
|
534 |
}, |
|
535 |
|i| terminatedc(i, malformed_message, end_of_message), |
|
536 |
empty_message, |
|
537 |
))(input) |
|
538 |
} |
|
12138 | 539 |
|
14780 | 540 |
pub fn extract_messages<'a>(input: &'a [u8]) -> HWResult<Vec<HWProtocolMessage>> { |
541 |
many0(|i: &'a [u8]| complete!(i, message))(input) |
|
542 |
} |
|
12138 | 543 |
|
13801 | 544 |
#[cfg(test)] |
14782 | 545 |
mod test { |
546 |
use super::{extract_messages, message}; |
|
547 |
use crate::protocol::{messages::HWProtocolMessage::*, test::gen_proto_msg}; |
|
548 |
use proptest::{proptest, proptest_helper}; |
|
549 |
||
550 |
#[cfg(test)] |
|
551 |
proptest! { |
|
552 |
#[test] |
|
553 |
fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) { |
|
554 |
println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes()); |
|
555 |
assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone()))) |
|
556 |
} |
|
557 |
} |
|
558 |
||
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
559 |
#[test] |
14782 | 560 |
fn parse_test() { |
561 |
assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping))); |
|
562 |
assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame))); |
|
563 |
assert_eq!( |
|
564 |
message(b"NICK\nit's me\n\n"), |
|
565 |
Ok((&b""[..], Nick("it's me".to_string()))) |
|
566 |
); |
|
567 |
assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51)))); |
|
568 |
assert_eq!( |
|
569 |
message(b"QUIT\nbye-bye\n\n"), |
|
570 |
Ok((&b""[..], Quit(Some("bye-bye".to_string())))) |
|
571 |
); |
|
572 |
assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None)))); |
|
573 |
assert_eq!( |
|
574 |
message(b"CMD\nwatch demo\n\n"), |
|
575 |
Ok((&b""[..], Watch("demo".to_string()))) |
|
576 |
); |
|
577 |
assert_eq!( |
|
578 |
message(b"BAN\nme\nbad\n77\n\n"), |
|
579 |
Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77))) |
|
580 |
); |
|
581 |
||
582 |
assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None)))); |
|
583 |
assert_eq!( |
|
584 |
message(b"CMD\nPART _msg_\n\n"), |
|
585 |
Ok((&b""[..], Part(Some("_msg_".to_string())))) |
|
586 |
); |
|
587 |
||
588 |
assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![])))); |
|
589 |
assert_eq!( |
|
590 |
message(b"CMD\nRND A B\n\n"), |
|
591 |
Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")]))) |
|
592 |
); |
|
593 |
||
594 |
assert_eq!( |
|
595 |
extract_messages(b"QUIT\n1\n2\n\n"), |
|
596 |
Ok((&b""[..], vec![Malformed])) |
|
597 |
); |
|
598 |
||
599 |
assert_eq!( |
|
600 |
extract_messages(b"PING\n\nPING\n\nP"), |
|
601 |
Ok((&b"P"[..], vec![Ping, Ping])) |
|
602 |
); |
|
603 |
assert_eq!( |
|
604 |
extract_messages(b"SING\n\nPING\n\n"), |
|
605 |
Ok((&b""[..], vec![Malformed, Ping])) |
|
606 |
); |
|
607 |
assert_eq!( |
|
608 |
extract_messages(b"\n\n\n\nPING\n\n"), |
|
609 |
Ok((&b""[..], vec![Empty, Empty, Ping])) |
|
610 |
); |
|
611 |
assert_eq!( |
|
612 |
extract_messages(b"\n\n\nPING\n\n"), |
|
613 |
Ok((&b""[..], vec![Empty, Empty, Ping])) |
|
614 |
); |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
615 |
} |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
616 |
} |