twilight_model/application/monetization/
subscription_status.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
7#[non_exhaustive]
8#[serde(from = "u8", into = "u8")]
9pub enum SubscriptionStatus {
10 Active,
12 Inactive,
14 Ending,
16 Unknown(u8),
17}
18
19impl From<u8> for SubscriptionStatus {
20 fn from(value: u8) -> Self {
21 match value {
22 0 => Self::Active,
23 1 => Self::Inactive,
24 2 => Self::Ending,
25 other => Self::Unknown(other),
26 }
27 }
28}
29
30impl From<SubscriptionStatus> for u8 {
31 fn from(value: SubscriptionStatus) -> Self {
32 match value {
33 SubscriptionStatus::Active => 0,
34 SubscriptionStatus::Inactive => 1,
35 SubscriptionStatus::Ending => 2,
36 SubscriptionStatus::Unknown(other) => other,
37 }
38 }
39}
40
41impl SubscriptionStatus {
42 pub const fn name(self) -> &'static str {
43 match self {
44 Self::Active => "Active",
45 Self::Inactive => "Inactive",
46 Self::Ending => "Ending",
47 Self::Unknown(_) => "Unknown",
48 }
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use super::SubscriptionStatus;
55 use serde_test::Token;
56
57 #[test]
58 fn variants() {
59 serde_test::assert_tokens(&SubscriptionStatus::Active, &[Token::U8(0)]);
60 serde_test::assert_tokens(&SubscriptionStatus::Inactive, &[Token::U8(1)]);
61 serde_test::assert_tokens(&SubscriptionStatus::Ending, &[Token::U8(2)]);
62 serde_test::assert_tokens(&SubscriptionStatus::Unknown(99), &[Token::U8(99)]);
63 }
64
65 #[test]
66 fn names() {
67 assert_eq!(SubscriptionStatus::Active.name(), "Active");
68 assert_eq!(SubscriptionStatus::Inactive.name(), "Inactive");
69 assert_eq!(SubscriptionStatus::Ending.name(), "Ending");
70 assert_eq!(SubscriptionStatus::Unknown(99).name(), "Unknown");
71 }
72}