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 std::io::{BufferedWriter, IoResult, TcpStream};
|
||||||
use data::Message;
|
use data::Message;
|
||||||
|
|
||||||
pub struct Connection(pub TcpStream);
|
pub enum Connection {
|
||||||
|
TcpConn(TcpStream),
|
||||||
|
}
|
||||||
|
|
||||||
pub fn connect(host: &str, port: u16) -> IoResult<Connection> {
|
pub fn connect(host: &str, port: u16) -> IoResult<Connection> {
|
||||||
let socket = try!(TcpStream::connect(host, port));
|
let socket = try!(TcpStream::connect(host, port));
|
||||||
Ok(Connection(socket))
|
Ok(TcpConn(socket))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_internal(conn: &Connection, msg: &str) -> IoResult<()> {
|
fn send_internal(conn: &Connection, msg: &str) -> IoResult<()> {
|
||||||
let &Connection(ref tcp) = conn;
|
match conn {
|
||||||
let mut writer = BufferedWriter::new(tcp.clone());
|
&TcpConn(ref tcp) => {
|
||||||
writer.write_str(msg)
|
let mut writer = BufferedWriter::new(tcp.clone());
|
||||||
|
writer.write_str(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(conn: &Connection, msg: Message) -> IoResult<()> {
|
pub fn send(conn: &Connection, msg: Message) -> IoResult<()> {
|
||||||
|
|
|
@ -7,7 +7,7 @@ use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::{BufferedReader, InvalidInput, IoError, IoResult};
|
use std::io::{BufferedReader, InvalidInput, IoError, IoResult};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
use conn::{Connection, connect, send};
|
use conn::{Connection, TcpConn, connect, send};
|
||||||
use data::{Config, Message};
|
use data::{Config, Message};
|
||||||
|
|
||||||
pub mod conn;
|
pub mod conn;
|
||||||
|
@ -66,7 +66,9 @@ impl<'a> Bot<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn output(&mut self) -> IoResult<()> {
|
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() {
|
for line in reader.lines() {
|
||||||
match line {
|
match line {
|
||||||
Ok(ln) => {
|
Ok(ln) => {
|
||||||
|
|
Loading…
Reference in a new issue