Pass first argument to msg! as a unquoted literal

This commit is contained in:
Dane Johnson 2022-10-12 11:08:24 -05:00
parent ab3c28ff37
commit 3ab9cedbcd
2 changed files with 9 additions and 9 deletions

View File

@ -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();
}
}
}

View File

@ -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());
}
}