twilight_model/guild/
role.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use super::{RoleFlags, RoleTags};
use crate::{
    guild::Permissions,
    id::{marker::RoleMarker, Id},
    util::image_hash::ImageHash,
};
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;

#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Role {
    pub color: u32,
    pub hoist: bool,
    /// Icon image hash.
    ///
    /// Present if the guild has the `ROLE_ICONS` feature and if the role has
    /// one.
    ///
    /// See [Discord Docs/Image Formatting].
    ///
    /// [Discord Docs/Image Formatting]: https://discord.com/developers/docs/reference#image-formatting
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon: Option<ImageHash>,
    pub id: Id<RoleMarker>,
    pub managed: bool,
    pub mentionable: bool,
    pub name: String,
    pub permissions: Permissions,
    pub position: i64,
    /// Flags for this role.
    pub flags: RoleFlags,
    /// Tags about the role.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<RoleTags>,
    /// Icon unicode emoji.
    ///
    /// Present if the guild has the `ROLE_ICONS` feature and if the role has
    /// one.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unicode_emoji: Option<String>,
}

impl Ord for Role {
    /// Compare two roles to each other using their position and ID.
    ///
    /// Roles are primarily ordered by their position in descending order. For example,
    /// a role with a position of 17 is considered a higher role than one with a
    /// position of 12.
    ///
    /// Discord does not guarantee that role positions are positive, unique, or contiguous. When
    /// two or more roles have the same position then the order is based on the roles' IDs in
    /// ascending order. For example, given two roles with positions of 10 then a role
    /// with an ID of 1 would be considered a higher role than one with an ID of 20.
    ///
    /// ### Examples
    ///
    /// Compare the position of two roles:
    ///
    /// ```
    /// # use twilight_model::{guild::{Permissions, Role, RoleFlags}, id::Id};
    /// # use std::cmp::Ordering;
    /// let role_a = Role {
    ///     id: Id::new(123),
    ///     position: 12,
    /// #   color: 0,
    /// #   hoist: true,
    /// #   icon: None,
    /// #   managed: false,
    /// #   mentionable: true,
    /// #   name: "test".to_owned(),
    /// #   permissions: Permissions::ADMINISTRATOR,
    /// #   flags: RoleFlags::empty(),
    /// #   tags: None,
    /// #   unicode_emoji: None,
    ///     // ...
    /// };
    /// let role_b = Role {
    ///     id: Id::new(456),
    ///     position: 13,
    /// #   color: 0,
    /// #   hoist: true,
    /// #   icon: None,
    /// #   managed: false,
    /// #   mentionable: true,
    /// #   name: "test".to_owned(),
    /// #   permissions: Permissions::ADMINISTRATOR,
    /// #   flags: RoleFlags::empty(),
    /// #   tags: None,
    /// #   unicode_emoji: None,
    ///     // ...
    /// };
    /// assert_eq!(Ordering::Less, role_a.cmp(&role_b));
    /// assert_eq!(Ordering::Greater, role_b.cmp(&role_a));
    /// assert_eq!(Ordering::Equal, role_a.cmp(&role_a));
    /// assert_eq!(Ordering::Equal, role_b.cmp(&role_b));
    /// ```
    ///
    /// Compare the position of two roles with the same position:
    ///
    /// ```
    /// # use twilight_model::{guild::{Permissions, Role, RoleFlags}, id::Id};
    /// # use std::cmp::Ordering;
    /// let role_a = Role {
    ///     id: Id::new(123),
    ///     position: 12,
    /// #   color: 0,
    /// #   hoist: true,
    /// #   icon: None,
    /// #   managed: false,
    /// #   mentionable: true,
    /// #   name: "test".to_owned(),
    /// #   permissions: Permissions::ADMINISTRATOR,
    /// #   flags: RoleFlags::empty(),
    /// #   tags: None,
    /// #   unicode_emoji: None,
    /// };
    /// let role_b = Role {
    ///     id: Id::new(456),
    ///     position: 12,
    /// #   color: 0,
    /// #   hoist: true,
    /// #   icon: None,
    /// #   managed: false,
    /// #   mentionable: true,
    /// #   name: "test".to_owned(),
    /// #   permissions: Permissions::ADMINISTRATOR,
    /// #   flags: RoleFlags::empty(),
    /// #   tags: None,
    /// #   unicode_emoji: None,
    /// };
    /// assert_eq!(Ordering::Less, role_a.cmp(&role_b));
    /// assert_eq!(Ordering::Greater, role_b.cmp(&role_a));
    /// assert_eq!(Ordering::Equal, role_a.cmp(&role_a));
    /// assert_eq!(Ordering::Equal, role_b.cmp(&role_b));
    /// ```
    fn cmp(&self, other: &Self) -> Ordering {
        self.position
            .cmp(&other.position)
            .then(self.id.get().cmp(&other.id.get()))
    }
}

impl PartialOrd for Role {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[cfg(test)]
mod tests {
    use super::{Permissions, Role};
    use crate::{guild::RoleFlags, id::Id};
    use serde::{Deserialize, Serialize};
    use serde_test::Token;
    use static_assertions::{assert_fields, assert_impl_all};
    use std::{fmt::Debug, hash::Hash};

    assert_fields!(
        Role: color,
        hoist,
        icon,
        id,
        managed,
        mentionable,
        name,
        permissions,
        position,
        tags,
        unicode_emoji
    );

    assert_impl_all!(
        Role: Clone,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Serialize
    );

    #[test]
    fn role() {
        let role = Role {
            color: 0,
            hoist: true,
            icon: None,
            id: Id::new(123),
            managed: false,
            mentionable: true,
            name: "test".to_owned(),
            permissions: Permissions::ADMINISTRATOR,
            position: 12,
            flags: RoleFlags::IN_PROMPT,
            tags: None,
            unicode_emoji: None,
        };

        serde_test::assert_tokens(
            &role,
            &[
                Token::Struct {
                    name: "Role",
                    len: 9,
                },
                Token::Str("color"),
                Token::U32(0),
                Token::Str("hoist"),
                Token::Bool(true),
                Token::Str("id"),
                Token::NewtypeStruct { name: "Id" },
                Token::Str("123"),
                Token::Str("managed"),
                Token::Bool(false),
                Token::Str("mentionable"),
                Token::Bool(true),
                Token::Str("name"),
                Token::Str("test"),
                Token::Str("permissions"),
                Token::Str("8"),
                Token::Str("position"),
                Token::I64(12),
                Token::Str("flags"),
                Token::U64(1),
                Token::StructEnd,
            ],
        );
    }
}