Don't panic if index is empty

This commit is contained in:
Yash Karandikar 2021-11-16 15:18:22 -06:00
parent 9dbac49bc4
commit e0472ea6f0

View file

@ -178,7 +178,12 @@ pub enum Command {
}
impl Command {
/// Performs the conversion
/// Creates a Command from a `&str`. Currently only `[PING]` and `[PRIVMSG]` are supported.
///
/// # Panics
///
/// This function will panic if the IRCd sends malformed messages. Please contact the
/// maintainer of your IRCd if this happens.
pub fn from_str(s: &str) -> Self {
let new = s.trim();
@ -187,12 +192,14 @@ impl Command {
let parts: Vec<&str> = new.split_whitespace().collect();
if parts[0] == "PING" {
if parts.get(0) == Some(&"PING") {
// We can assume that [1] exists because if it doesn't then something's gone very wrong
// with the IRCD
let command = parts[1].to_string();
return Self::PONG(command);
} else if parts[1] == "PRIVMSG" {
} else if parts.get(1) == Some(&"PRIVMSG") {
let nick = parts[0];
let index = nick.chars().position(|c| c == '!').unwrap();
let index = nick.chars().position(|c| c == '!').unwrap(); // This panics for the same reason as above
let nick = String::from(&nick[1..index]);
let target = parts[2];
let mut builder = String::new();