use crate::twitch::{parse_badges, parse_emotes, Badge, BadgeInfo, Color, Emotes};
use crate::{irc::*, MaybeOwned, MaybeOwnedIndex, Validator};
#[derive(Clone, PartialEq)]
pub struct UserState<'a> {
raw: MaybeOwned<'a>,
tags: TagIndices,
channel: MaybeOwnedIndex,
}
impl<'a> UserState<'a> {
raw!();
tags!();
str_field!(
channel
);
pub fn badge_info(&self) -> Vec<BadgeInfo<'_>> {
self.tags()
.get("badge-info")
.map(parse_badges)
.unwrap_or_default()
}
pub fn badges(&self) -> Vec<Badge<'_>> {
self.tags()
.get("badges")
.map(parse_badges)
.unwrap_or_default()
}
pub fn color(&self) -> Option<Color> {
self.tags().get_parsed("color")
}
pub fn display_name(&self) -> Option<&str> {
self.tags().get("display-name")
}
pub fn emotes(&self) -> Vec<Emotes> {
self.tags()
.get("emotes")
.map(parse_emotes)
.unwrap_or_default()
}
pub fn is_moderator(&self) -> bool {
self.tags().get_as_bool("mod")
}
}
impl<'a> FromIrcMessage<'a> for UserState<'a> {
type Error = MessageError;
fn from_irc(msg: IrcMessage<'a>) -> Result<Self, Self::Error> {
msg.expect_command(IrcMessage::USER_STATE)?;
let this = Self {
tags: msg.parse_tags(),
channel: msg.expect_arg_index(0)?,
raw: msg.raw,
};
Ok(this)
}
into_inner_raw!();
}
into_owned!(UserState { raw, tags, channel });
impl_custom_debug!(UserState { raw, tags, channel });
serde_struct!(UserState { raw, tags, channel });
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(feature = "serde")]
fn user_state_serde() {
let input = "@badges=bits/1000;badge-info=moderator :tmi.twitch.tv USERSTATE #museun\r\n";
crate::serde::round_trip_json::<UserState>(input);
crate::serde::round_trip_rmp::<UserState>(input);
}
#[test]
fn user_state() {
let input = ":tmi.twitch.tv USERSTATE #museun\r\n";
for msg in parse(input).map(|s| s.unwrap()) {
let msg = UserState::from_irc(msg).unwrap();
assert_eq!(msg.channel(), "#museun");
}
}
}