Fixed parsing bug allowing messages without arguments to parse correctly.

This commit is contained in:
Aaron Weiss 2018-01-06 13:31:08 -05:00
parent 6821fd4853
commit 40ff295821
No known key found for this signature in database
GPG key ID: 047D32DF25DC22EF

View file

@ -222,7 +222,15 @@ impl FromStr for Message {
state = state.find(' ').map_or("", |i| &state[i + 1..]);
cmd
}
_ => return Err(ErrorKind::InvalidCommand.into()),
// If there's no arguments but the "command" starts with colon, it's not a command.
None if state.starts_with(":") => return Err(ErrorKind::InvalidCommand.into()),
// If there's no arguments following the command, the rest of the state is the command.
None => {
let cmd = state;
state = "";
cmd
},
};
let args: Vec<_> = state.splitn(14, ' ').filter(|s| !s.is_empty()).collect();
Message::with_tags(tags, prefix, command, args, suffix)
@ -252,7 +260,7 @@ pub struct Tag(pub String, pub Option<String>);
#[cfg(test)]
mod test {
use super::{Message, Tag};
use proto::Command::{PRIVMSG, Raw};
use proto::Command::{PRIVMSG, QUIT, Raw};
#[test]
fn new() {
@ -448,6 +456,17 @@ mod test {
assert_eq!(msg, message);
}
#[test]
fn to_message_no_prefix_no_args() {
let message = Message {
tags: None,
prefix: None,
command: QUIT(None),
};
let msg: Message = "QUIT\r\n".into();
assert_eq!(msg, message);
}
#[test]
#[should_panic]
fn to_message_invalid_format() {