use join() instead of looping over the Vec

This commit is contained in:
Yash Karandikar 2021-12-24 12:17:04 -06:00
parent 304f800b4d
commit 772c84d7d5
Signed by: karx
GPG key ID: A794DA2529474BA5

View file

@ -81,20 +81,14 @@ async fn connection_handler(
break;
}
if buf.starts_with("MSG") {
let mut msg_send = String::new();
let mut msg = buf.split_whitespace().collect::<Vec<&str>>();
if msg.len() < 2 {
cm_sender.send(ClientMessage::ERR(format!("Can't send empty messages")))?;
} else {
msg.remove(0);
for w in msg {
msg_send.push_str(w);
msg_send.push_str(" ");
}
msg_send.push_str("\n");
br_sender.send(BroadcastMessage::MSG(nick.clone(), msg_send, id.clone()))?;
br_sender.send(BroadcastMessage::MSG(nick.clone(), msg.join(" "), id.clone()))?;
}
} else if buf.starts_with("NICK") {
let old_nick = nick.clone();
@ -132,7 +126,7 @@ async fn connection_handler(
BroadcastMessage::MSG(nick, msg, sender) => {
if sender != id {
tracing::info!("{}: {:?}", nick, msg);
tx.write_all(format!("{} MSG {}", nick, msg).as_bytes()).await?;
tx.write_all(format!("{} MSG {}\n", nick, msg).as_bytes()).await?;
}
},
BroadcastMessage::NICK(old_nick, new_nick, sender) => {
@ -143,13 +137,11 @@ async fn connection_handler(
},
BroadcastMessage::JOIN(sender) => {
if sender != nick {
tracing::info!("{} joined", sender);
tx.write_all(format!("JOIN {}\n", sender).as_bytes()).await?;
}
},
BroadcastMessage::LEAVE(sender) => {
if sender != nick {
tracing::info!("{} joined", sender);
tx.write_all(format!("LEAVE {}\n", sender).as_bytes()).await?;
}
},