17 releases
0.0.17 | Jun 22, 2024 |
---|---|
0.0.16 | Jun 15, 2024 |
#2258 in Parser implementations
29KB
668 lines
irc-command-micro
Early implementation of a parser for irc messages
Parse a simple NAMES command, CR and LF is optional
use irc_command_micro::Message;
let message_bytes = vec![58, 65, 110, 111, 110, 32, 78, 65, 77, 69, 83];
let message = Message::parse(message_bytes, None).unwrap();
println!("{:?}", message);
//Message { source: Some("Anon"), command: NAMES, params: [] }
Parse a NICK command that contains a single parameter
let message_bytes = vec![58,65,110,111,110,32,78,73,67,75,32,68,97,118,101];
let message = Message::parse(message_bytes, None).unwrap();
println!("{:?}", message);
// Message { source: Some("Anon"), command: NICK, params: ["Dave"] }
Here is the same NICK command but without the provided parameter
let message_bytes = vec![58,65,110,111,110,32,78,73,67,75];
let message = Message::parse(message_bytes, None);
println!("{:?}", message);
// Err(MessageError { detail: NotEnoughParams })
Now the client has sent a NICK command but not capitalised correctly (Nick)
let message_bytes = vec![58,65,110,111,110,32,78,105,99,107,32,68,97,118,101];
let message = Message::parse(message_bytes, None);
println!("{:?}", message);
// Err(MessageError { detail: InvalidCommand })
Whitespace will also be stripped from the start and end
let message_bytes = vec![32, 32, 32, 32, 32, 32, 58, 65, 110, 111, 110, 32, 78, 73, 67, 75, 32, 65, 110, 111, 110, 50, 32, 32, 32, 32, 32, 13, 10];
let message = Message::parse(message_bytes, None);
println!("{:?}", message);
// Message { source: Some("Anon"), command: NICK, params: ["Anon2"] }
You can also construct a Message
manually
let message = Message::new(Some("Anon"), IRCCommand::NAMES, Vec::new())
A message object can then be returned to a value to be transmitted, CR and LF is added making exact return value valid
let tcp_data: Vec<u8> = message.serialize()
// [58, 65, 110, 111, 110, 32, 78, 65, 77, 69, 83, 13, 10]