twilight_model/channel/message/
activity.rsuse serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct MessageActivity {
#[serde(rename = "type")]
pub kind: MessageActivityType,
#[serde(skip_serializing_if = "Option::is_none")]
pub party_id: Option<String>,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum MessageActivityType {
Join,
Spectate,
Listen,
JoinRequest,
Unknown(u8),
}
impl From<u8> for MessageActivityType {
fn from(value: u8) -> Self {
match value {
1 => Self::Join,
2 => Self::Spectate,
3 => Self::Listen,
5 => Self::JoinRequest,
unknown => Self::Unknown(unknown),
}
}
}
impl From<MessageActivityType> for u8 {
fn from(value: MessageActivityType) -> Self {
match value {
MessageActivityType::Join => 1,
MessageActivityType::Spectate => 2,
MessageActivityType::Listen => 3,
MessageActivityType::JoinRequest => 5,
MessageActivityType::Unknown(unknown) => unknown,
}
}
}
#[cfg(test)]
mod tests {
use super::{MessageActivity, MessageActivityType};
use serde_test::Token;
#[test]
fn message_activity() {
let value = MessageActivity {
kind: MessageActivityType::Join,
party_id: None,
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "MessageActivity",
len: 1,
},
Token::Str("type"),
Token::U8(1),
Token::StructEnd,
],
);
}
#[test]
fn message_activity_complete() {
let value = MessageActivity {
kind: MessageActivityType::Join,
party_id: Some("test".to_owned()),
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "MessageActivity",
len: 2,
},
Token::Str("type"),
Token::U8(1),
Token::Str("party_id"),
Token::Some,
Token::Str("test"),
Token::StructEnd,
],
);
}
#[test]
fn variants() {
serde_test::assert_tokens(&MessageActivityType::Join, &[Token::U8(1)]);
serde_test::assert_tokens(&MessageActivityType::Spectate, &[Token::U8(2)]);
serde_test::assert_tokens(&MessageActivityType::Listen, &[Token::U8(3)]);
serde_test::assert_tokens(&MessageActivityType::JoinRequest, &[Token::U8(5)]);
serde_test::assert_tokens(&MessageActivityType::Unknown(99), &[Token::U8(99)]);
}
}