From dc8003c8e31766e005197336cf8b0ae7c6514769 Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Tue, 7 Oct 2014 14:30:38 -0400 Subject: [PATCH] Converted Connection into an enumeration for extensibility. --- src/conn.rs | 15 ++++++++++----- src/lib.rs | 6 ++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/conn.rs b/src/conn.rs index f8fbda7..b5093b8 100644 --- a/src/conn.rs +++ b/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 { 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<()> { diff --git a/src/lib.rs b/src/lib.rs index a50bdf4..3f19e0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) => {