Replaced ToMessage with Into<Message>.

This commit is contained in:
Aaron Weiss 2015-04-26 00:01:33 -04:00
parent d3ac72d513
commit 87f3b65649
6 changed files with 17 additions and 29 deletions

View file

@ -9,8 +9,8 @@ use std::net::TcpStream;
use std::sync::{Mutex, MutexGuard}; use std::sync::{Mutex, MutexGuard};
#[cfg(feature = "encode")] use encoding::{DecoderTrap, EncoderTrap, Encoding}; #[cfg(feature = "encode")] use encoding::{DecoderTrap, EncoderTrap, Encoding};
#[cfg(feature = "encode")] use encoding::label::encoding_from_whatwg_label; #[cfg(feature = "encode")] use encoding::label::encoding_from_whatwg_label;
use client::data::Message;
use client::data::kinds::{IrcRead, IrcWrite}; use client::data::kinds::{IrcRead, IrcWrite};
use client::data::message::ToMessage;
#[cfg(feature = "ssl")] use openssl::ssl::{SslContext, SslMethod, SslStream}; #[cfg(feature = "ssl")] use openssl::ssl::{SslContext, SslMethod, SslStream};
#[cfg(feature = "ssl")] use openssl::ssl::error::SslError; #[cfg(feature = "ssl")] use openssl::ssl::error::SslError;
@ -110,14 +110,14 @@ impl<T: IrcRead, U: IrcWrite> Connection<T, U> {
/// Sends a Message over this connection. /// Sends a Message over this connection.
#[cfg(feature = "encode")] #[cfg(feature = "encode")]
pub fn send<M: ToMessage>(&self, to_msg: M, encoding: &str) -> Result<()> { pub fn send<M: Into<Message>>(&self, to_msg: M, encoding: &str) -> Result<()> {
let encoding = match encoding_from_whatwg_label(encoding) { let encoding = match encoding_from_whatwg_label(encoding) {
Some(enc) => enc, Some(enc) => enc,
None => return Err(Error::new( None => return Err(Error::new(
ErrorKind::InvalidInput, &format!("Failed to find encoder. ({})", encoding)[..] ErrorKind::InvalidInput, &format!("Failed to find encoder. ({})", encoding)[..]
)) ))
}; };
let msg = to_msg.to_message(); let msg: Message = to_msg.into();
let data = match encoding.encode(&msg.into_string(), EncoderTrap::Replace) { let data = match encoding.encode(&msg.into_string(), EncoderTrap::Replace) {
Ok(data) => data, Ok(data) => data,
Err(data) => return Err(Error::new(ErrorKind::InvalidInput, Err(data) => return Err(Error::new(ErrorKind::InvalidInput,
@ -131,9 +131,9 @@ impl<T: IrcRead, U: IrcWrite> Connection<T, U> {
/// Sends a message over this connection. /// Sends a message over this connection.
#[cfg(not(feature = "encode"))] #[cfg(not(feature = "encode"))]
pub fn send<M: ToMessage>(&self, to_msg: M) -> Result<()> { pub fn send<M: Into<Message>>(&self, to_msg: M) -> Result<()> {
let mut writer = self.writer.lock().unwrap(); let mut writer = self.writer.lock().unwrap();
try!(writer.write_all(&to_msg.to_message().into_string().as_bytes())); try!(writer.write_all(&to_msg.into::<Message>().into_string().as_bytes()));
writer.flush() writer.flush()
} }

View file

@ -2,7 +2,7 @@
use std::io::{Error, ErrorKind, Result}; use std::io::{Error, ErrorKind, Result};
use std::result::Result as StdResult; use std::result::Result as StdResult;
use std::str::FromStr; use std::str::FromStr;
use client::data::message::{Message, ToMessage}; use client::data::Message;
/// List of all client commands as defined in [RFC 2812](http://tools.ietf.org/html/rfc2812). This /// List of all client commands as defined in [RFC 2812](http://tools.ietf.org/html/rfc2812). This
/// also includes commands from the /// also includes commands from the
@ -149,9 +149,9 @@ pub enum Command {
CAP(Option<String>, CapSubCommand, Option<String>, Option<String>), CAP(Option<String>, CapSubCommand, Option<String>, Option<String>),
} }
impl ToMessage for Command { impl Into<Message> for Command {
/// Converts a Command into a Message. /// Converts a Command into a Message.
fn to_message(self) -> Message { fn into(self) -> Message {
match self { match self {
Command::PASS(p) => Message::from_owned(None, string("PASS"), None, Some(p)), Command::PASS(p) => Message::from_owned(None, string("PASS"), None, Some(p)),
Command::NICK(n) => Message::from_owned(None, string("NICK"), None, Some(n)), Command::NICK(n) => Message::from_owned(None, string("NICK"), None, Some(n)),

View file

@ -63,12 +63,6 @@ impl Message {
} }
} }
impl ToMessage for Message {
fn to_message(self) -> Message {
self
}
}
impl FromStr for Message { impl FromStr for Message {
type Err = &'static str; type Err = &'static str;
fn from_str(s: &str) -> Result<Message, &'static str> { fn from_str(s: &str) -> Result<Message, &'static str> {
@ -101,15 +95,9 @@ impl FromStr for Message {
} }
} }
/// A trait representing the ability to be converted into a Message. impl<'a> From<&'a str> for Message {
pub trait ToMessage { fn from(s: &'a str) -> Message {
/// Converts this to a Message. s.parse().unwrap()
fn to_message(self) -> Message;
}
impl<'a> ToMessage for &'a str {
fn to_message(self) -> Message {
self.parse().unwrap()
} }
} }

View file

@ -2,7 +2,7 @@
pub use client::data::command::Command; pub use client::data::command::Command;
pub use client::data::config::Config; pub use client::data::config::Config;
pub use client::data::message::{Message, ToMessage}; pub use client::data::message::Message;
pub use client::data::response::Response; pub use client::data::response::Response;
pub use client::data::user::{AccessLevel, User}; pub use client::data::user::{AccessLevel, User};

View file

@ -8,7 +8,7 @@ pub mod prelude {
//! A client-side IRC prelude, re-exporting all the necessary basics. //! A client-side IRC prelude, re-exporting all the necessary basics.
pub use client::server::{IrcServer, Server}; pub use client::server::{IrcServer, Server};
pub use client::server::utils::ServerExt; pub use client::server::utils::ServerExt;
pub use client::data::{Command, Config, Message, Response, ToMessage}; pub use client::data::{Command, Config, Message, Response};
pub use client::data::kinds::{IrcRead, IrcWrite}; pub use client::data::kinds::{IrcRead, IrcWrite};
} }

View file

@ -8,7 +8,7 @@ use std::io::{BufReader, BufWriter, Error, ErrorKind, Result};
use std::sync::{Mutex, RwLock}; use std::sync::{Mutex, RwLock};
use std::iter::Map; use std::iter::Map;
use client::conn::{Connection, NetStream}; use client::conn::{Connection, NetStream};
use client::data::{Command, Config, Message, Response, ToMessage, User}; use client::data::{Command, Config, Message, Response, User};
use client::data::Command::{JOIN, NICK, NICKSERV, PONG, MODE}; use client::data::Command::{JOIN, NICK, NICKSERV, PONG, MODE};
use client::data::kinds::{IrcRead, IrcWrite}; use client::data::kinds::{IrcRead, IrcWrite};
#[cfg(feature = "ctcp")] use time::now; #[cfg(feature = "ctcp")] use time::now;
@ -20,7 +20,7 @@ pub trait Server<'a, T, U> {
/// Gets the configuration being used with this Server. /// Gets the configuration being used with this Server.
fn config(&self) -> &Config; fn config(&self) -> &Config;
/// Sends a Command to this Server. /// Sends a Command to this Server.
fn send<M: ToMessage>(&self, message: M) -> Result<()>; fn send<M: Into<Message>>(&self, message: M) -> Result<()>;
/// Gets an Iterator over Messages received by this Server. /// Gets an Iterator over Messages received by this Server.
fn iter(&'a self) -> ServerIterator<'a, T, U>; fn iter(&'a self) -> ServerIterator<'a, T, U>;
/// Gets an Iterator over Commands received by this Server. /// Gets an Iterator over Commands received by this Server.
@ -76,12 +76,12 @@ impl<'a, T: IrcRead, U: IrcWrite> Server<'a, T, U> for IrcServer<T, U> {
} }
#[cfg(feature = "encode")] #[cfg(feature = "encode")]
fn send<M: ToMessage>(&self, msg: M) -> Result<()> { fn send<M: Into<Message>>(&self, msg: M) -> Result<()> {
self.conn.send(msg, self.config.encoding()) self.conn.send(msg, self.config.encoding())
} }
#[cfg(not(feature = "encode"))] #[cfg(not(feature = "encode"))]
fn send<M: ToMessage>(&self, msg: M) -> Result<()> { fn send<M: Into<Message>>(&self, msg: M) -> Result<()> {
self.conn.send(msg) self.conn.send(msg)
} }