twilight_model/guild/scheduled_event/
mod.rsmod user;
pub use self::user::GuildScheduledEventUser;
use crate::{
id::{
marker::{
ChannelMarker, GuildMarker, ScheduledEventEntityMarker, ScheduledEventMarker,
UserMarker,
},
Id,
},
user::User,
util::{ImageHash, Timestamp},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct GuildScheduledEvent {
#[serde(skip_serializing_if = "Option::is_none")]
pub channel_id: Option<Id<ChannelMarker>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub creator: Option<User>,
#[serde(skip_serializing_if = "Option::is_none")]
pub creator_id: Option<Id<UserMarker>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub entity_id: Option<Id<ScheduledEventEntityMarker>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub entity_metadata: Option<EntityMetadata>,
pub entity_type: EntityType,
pub guild_id: Id<GuildMarker>,
pub id: Id<ScheduledEventMarker>,
#[serde(skip_serializing_if = "Option::is_none")]
pub image: Option<ImageHash>,
pub name: String,
pub privacy_level: PrivacyLevel,
#[serde(skip_serializing_if = "Option::is_none")]
pub scheduled_end_time: Option<Timestamp>,
pub scheduled_start_time: Timestamp,
pub status: Status,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_count: Option<u64>,
}
#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct EntityMetadata {
pub location: Option<String>,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum EntityType {
StageInstance,
Voice,
External,
Unknown(u8),
}
impl From<u8> for EntityType {
fn from(value: u8) -> Self {
match value {
1 => EntityType::StageInstance,
2 => EntityType::Voice,
3 => EntityType::External,
unknown => EntityType::Unknown(unknown),
}
}
}
impl From<EntityType> for u8 {
fn from(value: EntityType) -> Self {
match value {
EntityType::StageInstance => 1,
EntityType::Voice => 2,
EntityType::External => 3,
EntityType::Unknown(unknown) => unknown,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum PrivacyLevel {
GuildOnly,
Unknown(u8),
}
impl From<u8> for PrivacyLevel {
fn from(value: u8) -> Self {
match value {
2 => PrivacyLevel::GuildOnly,
unknown => PrivacyLevel::Unknown(unknown),
}
}
}
impl From<PrivacyLevel> for u8 {
fn from(value: PrivacyLevel) -> Self {
match value {
PrivacyLevel::GuildOnly => 2,
PrivacyLevel::Unknown(unknown) => unknown,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum Status {
Scheduled,
Active,
Completed,
Cancelled,
Unknown(u8),
}
impl From<u8> for Status {
fn from(value: u8) -> Self {
match value {
1 => Status::Scheduled,
2 => Status::Active,
3 => Status::Completed,
4 => Status::Cancelled,
unknown => Status::Unknown(unknown),
}
}
}
impl From<Status> for u8 {
fn from(value: Status) -> Self {
match value {
Status::Scheduled => 1,
Status::Active => 2,
Status::Completed => 3,
Status::Cancelled => 4,
Status::Unknown(unknown) => unknown,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test::image_hash::{COVER, COVER_INPUT};
use serde_test::Token;
use std::error::Error;
#[test]
fn scheduled_event() -> Result<(), Box<dyn Error>> {
let scheduled_start_time = Timestamp::parse("2022-01-01T00:00:00.000000+00:00")?;
let value = GuildScheduledEvent {
channel_id: Some(Id::new(1)),
creator: None,
creator_id: None,
description: Some("this is a dance party for garfield lovers".into()),
entity_id: Some(Id::new(2)),
entity_metadata: None,
entity_type: EntityType::StageInstance,
guild_id: Id::new(3),
id: Id::new(4),
image: Some(COVER),
name: "garfield dance party".into(),
privacy_level: PrivacyLevel::GuildOnly,
scheduled_end_time: None,
scheduled_start_time,
status: Status::Completed,
user_count: Some(1),
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "GuildScheduledEvent",
len: 12,
},
Token::Str("channel_id"),
Token::Some,
Token::NewtypeStruct { name: "Id" },
Token::Str("1"),
Token::Str("description"),
Token::Some,
Token::Str("this is a dance party for garfield lovers"),
Token::Str("entity_id"),
Token::Some,
Token::NewtypeStruct { name: "Id" },
Token::Str("2"),
Token::Str("entity_type"),
Token::U8(1),
Token::Str("guild_id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("3"),
Token::Str("id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("4"),
Token::Str("image"),
Token::Some,
Token::Str(COVER_INPUT),
Token::Str("name"),
Token::Str("garfield dance party"),
Token::Str("privacy_level"),
Token::U8(2),
Token::Str("scheduled_start_time"),
Token::Str("2022-01-01T00:00:00.000000+00:00"),
Token::Str("status"),
Token::U8(3),
Token::Str("user_count"),
Token::Some,
Token::U64(1),
Token::StructEnd,
],
);
Ok(())
}
}