titanium_model/
monetization.rs1use crate::Snowflake;
6use serde::{Deserialize, Serialize};
7use serde_repr::{Deserialize_repr, Serialize_repr};
8
9#[derive(Debug, Clone, Deserialize, Serialize)]
11pub struct Entitlement {
12 pub id: Snowflake,
14
15 pub sku_id: Snowflake,
17
18 pub application_id: Snowflake,
20
21 #[serde(default)]
23 pub user_id: Option<Snowflake>,
24
25 #[serde(rename = "type")]
27 pub entitlement_type: EntitlementType,
28
29 #[serde(default)]
31 pub deleted: bool,
32
33 #[serde(default)]
35 pub starts_at: Option<String>,
36
37 #[serde(default)]
39 pub ends_at: Option<String>,
40
41 #[serde(default)]
43 pub guild_id: Option<Snowflake>,
44
45 #[serde(default)]
47 pub consumed: bool,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
52#[repr(u8)]
53pub enum EntitlementType {
54 Purchase = 1,
56 PremiumSubscription = 2,
58 DeveloperGift = 3,
60 TestModePurchase = 4,
62 FreePurchase = 5,
64 UserGift = 6,
66 PremiumPurchase = 7,
68 ApplicationSubscription = 8,
70}
71
72#[derive(Debug, Clone, Deserialize, Serialize)]
74pub struct Subscription {
75 pub id: Snowflake,
77
78 pub user_id: Snowflake,
80
81 pub sku_ids: Vec<Snowflake>,
83
84 pub entitlement_ids: Vec<Snowflake>,
86
87 pub current_period_start: String,
89
90 pub current_period_end: String,
92
93 pub status: SubscriptionStatus,
95
96 #[serde(default)]
98 pub canceled_at: Option<String>,
99
100 #[serde(default)]
102 pub country: Option<String>,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
107#[repr(u8)]
108pub enum SubscriptionStatus {
109 Active = 0,
111 Ending = 1,
113 Inactive = 2,
115}
116
117#[derive(Debug, Clone, Deserialize, Serialize)]
119pub struct Sku {
120 pub id: Snowflake,
122
123 #[serde(rename = "type")]
125 pub sku_type: SkuType,
126
127 pub application_id: Snowflake,
129
130 pub name: String,
132
133 pub slug: String,
135
136 #[serde(default)]
138 pub flags: u64,
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
143#[repr(u8)]
144pub enum SkuType {
145 Durable = 2,
147 Consumable = 3,
149 Subscription = 5,
151 SubscriptionGroup = 6,
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn test_entitlement() {
161 let json = r#"{
162 "id": "123",
163 "sku_id": "456",
164 "application_id": "789",
165 "type": 8,
166 "deleted": false,
167 "consumed": false
168 }"#;
169
170 let entitlement: Entitlement = crate::json::from_str(json).unwrap();
171 assert_eq!(
172 entitlement.entitlement_type,
173 EntitlementType::ApplicationSubscription
174 );
175 }
176
177 #[test]
178 fn test_subscription() {
179 let json = r#"{
180 "id": "123",
181 "user_id": "456",
182 "sku_ids": ["789"],
183 "entitlement_ids": ["321"],
184 "current_period_start": "2024-01-01T00:00:00.000Z",
185 "current_period_end": "2024-02-01T00:00:00.000Z",
186 "status": 0
187 }"#;
188
189 let subscription: Subscription = crate::json::from_str(json).unwrap();
190 assert_eq!(subscription.status, SubscriptionStatus::Active);
191 }
192}