From 3ab9cedbcdefe8b449de52c76046e0e7f1ddd7e1 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 12 Oct 2022 11:08:24 -0500 Subject: [PATCH] Pass first argument to msg! as a unquoted literal --- src/client.rs | 6 +++--- src/message.rs | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/client.rs b/src/client.rs index e0a9e0a..869d816 100644 --- a/src/client.rs +++ b/src/client.rs @@ -62,7 +62,7 @@ impl Client { let game_controller = Arc::new(Mutex::new(game_controller)); self.global_state.rooms.lock().await.insert(room_code.clone(), Arc::clone(&game_controller)); tokio::spawn(async move {game_loop(game_controller)}); - self.ws.send(msg!("ROOM_CODE", room_code)).await.unwrap(); + self.ws.send(msg!(ROOM_CODE, room_code)).await.unwrap(); } "JOIN" => { let room_code = &msg.args[0]; @@ -75,10 +75,10 @@ impl Client { let (client_channel, server_channel) = channel_pair(); self.channel = Some(client_channel); room.channels.push(server_channel); - self.ws.send(msg!("JOIN_OK")).await.unwrap(); + self.ws.send(msg!(JOIN_OK)).await.unwrap(); } None => { - self.ws.send(msg!("JOIN_INVALID")).await.unwrap(); + self.ws.send(msg!(JOIN_INVALID)).await.unwrap(); } } } diff --git a/src/message.rs b/src/message.rs index 150265c..1b0a38d 100644 --- a/src/message.rs +++ b/src/message.rs @@ -83,16 +83,16 @@ impl MessageWebSocket { #[macro_export] macro_rules! msg { - ( $command:expr) => { + ( $command:ident) => { { - let command = $command.to_string(); + let command = stringify!($command).to_string(); let args = vec![]; Message { command, args } } }; - ( $command:expr, $( $arg:expr ),*) => { + ( $command:ident, $( $arg:expr ),*) => { { - let command = $command.to_string(); + let command = stringify!($command).to_string(); let args = vec![$($arg.to_string()),*]; Message { command, args } } @@ -117,12 +117,12 @@ mod test { fn test_parse() -> Result<()> { let text = "COMMAND: arg1, arg2"; let msg = Message::parse(text.to_string())?; - assert_eq!(msg!("COMMAND", "arg1", "arg2"), msg); + assert_eq!(msg!(COMMAND, "arg1", "arg2"), msg); Ok(()) } #[test] fn test_to_string() { - let msg = msg!("COMMAND", "arg1", "arg2"); + let msg = msg!(COMMAND, "arg1", "arg2"); assert_eq!(msg.to_string(), "COMMAND: arg1, arg2".to_string()); } }