From b6d54d086e86c5029cd0ebf6f1d92295497b77d5 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Tue, 4 Oct 2022 09:46:17 -0500 Subject: [PATCH] test and lint --- src/main.rs | 11 +++++------ src/message.rs | 27 +++++++++------------------ src/websocket.rs | 4 ++-- test/test.py | 2 +- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index ad36f4e..6447154 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,20 +20,18 @@ fn main() { let code_generator = Arc::clone(&code_generator); let rooms = Arc::clone(&rooms); thread::spawn (move || { - let mut ws = tungstenite::accept(stream.unwrap()).unwrap(); - println!("New client!"); + let ws = tungstenite::accept(stream.unwrap()).unwrap(); let mut ws = WebsocketWrapper::new(ws); + ws.send(msg!("HOSTJOIN")); - let msg = ws.recv(); match msg { None => (), - Some(msg) => match msg.command.as_str() { "HOST" => { let code = code_generator.lock().unwrap().generate(); let mut room = Room::default(); - let player = Player { name: "Guest".to_string() }; + let player = Player {}; room.players.push(player); rooms.lock().unwrap().insert(code.clone(), room); ws.send(msg!("CODE", code)); @@ -41,14 +39,15 @@ fn main() { _ => unimplemented!(), } } + }); } } + #[derive(Default)] struct Room { players: Vec, } struct Player { - name: String, } diff --git a/src/message.rs b/src/message.rs index 7b8075d..c95a994 100644 --- a/src/message.rs +++ b/src/message.rs @@ -10,13 +10,9 @@ pub type Result = std::result::Result; #[derive(PartialEq, Debug)] pub enum Error { BadParse, - UnknownCommand, } impl Message { - pub fn new(command: String, args: Vec) -> Self { - Message { command, args } - } pub fn parse(text: String) -> Result { let re = regex::Regex::new(r"^([A-Z_]+):\s*(.*)").unwrap(); match re.captures(text.as_str()) { @@ -49,10 +45,7 @@ macro_rules! msg { ( $command:expr, $( $arg:expr ),*) => { { let command = $command.to_string(); - let mut args = Vec::new(); - $( - args.push($arg.to_string()); - )* + let args = vec![$($arg.to_string()),*]; Message { command, args } } }; @@ -61,7 +54,11 @@ macro_rules! msg { impl std::fmt::Display for Message { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { - write!(f, "{}: {}", self.command, self.args.as_slice().join(", ")) + if self.args.is_empty() { + write!(f, "{}:", self.command) + } else { + write!(f, "{}: {}", self.command, self.args.as_slice().join(", ")) + } } } @@ -72,19 +69,13 @@ mod test { #[test] fn test_parse() -> Result<()> { let text = "COMMAND: arg1, arg2"; - let msg = Message::parse(text)?; - assert_eq!(Message { - command: "COMMAND", - args: vec!["arg1", "arg2"], - }, msg); + let msg = Message::parse(text.to_string())?; + assert_eq!(msg!("COMMAND", "arg1", "arg2"), msg); Ok(()) } #[test] fn test_to_string() { - let msg = Message { - command: "COMMAND", - args: vec!["arg1", "arg2"], - }; + let msg = msg!("COMMAND", "arg1", "arg2"); assert_eq!(msg.to_string(), "COMMAND: arg1, arg2".to_string()); } } diff --git a/src/websocket.rs b/src/websocket.rs index 68d3c6d..dce8417 100644 --- a/src/websocket.rs +++ b/src/websocket.rs @@ -21,12 +21,12 @@ impl WebsocketWrapper { Ok(WsMessage::Text(text)) => match Message::parse(text) { Ok(msg) => Some(msg), Err(_) => { - self.websocket.write_message(WsMessage::Text("ERROR: bad_format".to_string())); + self.websocket.write_message(WsMessage::Text("ERROR: bad_format".to_string())).unwrap(); self.recv() } } _ => { - self.websocket.write_message(WsMessage::Text("ERROR: bad_command".to_string())); + self.websocket.write_message(WsMessage::Text("ERROR: bad_command".to_string())).unwrap(); self.recv() } } diff --git a/test/test.py b/test/test.py index 5777945..1592b33 100644 --- a/test/test.py +++ b/test/test.py @@ -8,7 +8,7 @@ async def test(): assert(response == "HOSTJOIN:") await ws.send("HOST:") response = await ws.recv() - assert(re.match(r"(\d|[a-f]){6}", response)) + assert(re.match(r"CODE: [a-f\d]{6}", response)) asyncio.get_event_loop().run_until_complete(test()) print("All tests passed")