Added identify(...) utility, and clarified intent in some places using match.

This commit is contained in:
Aaron Weiss 2014-11-03 02:02:29 -05:00
parent 4d7c2065e9
commit 42b4dcbf03
3 changed files with 21 additions and 7 deletions

View file

@ -70,11 +70,12 @@ impl FromStr for Message {
} else { } else {
None None
}; };
let command = if let Some(cmd) = state.find(' ').map(|i| s[..i]) { let command = match state.find(' ').map(|i| s[..i]) {
Some(cmd) => {
state = state.find(' ').map_or("", |i| s[i..]); state = state.find(' ').map_or("", |i| s[i..]);
cmd cmd
} else { }
return None _ => return None
}; };
let args: Vec<_> = state.splitn(14, ' ').collect(); let args: Vec<_> = state.splitn(14, ' ').collect();
Some(Message::new(prefix, command, if args.len() > 0 { Some(args) } else { None }, suffix)) Some(Message::new(prefix, command, if args.len() > 0 { Some(args) } else { None }, suffix))

View file

@ -86,7 +86,9 @@ impl<'a, T, U> ServerIterator<'a, T, U> where T: IrcWriter, U: IrcReader {
impl<'a, T, U> Iterator<Message> for ServerIterator<'a, T, U> where T: IrcWriter, U: IrcReader { impl<'a, T, U> Iterator<Message> for ServerIterator<'a, T, U> where T: IrcWriter, U: IrcReader {
fn next(&mut self) -> Option<Message> { fn next(&mut self) -> Option<Message> {
let line = self.server.conn.recv(); let line = self.server.conn.recv();
if let Err(_) = line { return None } match line {
from_str(line.unwrap()[]) Err(_) => None,
Ok(msg) => from_str(msg[])
}
} }
} }

View file

@ -1,2 +1,13 @@
//! Utilities and shortcuts for working with IRC servers //! Utilities and shortcuts for working with IRC servers
#![experimental] #![experimental]
use std::io::IoResult;
use data::command::{NICK, USER};
use data::kinds::{IrcReader, IrcWriter};
use server::Server;
/// Sends a NICK and USER to identify
pub fn identify<'a, T, U>(server: &Server<'a, T, U>) -> IoResult<()> where T: IrcWriter, U: IrcReader {
try!(server.send(NICK(server.config().nickname[])));
server.send(USER(server.config().username[], "0", server.config().realname[]))
}