#message-parser #irc #message #validation #parser #library #irc-client

irc-command-micro

A library to parse irc messages and provide a stable representation in return

17 releases

0.0.17 Jun 22, 2024
0.0.16 Jun 15, 2024

#2411 in Parser implementations

Download history 5/week @ 2024-09-10 1/week @ 2024-09-17 2/week @ 2024-09-24 1/week @ 2024-10-01

1,111 downloads per month

MIT license

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]

No runtime deps