From cd3a5cc95b73674cac7a3648ac40c35f10cf882b Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 4 Sep 2014 12:43:22 -0400 Subject: no need to separate commands and replies as types it just clutters things --- src/client.rs | 20 ++----- src/constants.rs | 165 +++++++++++++++++++++++++------------------------------ src/message.rs | 21 +++---- 3 files changed, 85 insertions(+), 121 deletions(-) diff --git a/src/client.rs b/src/client.rs index 544283b..e66efb2 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,6 +1,6 @@ use std::{io, str}; -use constants::{CommandMessage, Nick, Pass, User}; +use constants::{Nick, Pass, User}; use message::Message; pub struct ClientBuilder { @@ -64,24 +64,12 @@ impl ClientBuilder { match self.pass { Some(ref pass) => { - client.write( - Message::new( - None, - CommandMessage(Pass), - vec![pass.clone()], - ) - ); + client.write(Message::new(None, Pass, vec![pass.clone()])); }, None => {}, } - client.write( - Message::new( - None, - CommandMessage(Nick), - vec![self.nick.clone()], - ) - ); + client.write(Message::new(None, Nick, vec![self.nick.clone()])); let hostname = match self.hostname { Some(ref host) => host.clone(), @@ -93,7 +81,7 @@ impl ClientBuilder { client.write( Message::new( None, - CommandMessage(User), + User, vec![ self.username.clone(), hostname, diff --git a/src/constants.rs b/src/constants.rs index 6fb6642..ff5e399 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -4,7 +4,7 @@ use std::fmt::{FormatError, Formatter, Show}; use std::from_str::FromStr; #[deriving(PartialEq, Eq)] -pub enum Command { +pub enum MessageType { Pass, Nick, User, @@ -45,10 +45,11 @@ pub enum Command { Wallops, Userhost, Ison, - Raw(String), + RawCommand(String), + Reply(u16), } -impl Show for Command { +impl Show for MessageType { fn fmt (&self, f: &mut Formatter) -> Result<(), FormatError> { match self { &Pass => try!(write!(f, "PASS")), @@ -91,57 +92,77 @@ impl Show for Command { &Wallops => try!(write!(f, "WALLOPS")), &Userhost => try!(write!(f, "USERHOST")), &Ison => try!(write!(f, "ISON")), - &Raw(ref s) => try!(write!(f, "{}", s)), + &RawCommand(ref s) => try!(write!(f, "{}", s)), + &Reply(i) => try!(write!(f, "{:03}", i)), } Ok(()) } } -impl FromStr for Command { - fn from_str (s: &str) -> Option { +impl FromStr for MessageType { + fn from_str (s: &str) -> Option { match s { - s if s == "PASS" => Some(Pass), - s if s == "NICK" => Some(Nick), - s if s == "USER" => Some(User), - s if s == "SERVER" => Some(Server), - s if s == "OPER" => Some(Oper), - s if s == "QUIT" => Some(Quit), - s if s == "SQUIT" => Some(Squit), - s if s == "JOIN" => Some(Join), - s if s == "PART" => Some(Part), - s if s == "MODE" => Some(Mode), - s if s == "TOPIC" => Some(Topic), - s if s == "NAMES" => Some(Names), - s if s == "LIST" => Some(List), - s if s == "INVITE" => Some(Invite), - s if s == "KICK" => Some(Kick), - s if s == "VERSION" => Some(Version), - s if s == "STATS" => Some(Stats), - s if s == "LINKS" => Some(Links), - s if s == "TIME" => Some(Time), - s if s == "CONNECT" => Some(Connect), - s if s == "TRACE" => Some(Trace), - s if s == "ADMIN" => Some(Admin), - s if s == "INFO" => Some(Info), - s if s == "PRIVMSG" => Some(Privmsg), - s if s == "NOTICE" => Some(Notice), - s if s == "WHO" => Some(Who), - s if s == "WHOIS" => Some(Whois), - s if s == "WHOWAS" => Some(Whowas), - s if s == "KILL" => Some(Kill), - s if s == "PING" => Some(Ping), - s if s == "PONG" => Some(Pong), - s if s == "ERROR" => Some(Error), - s if s == "AWAY" => Some(Away), - s if s == "REHASH" => Some(Rehash), - s if s == "RESTART" => Some(Restart), - s if s == "SUMMON" => Some(Summon), - s if s == "USERS" => Some(Users), - s if s == "WALLOPS" => Some(Wallops), - s if s == "USERHOST" => Some(Userhost), - s if s == "ISON" => Some(Ison), - s => Some(Raw(s.to_string())), + "PASS" => Some(Pass), + "NICK" => Some(Nick), + "USER" => Some(User), + "SERVER" => Some(Server), + "OPER" => Some(Oper), + "QUIT" => Some(Quit), + "SQUIT" => Some(Squit), + "JOIN" => Some(Join), + "PART" => Some(Part), + "MODE" => Some(Mode), + "TOPIC" => Some(Topic), + "NAMES" => Some(Names), + "LIST" => Some(List), + "INVITE" => Some(Invite), + "KICK" => Some(Kick), + "VERSION" => Some(Version), + "STATS" => Some(Stats), + "LINKS" => Some(Links), + "TIME" => Some(Time), + "CONNECT" => Some(Connect), + "TRACE" => Some(Trace), + "ADMIN" => Some(Admin), + "INFO" => Some(Info), + "PRIVMSG" => Some(Privmsg), + "NOTICE" => Some(Notice), + "WHO" => Some(Who), + "WHOIS" => Some(Whois), + "WHOWAS" => Some(Whowas), + "KILL" => Some(Kill), + "PING" => Some(Ping), + "PONG" => Some(Pong), + "ERROR" => Some(Error), + "AWAY" => Some(Away), + "REHASH" => Some(Rehash), + "RESTART" => Some(Restart), + "SUMMON" => Some(Summon), + "USERS" => Some(Users), + "WALLOPS" => Some(Wallops), + "USERHOST" => Some(Userhost), + "ISON" => Some(Ison), + s => { + match s.char_at(0) { + '0'..'9' => { + match from_str(s) { + Some(i) => Some(Reply(i)), + None => Some(RawCommand(s.to_string())), + } + }, + _ => Some(RawCommand(s.to_string())), + } + }, + } + } +} + +impl MessageType { + pub fn is_reply (&self) -> bool { + match self { + &Reply(_) => true, + _ => false, } } } @@ -316,49 +337,11 @@ pub static ERR_NOSERVICEHOST: u16 = 492; pub static RPL_TOPICDATE: u16 = 333; // date the topic was set, in seconds since the epoch pub static ERR_MSGFORBIDDEN: u16 = 505; // freenode blocking privmsg from unreged users -#[deriving(PartialEq, Eq)] -pub struct Reply(pub u16); - -impl Show for Reply { - fn fmt (&self, f: &mut Formatter) -> Result<(), FormatError> { - let Reply(u) = *self; - try!(write!(f, "{:03u}", u)); - Ok(()) - } -} - -impl FromStr for Reply { - fn from_str (s: &str) -> Option { - match from_str(s) { - Some(i) => Some(Reply(i)), - None => None, - } - } -} +pub static MAX_MESSAGE_LENGTH: i32 = 512; -#[deriving(PartialEq, Eq, Show)] -pub enum MessageType { - CommandMessage(Command), - ReplyMessage(Reply), +#[test] +fn test_message_type () { + assert!(!from_str::("PASS").unwrap().is_reply()); + assert!(from_str::("001").unwrap().is_reply()); + assert!(!from_str::("NOTACOMMAND").unwrap().is_reply()); } - -impl FromStr for MessageType { - fn from_str (s: &str) -> Option { - match s.char_at(0) { - '0' .. '9' => { - match from_str(s) { - Some(r) => Some(ReplyMessage(r)), - None => None, - } - }, - _ => { - match from_str(s) { - Some(c) => Some(CommandMessage(c)), - None => None, - } - }, - } - } -} - -pub static MAX_MESSAGE_LENGTH: i32 = 512; diff --git a/src/message.rs b/src/message.rs index bcbc226..9bd6b6f 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,4 +1,4 @@ -use constants::{CommandMessage, MessageType, ReplyMessage}; +use constants::MessageType; #[deriving(PartialEq, Eq, Show)] pub struct Message { @@ -49,14 +49,7 @@ impl Message { None => {}, } - match self.message_type { - CommandMessage(ref c) => { - write!(w, "{}", c); - }, - ReplyMessage(ref r) => { - write!(w, "{}", r); - }, - } + write!(w, "{}", self.message_type); for param in self.params.iter() { if param.as_slice().contains_char(' ') { @@ -112,7 +105,7 @@ fn test_message_parser () { Ok( Message { from: None, - message_type: CommandMessage(Pass), + message_type: Pass, params: vec!["secretpasswordhere".to_string()], } ) @@ -126,7 +119,7 @@ fn test_message_parser () { Ok( Message { from: Some("WiZ".to_string()), - message_type: CommandMessage(Nick), + message_type: Nick, params: vec!["Kilroy".to_string()], } ) @@ -140,7 +133,7 @@ fn test_message_parser () { Ok( Message { from: None, - message_type: CommandMessage(Quit), + message_type: Quit, params: vec!["Gone to have lunch".to_string()], } ) @@ -154,7 +147,7 @@ fn test_message_parser () { Ok( Message { from: Some("Trillian".to_string()), - message_type: CommandMessage(Squit), + message_type: Squit, params: vec![ "cm22.eng.umd.edu".to_string(), "Server out of control".to_string(), @@ -171,7 +164,7 @@ fn test_message_parser () { Ok( Message { from: None, - message_type: ReplyMessage(Reply(ERR_NOSUCHNICK)), + message_type: Reply(ERR_NOSUCHNICK), params: vec![ "doy".to_string(), "No such nick/channel".to_string(), -- cgit v1.2.3