twilight_model/application/monetization/
entitlement.rsuse serde::{Deserialize, Serialize};
use crate::{
id::{
marker::{ApplicationMarker, EntitlementMarker, GuildMarker, SkuMarker, UserMarker},
Id,
},
util::Timestamp,
};
use super::entitlement_type::EntitlementType;
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Entitlement {
pub application_id: Id<ApplicationMarker>,
#[serde(skip_serializing_if = "Option::is_none")]
pub consumed: Option<bool>,
pub deleted: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub ends_at: Option<Timestamp>,
#[serde(skip_serializing_if = "Option::is_none")]
pub guild_id: Option<Id<GuildMarker>>,
pub id: Id<EntitlementMarker>,
#[serde(rename = "type")]
pub kind: EntitlementType,
pub sku_id: Id<SkuMarker>,
#[serde(skip_serializing_if = "Option::is_none")]
pub starts_at: Option<Timestamp>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id: Option<Id<UserMarker>>,
}
#[cfg(test)]
mod tests {
use std::error::Error;
use serde_test::Token;
use super::Entitlement;
use crate::application::monetization::entitlement_type::EntitlementType;
use crate::id::Id;
use crate::util::Timestamp;
#[test]
fn entitlement() -> Result<(), Box<dyn Error>> {
let starts_at_str = "2022-09-14T17:00:18.704163+00:00";
let ends_at_str = "2022-10-14T17:00:21.704163+00:00";
let starts_at = Timestamp::parse(starts_at_str)?;
let ends_at = Timestamp::parse(ends_at_str)?;
let value = Entitlement {
application_id: Id::new(1),
consumed: Some(false),
deleted: false,
ends_at: ends_at.into(),
guild_id: Some(Id::new(10)),
id: Id::new(2),
kind: EntitlementType::ApplicationSubscription,
sku_id: Id::new(3),
starts_at: starts_at.into(),
user_id: Some(Id::new(42)),
};
serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "Entitlement",
len: 10,
},
Token::Str("application_id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("1"),
Token::Str("consumed"),
Token::Some,
Token::Bool(false),
Token::Str("deleted"),
Token::Bool(false),
Token::Str("ends_at"),
Token::Some,
Token::Str(ends_at_str),
Token::Str("guild_id"),
Token::Some,
Token::NewtypeStruct { name: "Id" },
Token::Str("10"),
Token::Str("id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("2"),
Token::Str("type"),
Token::U8(8),
Token::Str("sku_id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("3"),
Token::Str("starts_at"),
Token::Some,
Token::Str(starts_at_str),
Token::Str("user_id"),
Token::Some,
Token::NewtypeStruct { name: "Id" },
Token::Str("42"),
Token::StructEnd,
],
);
Ok(())
}
}