Crates.io | ircv3_parse |
lib.rs | ircv3_parse |
version | 0.2.3 |
source | src |
created_at | 2024-02-29 09:22:49.935725 |
updated_at | 2024-10-11 13:10:46.051381 |
description | IRCv3 parse |
homepage | |
repository | https://github.com/m3idnotfree/ircv3_parse.git |
max_upload_size | |
id | 1157586 |
size | 40,672 |
implement IRCv3 message
RFC 1459, section 2.3.1
channel = "foo = #bar", "foo #bar", "#bar"
use ircv3_parse::IRCv3;
fn main(){
let msg = ":foo!foo@foo.tmi.abcdef.gh PRIVMSG #bar :LLLLLl";
let ircv3_message = IRCv3::parse(msg);
}
use ircv3_parse::{IRCv3Builder, ParamsParse};
use nom::{
branch::alt,
bytes::complete::{tag, take_until},
character::complete::{not_line_ending, space1},
sequence::tuple,
IResult,
};
let msg = ":foo!foo@foo.tmi.abcdef.gh PRIVMSG guest w :bleedPurple";
let result = IRCv3Builder::new(WhoAmI::default()).parse(msg);
assert_eq!("guest".to_string(), result.params.stats);
assert_eq!("w".to_string(), result.params.user);
#[derive(Default)]
struct WhoAmI {
pub stats: String,
pub user: String,
}
impl ParamsParse for WhoAmI {
fn parse(&self, _: &str, middle: ircv3_parse::IRCv3ParamsBase) -> Self
where
Self: Sized,
{
let join_middle = middle.middle.join(" ");
let (_, (who, user)) = whoami(join_middle.as_str()).unwrap();
WhoAmI {
stats: who.to_string(),
user: user.to_string(),
}
}
}
pub fn whoami(msg: &str) -> IResult<&str, (&str, &str)> {
let (remain, who) = alt((owner_user, guest_user))(msg)?;
Ok((remain, who))
}
fn owner_user(msg: &str) -> IResult<&str, (&str, &str)> {
let (remain, (gust, _, user)) = tuple((
tag("owner"),
space1,
alt((take_until(" "), not_line_ending)),
))(msg)?;
Ok((remain, (gust, user)))
}
fn guest_user(msg: &str) -> IResult<&str, (&str, &str)> {
let (remain, (gust, _, user)) = tuple((
tag("guest"),
space1,
alt((take_until(" "), not_line_ending)),
))(msg)?;
Ok((remain, (gust, user)))
}