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
15
src/conn.rs
15
src/conn.rs
|
@ -1,17 +1,22 @@
|
|||
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;
|
||||
let mut writer = BufferedWriter::new(tcp.clone());
|
||||
writer.write_str(msg)
|
||||
match conn {
|
||||
&TcpConn(ref tcp) => {
|
||||
let mut writer = BufferedWriter::new(tcp.clone());
|
||||
writer.write_str(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(conn: &Connection, msg: Message) -> IoResult<()> {
|
||||
|
|
|
@ -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