3个版本
0.1.2 | 2023年1月26日 |
---|---|
0.1.1 | 2023年1月24日 |
0.1.0 | 2023年1月24日 |
#13 in #tv
34 每月下载量
180KB
4K SLoC
twitch_message
阅读文档获取更多信息
这是一个用于解析https://www.twitch.tv聊天消息的crate
此crate不提供任何I/O功能,仅提供将&str
解析为类型化消息的功能。
快速入门
use twitch_message::messages::*;
// get some data from somewhere
let data: &str = read_line();
// parse returns a `ParseResult` which contains the remaining data (if any) and the parsed message
let result = twitch_message::parse(data)?;
let msg: Message<'_> = result.message;
match msg.kind {
MessageKind::Ready => {
let ready = msg.as_typed_message::<Ready>().unwrap();
println!("connected as: {name}", name = ready.name);
}
MessageKind::Privmsg => {
let pm = msg.as_typed_message::<Privmsg>().unwrap();
println!("[{channel}] {sender}: {data}",
channel = pm.channel,
sender = pm.sender,
data = pm.data
);
}
MessageKind::Ping => {
let ping = msg.as_typed_message::<Ping>().unwrap();
let resp = twitch_message::encode::pong(&ping.token);
// you can format data to various 'sinks'
use twitch_message::encode::Formattable;
let mut out = String::new();
resp.format(&mut out)?;
assert_eq!(out, "PONG :1234567890\r\n");
}
_ => {}
}
编码
格式/可格式化
// this adds the # to the channel, if its missing
let pm = twitch_message::encode::privmsg("museun", "hello, world.");
// using `Formattable`
use twitch_message::encode::Formattable;
let mut buf = String::new();
pm.format(&mut buf)?;
assert_eq!(buf, "PRIVMSG #museun :hello, world.\r\n");
// using `Format`
use twitch_message::encode::Format;
let mut buf = String::new();
buf.format_msg(pm)?;
assert_eq!(buf, "PRIVMSG #museun :hello, world.\r\n");
编码/可编码
// this adds the # to the channel, if its missing
let pm = twitch_message::encode::privmsg("museun", "hello, world.");
// using `Encodable`
use twitch_message::encode::Encodable;
let mut buf = Vec::new();
pm.encode(&mut buf)?;
assert_eq!(buf, b"PRIVMSG #museun :hello, world.\r\n");
// using `Encode`
use twitch_message::encode::Encode;
let mut buf = Vec::new();
buf.encode_msg(pm)?;
assert_eq!(buf, b"PRIVMSG #museun :hello, world.\r\n");
功能
功能 | 描述 |
---|---|
默认 | 没有默认功能 |
ping | 启用PingTracker |
std | 启用Encode 和Encodable 特性 |
serde | 启用类型上的serde 特性 |
hashbrown | 启用使用hashbrown 的内部HashMap |
sync | 启用使用std::sync::Mutex 来覆盖std::cell::RefCell ,参见共享数据 |
parking_lot | 与sync 相同,但使用parking_lot::Mutex |
Twitch聊天参考:链接
许可证:0BSD
依赖项
~0–5.5MB
~11K SLoC