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