Converted Connection into an enumeration for extensibility.
This commit is contained in:
parent
394ca6bb8f
commit
dc8003c8e3
2 changed files with 14 additions and 7 deletions
11
src/conn.rs
11
src/conn.rs
|
@ -1,18 +1,23 @@
|
|||
use std::io::{BufferedWriter, IoResult, TcpStream};
|
||||
use data::Message;
|
||||
|
||||
pub struct Connection(pub TcpStream);
|
||||
pub enum Connection {
|
||||
TcpConn(TcpStream),
|
||||
}
|
||||
|
||||
pub fn connect(host: &str, port: u16) -> IoResult<Connection> {
|
||||
let socket = try!(TcpStream::connect(host, port));
|
||||
Ok(Connection(socket))
|
||||
Ok(TcpConn(socket))
|
||||
}
|
||||
|
||||
fn send_internal(conn: &Connection, msg: &str) -> IoResult<()> {
|
||||
let &Connection(ref tcp) = conn;
|
||||
match conn {
|
||||
&TcpConn(ref tcp) => {
|
||||
let mut writer = BufferedWriter::new(tcp.clone());
|
||||
writer.write_str(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(conn: &Connection, msg: Message) -> IoResult<()> {
|
||||
let mut send = msg.command.to_string();
|
||||
|
|
|
@ -7,7 +7,7 @@ use std::cell::RefCell;
|
|||
use std::collections::HashMap;
|
||||
use std::io::{BufferedReader, InvalidInput, IoError, IoResult};
|
||||
use std::vec::Vec;
|
||||
use conn::{Connection, connect, send};
|
||||
use conn::{Connection, TcpConn, connect, send};
|
||||
use data::{Config, Message};
|
||||
|
||||
pub mod conn;
|
||||
|
@ -66,7 +66,9 @@ impl<'a> Bot<'a> {
|
|||
}
|
||||
|
||||
pub fn output(&mut self) -> IoResult<()> {
|
||||
let mut reader = { let Connection(ref tcp) = self.conn; BufferedReader::new(tcp.clone()) };
|
||||
let mut reader = match self.conn {
|
||||
TcpConn(ref tcp) => BufferedReader::new(tcp.clone()),
|
||||
};
|
||||
for line in reader.lines() {
|
||||
match line {
|
||||
Ok(ln) => {
|
||||
|
|
Loading…
Reference in a new issue