Initial commit
This commit is contained in:
commit
5e85202dd5
5 changed files with 166 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
|
||||
/target
|
||||
**/*.rs.bk
|
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
|
@ -0,0 +1,4 @@
|
|||
[[package]]
|
||||
name = "plankircd"
|
||||
version = "0.1.0"
|
||||
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "plankircd"
|
||||
version = "0.1.0"
|
||||
authors = ["Adrian Hedqvist <adrian@tollyx.net>"]
|
||||
|
||||
[dependencies]
|
||||
tokio = "0.1"
|
5
src/main.rs
Normal file
5
src/main.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
pub mod models;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
147
src/models.rs
Normal file
147
src/models.rs
Normal file
|
@ -0,0 +1,147 @@
|
|||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum IrcCommand {
|
||||
USER,
|
||||
NICK,
|
||||
JOIN,
|
||||
PART,
|
||||
PRIVMSG,
|
||||
WHO,
|
||||
}
|
||||
|
||||
impl IrcCommand {
|
||||
fn from_str(s: &str) -> Option<IrcCommand> {
|
||||
use self::IrcCommand::*;
|
||||
match s {
|
||||
"USER" => Some(USER),
|
||||
"NICK" => Some(NICK),
|
||||
"JOIN" => Some(JOIN),
|
||||
"PART" => Some(PART),
|
||||
"PRIVMSG" => Some(PRIVMSG),
|
||||
"WHO" => Some(WHO),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct IrcMessage {
|
||||
pub prefix: Option<String>,
|
||||
pub command: IrcCommand,
|
||||
pub params: Vec<String>,
|
||||
}
|
||||
|
||||
impl IrcMessage {
|
||||
pub fn new(pre: Option<&str>, cmd: IrcCommand, prm: Vec<&str>) -> IrcMessage {
|
||||
IrcMessage {
|
||||
prefix: if let Some(s) = pre { Some(s.to_string()) } else { None },
|
||||
command: cmd,
|
||||
params: prm.iter().map(|s| s.to_string()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_str(msg: &str) -> Option<IrcMessage> { // TODO: Wrap in Result<>
|
||||
let mut args = msg.trim().splitn(2, " :");
|
||||
let mut left = args.next().unwrap().split(' ');
|
||||
|
||||
let mut prefix = None;
|
||||
let mut cmd = None;
|
||||
|
||||
if let Some(s) = left.next() {
|
||||
if s.starts_with(':') {
|
||||
prefix = Some(&s[1..]);
|
||||
|
||||
if let Some(c) = left.next() {
|
||||
cmd = IrcCommand::from_str(c);
|
||||
};
|
||||
}
|
||||
else {
|
||||
cmd = IrcCommand::from_str(s);
|
||||
}
|
||||
}
|
||||
|
||||
let mut params: Vec<&str> = left.collect();
|
||||
|
||||
if let Some(s) = args.next() {
|
||||
params.push(s);
|
||||
};
|
||||
|
||||
match cmd {
|
||||
Some(c) => Some(IrcMessage::new(prefix, c, params)),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
let param: Vec<String> = self.params.iter()
|
||||
.map(|s| if s.contains(" ") { format!(":{}", s) } else { s.clone() })
|
||||
.collect();
|
||||
// TODO: get rid of clone, also would be better to only check and (...)
|
||||
// replace the last element since we assume that the preceding params
|
||||
// doesn't have any spaces
|
||||
|
||||
match self.prefix {
|
||||
Some(ref prefix) => format!(":{} {:?} {}\r\n", prefix, self.command, param.join(" ")),
|
||||
None => format!("{:?} {}\r\n", self.command, param.join(" "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn message_from_string() {
|
||||
let msg = IrcMessage::from_str(":tlx PRIVMSG supes :u suk lol\r\n").unwrap();
|
||||
|
||||
assert_eq!(msg, IrcMessage {
|
||||
prefix: Some("tlx".to_string()),
|
||||
command: IrcCommand::PRIVMSG,
|
||||
params: vec!["supes".to_string(), "u suk lol".to_string()]
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_from_string_with_multiple_colons_in_trailing() {
|
||||
let msg = IrcMessage::from_str(":tlx PRIVMSG supes :u suk lol :D\r\n").unwrap();
|
||||
|
||||
assert_eq!(msg, IrcMessage {
|
||||
prefix: Some("tlx".to_string()),
|
||||
command: IrcCommand::PRIVMSG,
|
||||
params: vec!["supes".to_string(), "u suk lol :D".to_string()]
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_from_string_without_prefix() {
|
||||
let msg = IrcMessage::from_str("PRIVMSG supes :u suk lol\r\n").unwrap();
|
||||
|
||||
assert_eq!(msg, IrcMessage {
|
||||
prefix: None,
|
||||
command: IrcCommand::PRIVMSG,
|
||||
params: vec!["supes".to_string(), "u suk lol".to_string()]
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_to_string() {
|
||||
let msg = IrcMessage {
|
||||
prefix: Some("tlx".to_string()),
|
||||
command: IrcCommand::PRIVMSG,
|
||||
params: vec!["supes".to_string(), "u suk lol".to_string()]
|
||||
};
|
||||
|
||||
assert_eq!(&msg.to_string(), ":tlx PRIVMSG supes :u suk lol\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_to_string_without_prefix() {
|
||||
let msg = IrcMessage {
|
||||
prefix: None,
|
||||
command: IrcCommand::PRIVMSG,
|
||||
params: vec!["supes".to_string(), "u suk lol".to_string()]
|
||||
};
|
||||
|
||||
assert_eq!(&msg.to_string(), "PRIVMSG supes :u suk lol\r\n");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue