Updated get_source_nickname behavior to match spec (fixes #40).

This commit is contained in:
Aaron Weiss 2015-11-26 13:11:02 -05:00
parent 7eebe9ad25
commit 8104894c28

View file

@ -47,7 +47,14 @@ impl Message {
/// Gets the nickname of the message source, if it exists.
pub fn get_source_nickname(&self) -> Option<&str> {
self.prefix.as_ref().and_then(|s| s.find('!').map(|i| &s[..i]))
self.prefix.as_ref().and_then(|s|
match (s.find('!'), s.find('@'), s.find('.')) {
(_, _, Some(_)) => None,
(Some(i), _, None) => Some(&s[..i]),
(None, Some(i), None) => Some(&s[..i]),
(None, None, None) => Some(&s)
}
)
}
/// Converts a Message into a String according to the IRC protocol.
@ -152,6 +159,12 @@ mod test {
assert_eq!(Message::new(
Some("test!test@test"), "PING", None, None
).get_source_nickname(), Some("test"));
assert_eq!(Message::new(
Some("test@test"), "PING", None, None
).get_source_nickname(), Some("test"));
assert_eq!(Message::new(
Some("test"), "PING", None, None
).get_source_nickname(), Some("test"));
}
#[test]