Added get_source_nickname() to Message.

This commit is contained in:
Aaron Weiss 2015-01-02 15:20:26 -05:00
parent 327be754d5
commit 48788fee5f

View file

@ -31,6 +31,12 @@ impl Message {
}
}
/// Gets the nickname of the message source, if it exists.
#[experimental]
pub fn get_source_nickname(&self) -> Option<&str> {
self.prefix.as_ref().and_then(|s| s.find('!').map(|i| s[..i]))
}
/// Converts a Message into a String according to the IRC protocol.
#[experimental]
pub fn into_string(&self) -> String {
@ -118,6 +124,17 @@ mod test {
assert_eq!(Message::new(None, "PRIVMSG", Some(vec!["test"]), Some("Testing!")), message);
}
#[test]
fn get_source_nickname() {
assert_eq!(Message::new(None, "PING", None, None).get_source_nickname(), None);
assert_eq!(Message::new(
Some("irc.test.net"), "PING", None, None
).get_source_nickname(), None);
assert_eq!(Message::new(
Some("test!test@test"), "PING", None, None
).get_source_nickname(), Some("test"));
}
#[test]
fn into_string() {
let message = Message {