Laid out basic structure of rewritten library.

This commit is contained in:
Aaron Weiss 2014-11-02 16:25:57 -05:00
parent 91aa5bcc6f
commit b2006d044d
9 changed files with 60 additions and 320 deletions

View file

@ -1,3 +1,7 @@
//! A simple, thread-safe IRC client library.
#![crate_name = "irc"]
#![crate_type = "lib"]
#![feature(if_let)]
#![feature(phase)]
#![feature(slicing_syntax)]
@ -5,66 +9,6 @@ extern crate regex;
#[phase(plugin)] extern crate regex_macros;
extern crate serialize;
use std::io::{InvalidInput, IoError, IoResult};
pub mod bot;
pub mod conn;
pub mod data;
pub trait Server<'a>: Iterator<data::Message> {
fn send(&self, message: data::Message) -> IoResult<()>;
fn config(&self) -> &data::Config;
fn get_users(&self, chan: &str) -> Option<Vec<data::User>>;
}
fn process(msg: &str) -> IoResult<(&str, &str, Vec<&str>)> {
let reg = regex!(r"^(?::([^ ]+) )?([^ ]+)(.*)");
let cap = match reg.captures(msg) {
Some(x) => x,
None => return Err(IoError {
kind: InvalidInput,
desc: "Failed to parse line",
detail: None,
}),
};
let source = cap.at(1);
let command = cap.at(2);
let args = parse_args(cap.at(3));
Ok((source, command, args))
}
fn parse_args(line: &str) -> Vec<&str> {
let reg = regex!(r" ([^: ]+)| :([^\r\n]*)[\r\n]*$");
reg.captures_iter(line).map(|cap| {
match cap.at(1) {
"" => cap.at(2),
x => x,
}
}).collect()
}
#[cfg(test)]
mod test {
use super::{process, parse_args};
#[test]
fn process_line() {
let res = process(":flare.to.ca.fyrechat.net 353 pickles = #pickles :pickles awe\r\n").unwrap();
let (source, command, args) = res;
assert_eq!(source, "flare.to.ca.fyrechat.net");
assert_eq!(command, "353");
assert_eq!(args, vec!["pickles", "=", "#pickles", "pickles awe"]);
let res = process("PING :flare.to.ca.fyrechat.net\r\n").unwrap();
let (source, command, args) = res;
assert_eq!(source, "");
assert_eq!(command, "PING");
assert_eq!(args, vec!["flare.to.ca.fyrechat.net"]);
}
#[test]
fn process_args() {
let res = parse_args("PRIVMSG #vana :hi");
assert_eq!(res, vec!["#vana", "hi"])
}
}
mod conn;
pub mod server;
mod utils;