Merge pull request #11 from SBSTP/tomessage
[API Improvement] Implement ToMessage for Command and Message and use ToMessage in Connection
This commit is contained in:
commit
202748e7a9
4 changed files with 63 additions and 40 deletions
|
@ -6,7 +6,7 @@ use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream};
|
|||
#[cfg(feature = "encode")] use encoding::{DecoderTrap, EncoderTrap, Encoding};
|
||||
#[cfg(feature = "encode")] use encoding::label::encoding_from_whatwg_label;
|
||||
use data::kinds::{IrcReader, IrcWriter};
|
||||
use data::message::Message;
|
||||
use data::message::ToMessage;
|
||||
#[cfg(feature = "ssl")] use openssl::ssl::{SslContext, SslMethod, SslStream};
|
||||
#[cfg(feature = "ssl")] use openssl::ssl::error::SslError;
|
||||
|
||||
|
@ -114,7 +114,8 @@ impl<T: IrcReader, U: IrcWriter> Connection<T, U> {
|
|||
/// Sends a Message over this connection.
|
||||
#[experimental]
|
||||
#[cfg(feature = "encode")]
|
||||
pub fn send(&self, message: Message, encoding: &str) -> IoResult<()> {
|
||||
pub fn send<T: ToMessage>(&self, tomsg: T, encoding: &str) -> IoResult<()> {
|
||||
let message = tomsg.to_message();
|
||||
let encoding = match encoding_from_whatwg_label(encoding) {
|
||||
Some(enc) => enc,
|
||||
None => return Err(IoError {
|
||||
|
@ -139,7 +140,8 @@ impl<T: IrcReader, U: IrcWriter> Connection<T, U> {
|
|||
/// Sends a message over this connection.
|
||||
#[experimental]
|
||||
#[cfg(not(feature = "encode"))]
|
||||
pub fn send(&self, message: Message) -> IoResult<()> {
|
||||
pub fn send<T: ToMessage>(&self, tomsg: T) -> IoResult<()> {
|
||||
let message = tomsg.to_message();
|
||||
let mut writer = self.writer.lock();
|
||||
try!(writer.write_str(message.into_string()[]));
|
||||
writer.flush()
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#![stable]
|
||||
use std::io::{InvalidInput, IoError, IoResult};
|
||||
use std::str::FromStr;
|
||||
use data::message::Message;
|
||||
use data::message::{Message, ToMessage};
|
||||
|
||||
/// List of all client commands as defined in [RFC 2812](http://tools.ietf.org/html/rfc2812). This
|
||||
/// also includes commands from the
|
||||
|
@ -150,11 +150,12 @@ pub enum Command<'a> {
|
|||
CAP(CapSubCommand, Option<&'a str>),
|
||||
}
|
||||
|
||||
impl<'a> Command<'a> {
|
||||
impl<'a> ToMessage for Command<'a> {
|
||||
|
||||
/// Converts a Command into a Message.
|
||||
#[stable]
|
||||
pub fn to_message(self) -> Message {
|
||||
match self {
|
||||
fn to_message(&self) -> Message {
|
||||
match *self {
|
||||
Command::PASS(p) => Message::new(None, "PASS", None, Some(p)),
|
||||
Command::NICK(n) => Message::new(None, "NICK", None, Some(n)),
|
||||
Command::USER(u, m, r) => Message::new(None, "USER", Some(vec![u, m, "*"]), Some(r)),
|
||||
|
@ -243,8 +244,8 @@ impl<'a> Command<'a> {
|
|||
Command::USERS(Some(t)) => Message::new(None, "USERS", None, Some(t)),
|
||||
Command::USERS(None) => Message::new(None, "USERS", None, None),
|
||||
Command::WALLOPS(t) => Message::new(None, "WALLOPS", None, Some(t)),
|
||||
Command::USERHOST(u) => Message::new(None, "USERHOST", Some(u), None),
|
||||
Command::ISON(u) => Message::new(None, "ISON", Some(u), None),
|
||||
Command::USERHOST(ref u) => Message::new(None, "USERHOST", Some(u.clone()), None),
|
||||
Command::ISON(ref u) => Message::new(None, "ISON", Some(u.clone()), None),
|
||||
Command::SAJOIN(n, c) => Message::new(None, "SAJOIN", Some(vec![n, c]), None),
|
||||
Command::SAMODE(t, m, Some(p)) => Message::new(None, "SAMODE", Some(vec![t, m, p]),
|
||||
None),
|
||||
|
@ -262,6 +263,9 @@ impl<'a> Command<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<'a> Command<'a> {
|
||||
/// Converts a Message into a Command.
|
||||
#[stable]
|
||||
pub fn from_message(m: &'a Message) -> IoResult<Command<'a>> {
|
||||
|
|
|
@ -3,6 +3,14 @@
|
|||
use std::borrow::ToOwned;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// Represents something that can be converted to a message.
|
||||
pub trait ToMessage {
|
||||
|
||||
/// Convert to message.
|
||||
fn to_message(&self) -> Message;
|
||||
|
||||
}
|
||||
|
||||
/// IRC Message data.
|
||||
#[experimental]
|
||||
#[deriving(Clone, PartialEq, Show)]
|
||||
|
@ -54,6 +62,14 @@ impl Message {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToMessage for Message {
|
||||
|
||||
fn to_message(&self) -> Message {
|
||||
self.clone()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl FromStr for Message {
|
||||
fn from_str(s: &str) -> Option<Message> {
|
||||
let mut state = s.clone();
|
||||
|
|
|
@ -8,6 +8,7 @@ use conn::{Connection, NetStream};
|
|||
use data::{Command, Config, Message, Response, User};
|
||||
use data::Command::{JOIN, NICK, NICKSERV, PONG};
|
||||
use data::kinds::{IrcReader, IrcWriter};
|
||||
use data::message::ToMessage;
|
||||
#[cfg(feature = "ctcp")] use time::now;
|
||||
|
||||
pub mod utils;
|
||||
|
|
Loading…
Reference in a new issue