ServerIterator now iterates over IoResults.

This commit is contained in:
Aaron Weiss 2014-12-20 16:50:54 -05:00
parent fb743f148b
commit 70031aa8d8
5 changed files with 24 additions and 13 deletions

View file

@ -22,7 +22,7 @@ fn main() {
// We won't use a wrapper here because we don't need the added functionality. // We won't use a wrapper here because we don't need the added functionality.
spawn(move || { spawn(move || {
for msg in server.iter() { for msg in server.iter() {
print!("{}", msg.into_string()); print!("{}", msg.unwrap().into_string());
} }
}); });
} }

View file

@ -19,6 +19,7 @@ fn main() {
let server = Wrapper::new(&irc_server); let server = Wrapper::new(&irc_server);
server.identify().unwrap(); server.identify().unwrap();
for message in server.iter() { for message in server.iter() {
let message = message.unwrap();
print!("{}", message.into_string()); print!("{}", message.into_string());
if message.command[] == "PRIVMSG" { if message.command[] == "PRIVMSG" {
if let Some(msg) = message.suffix { if let Some(msg) = message.suffix {

View file

@ -20,6 +20,7 @@ fn main() {
let server = Wrapper::new(&irc_server); let server = Wrapper::new(&irc_server);
server.identify().unwrap(); server.identify().unwrap();
for message in server.iter() { for message in server.iter() {
let message = message.unwrap();
print!("{}", message.into_string()); print!("{}", message.into_string());
if message.command[] == "PRIVMSG" { if message.command[] == "PRIVMSG" {
if let Some(msg) = message.suffix { if let Some(msg) = message.suffix {

View file

@ -22,7 +22,7 @@ fn main() {
let server = Wrapper::new(&*irc_server2); let server = Wrapper::new(&*irc_server2);
server.identify().unwrap(); server.identify().unwrap();
// Let's set up a loop that just prints the messages. // Let's set up a loop that just prints the messages.
spawn(move || { irc_server.iter().map(|m| print!("{}", m.into_string())).count(); }); spawn(move || { irc_server.iter().map(|m| print!("{}", m.unwrap().into_string())).count(); });
loop { loop {
server.send_privmsg("#vana", "TWEET TWEET").unwrap(); server.send_privmsg("#vana", "TWEET TWEET").unwrap();
sleep(Duration::seconds(10)) sleep(Duration::seconds(10))

View file

@ -1,7 +1,7 @@
//! Interface for working with IRC Servers //! Interface for working with IRC Servers
#![experimental] #![experimental]
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{BufferedReader, BufferedWriter, IoResult}; use std::io::{BufferedReader, BufferedWriter, IoError, IoErrorKind, IoResult};
use std::sync::{Mutex, RWLock}; use std::sync::{Mutex, RWLock};
use conn::{Connection, NetStream}; use conn::{Connection, NetStream};
use data::{Command, Config, Message, Response, User}; use data::{Command, Config, Message, Response, User};
@ -217,15 +217,24 @@ impl<'a, T: IrcReader, U: IrcWriter> ServerIterator<'a, T, U> {
} }
} }
impl<'a, T: IrcReader, U: IrcWriter> Iterator<Message> for ServerIterator<'a, T, U> { impl<'a, T: IrcReader, U: IrcWriter> Iterator<IoResult<Message>> for ServerIterator<'a, T, U> {
fn next(&mut self) -> Option<Message> { fn next(&mut self) -> Option<IoResult<Message>> {
match self.get_next_line() { let res = self.get_next_line().and_then(|msg|
Err(_) => None, match from_str(msg[]) {
Ok(msg) => { Some(msg) => {
let message = from_str(msg[]); self.server.handle_message(&msg);
self.server.handle_message(message.as_ref().unwrap()); Ok(msg)
message },
None => Err(IoError {
kind: IoErrorKind::InvalidInput,
desc: "Failed to parse message.",
detail: Some(msg)
})
} }
);
match res {
Err(ref err) if err.kind == IoErrorKind::EndOfFile => None,
_ => Some(res)
} }
} }
} }
@ -265,7 +274,7 @@ mod test {
)); ));
let mut messages = String::new(); let mut messages = String::new();
for message in server.iter() { for message in server.iter() {
messages.push_str(message.into_string()[]); messages.push_str(message.unwrap().into_string()[]);
} }
assert_eq!(messages[], exp); assert_eq!(messages[], exp);
} }