twilight_model/guild/
role_tags.rs

1use crate::{
2    id::{
3        marker::{IntegrationMarker, RoleSubscriptionSkuMarker, UserMarker},
4        Id,
5    },
6    util::is_false,
7};
8use serde::{Deserialize, Serialize};
9
10/// Tags that a [`Role`] has.
11///
12/// [`Role`]: super::Role
13#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
14pub struct RoleTags {
15    /// Whether this role is available for purchase.
16    #[serde(
17        default,
18        skip_serializing_if = "is_false",
19        with = "crate::visitor::null_boolean"
20    )]
21    pub available_for_purchase: bool,
22    /// ID of the bot the role belongs to.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub bot_id: Option<Id<UserMarker>>,
25    /// Whether this role is a guild's linked role.
26    #[serde(
27        default,
28        skip_serializing_if = "is_false",
29        with = "crate::visitor::null_boolean"
30    )]
31    pub guild_connections: bool,
32    /// ID of the integration the role belongs to.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub integration_id: Option<Id<IntegrationMarker>>,
35    /// ID of the role's subscription SKU and listing.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub subscription_listing_id: Option<Id<RoleSubscriptionSkuMarker>>,
38    /// Whether this is the guild's premium subscriber role.
39    #[serde(
40        default,
41        skip_serializing_if = "is_false",
42        with = "crate::visitor::null_boolean"
43    )]
44    pub premium_subscriber: bool,
45}
46
47#[cfg(test)]
48mod tests {
49    use super::RoleTags;
50    use crate::id::Id;
51    use serde_test::Token;
52
53    #[test]
54    fn bot() {
55        let tags = RoleTags {
56            available_for_purchase: false,
57            bot_id: Some(Id::new(1)),
58            guild_connections: false,
59            integration_id: Some(Id::new(2)),
60            premium_subscriber: false,
61            subscription_listing_id: None,
62        };
63
64        serde_test::assert_tokens(
65            &tags,
66            &[
67                Token::Struct {
68                    name: "RoleTags",
69                    len: 2,
70                },
71                Token::Str("bot_id"),
72                Token::Some,
73                Token::NewtypeStruct { name: "Id" },
74                Token::Str("1"),
75                Token::Str("integration_id"),
76                Token::Some,
77                Token::NewtypeStruct { name: "Id" },
78                Token::Str("2"),
79                Token::StructEnd,
80            ],
81        );
82    }
83
84    #[test]
85    fn premium_subscriber() {
86        let tags = RoleTags {
87            available_for_purchase: false,
88            bot_id: None,
89            guild_connections: false,
90            integration_id: None,
91            premium_subscriber: true,
92            subscription_listing_id: None,
93        };
94
95        serde_test::assert_tokens(
96            &tags,
97            &[
98                Token::Struct {
99                    name: "RoleTags",
100                    len: 1,
101                },
102                Token::Str("premium_subscriber"),
103                Token::None,
104                Token::StructEnd,
105            ],
106        );
107    }
108
109    #[test]
110    fn subscription() {
111        let tags = RoleTags {
112            available_for_purchase: true,
113            bot_id: None,
114            guild_connections: false,
115            integration_id: Some(Id::new(1)),
116            subscription_listing_id: Some(Id::new(2)),
117            premium_subscriber: false,
118        };
119
120        serde_test::assert_tokens(
121            &tags,
122            &[
123                Token::Struct {
124                    name: "RoleTags",
125                    len: 3,
126                },
127                Token::Str("available_for_purchase"),
128                Token::None,
129                Token::Str("integration_id"),
130                Token::Some,
131                Token::NewtypeStruct { name: "Id" },
132                Token::Str("1"),
133                Token::Str("subscription_listing_id"),
134                Token::Some,
135                Token::NewtypeStruct { name: "Id" },
136                Token::Str("2"),
137                Token::StructEnd,
138            ],
139        );
140    }
141
142    /// Test that if all fields are None and the optional null fields are false,
143    /// then serialize back into the source payload (where all fields are not
144    /// present).
145    #[test]
146    fn none() {
147        let tags = RoleTags {
148            available_for_purchase: false,
149            bot_id: None,
150            guild_connections: false,
151            integration_id: None,
152            premium_subscriber: false,
153            subscription_listing_id: None,
154        };
155
156        serde_test::assert_tokens(
157            &tags,
158            &[
159                Token::Struct {
160                    name: "RoleTags",
161                    len: 0,
162                },
163                Token::StructEnd,
164            ],
165        );
166    }
167}