Skip to content

Commit

Permalink
proto::message uses Prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
brigand committed Sep 17, 2018
1 parent 113abd4 commit 1ad3a56
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/proto/message.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! A module providing a data structure for messages to and from IRC servers.
use std::borrow::ToOwned;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::fmt::{Display, Formatter, Result as FmtResult, Write};
use std::str::FromStr;

use error;
use error::{IrcError, MessageParseError};
use proto::{Command, ChannelExt};
use proto::{Command, ChannelExt, Prefix};

/// A data structure representing an IRC message according to the protocol specification. It
/// consists of a collection of IRCv3 tags, a prefix (describing the source of the message), and
Expand All @@ -19,7 +19,7 @@ pub struct Message {
/// in IRCv3 extensions to the IRC protocol.
pub tags: Option<Vec<Tag>>,
/// The message prefix (or source) as defined by [RFC 2812](http://tools.ietf.org/html/rfc2812).
pub prefix: Option<String>,
pub prefix: Option<Prefix>,
/// The IRC command, parsed according to the known specifications. The command itself and its
/// arguments (including the special suffix argument) are captured in this component.
pub command: Command,
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Message {
) -> Result<Message, error::MessageParseError> {
Ok(Message {
tags: tags,
prefix: prefix.map(|s| s.to_owned()),
prefix: prefix.map(|p| p.into()),
command: Command::new(command, args, suffix)?,
})
}
Expand All @@ -80,15 +80,9 @@ impl Message {
pub fn source_nickname(&self) -> Option<&str> {
// <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
// <servername> ::= <host>
self.prefix.as_ref().and_then(|s| match (
s.find('!'),
s.find('@'),
s.find('.'),
) {
(Some(i), _, _) | // <nick> '!' <user> [ '@' <host> ]
(None, Some(i), _) => Some(&s[..i]), // <nick> '@' <host>
(None, None, None) => Some(s), // <nick>
_ => None, // <servername>
self.prefix.as_ref().and_then(|p| match p {
Prefix::Nickname(name, _, _) => Some(&name[..]),
_ => None
})
}

Expand Down Expand Up @@ -148,9 +142,7 @@ impl Message {
ret.push(' ');
}
if let Some(ref prefix) = self.prefix {
ret.push(':');
ret.push_str(prefix);
ret.push(' ');
write!(ret, ":{} ", prefix).unwrap();
}
let cmd: String = From::from(&self.command);
ret.push_str(&cmd);
Expand Down Expand Up @@ -361,7 +353,7 @@ mod test {
assert_eq!(&message.to_string()[..], "PRIVMSG test :Testing!\r\n");
let message = Message {
tags: None,
prefix: Some(format!("test!test@test")),
prefix: Some("test!test@test".into()),
command: PRIVMSG(format!("test"), format!("Still testing!")),
};
assert_eq!(
Expand All @@ -383,7 +375,7 @@ mod test {
);
let message = Message {
tags: None,
prefix: Some(format!("test!test@test")),
prefix: Some("test!test@test".into()),
command: PRIVMSG(format!("test"), format!("Still testing!")),
};
assert_eq!(
Expand All @@ -398,7 +390,7 @@ mod test {
Tag(format!("ccc"), None),
Tag(format!("example.com/ddd"), Some(format!("eee"))),
]),
prefix: Some(format!("test!test@test")),
prefix: Some("test!test@test".into()),
command: PRIVMSG(format!("test"), format!("Testing with tags!")),
};
assert_eq!(
Expand Down Expand Up @@ -449,7 +441,7 @@ mod test {
assert_eq!(msg, message);
let message = Message {
tags: None,
prefix: Some(format!("test!test@test")),
prefix: Some("test!test@test".into()),
command: PRIVMSG(format!("test"), format!("Still testing!")),
};
let msg: Message = ":test!test@test PRIVMSG test :Still testing!\r\n".into();
Expand All @@ -462,7 +454,7 @@ mod test {
// colons within individual parameters. So, let's make sure it parses correctly.
let message = Message {
tags: None,
prefix: Some(format!("test!test@test")),
prefix: Some("test!test@test".into()),
command: Raw(
format!("COMMAND"),
vec![format!("ARG:test")],
Expand Down

0 comments on commit 1ad3a56

Please sign in to comment.