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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! Provides the Snowflake trait for defining extractable information from a Discord Snowflake.

use twilight_model::id::{
    marker::{
        ApplicationMarker, AttachmentMarker, AuditLogEntryMarker, ChannelMarker, CommandMarker,
        CommandVersionMarker, EmojiMarker, GenericMarker, GuildMarker, IntegrationMarker,
        InteractionMarker, MessageMarker, OauthSkuMarker, OauthTeamMarker, RoleMarker,
        RoleSubscriptionSkuMarker, ScheduledEventEntityMarker, ScheduledEventMarker, StageMarker,
        StickerMarker, StickerPackMarker, StickerPackSkuMarker, UserMarker, WebhookMarker,
    },
    Id,
};

/// Snowflake is a trait for defining extractable information from a Snowflake. A Snowflake is a
/// u64 generated by Discord to uniquely identify a resource.
pub trait Snowflake {
    /// Returns the u64 backing the Snowflake.
    fn id(&self) -> u64;

    /// The Unix epoch of the Snowflake in milliseconds, indicating when it was generated.
    ///
    /// Derived from bits 22..63 of the id.
    ///
    /// # Examples
    ///
    /// See when a user was created using [`chrono`](https://docs.rs/chrono):
    ///
    /// ```
    /// use chrono::{TimeZone, Utc};
    /// use twilight_model::id::{marker::UserMarker, Id};
    /// use twilight_util::snowflake::Snowflake;
    ///
    /// let id = Id::<UserMarker>::new(105484726235607040);
    ///
    /// assert_eq!(
    ///     "2015-10-19T01:58:38.546+00:00",
    ///     Utc.timestamp_millis(id.timestamp()).to_rfc3339()
    /// );
    /// ```
    ///
    /// See when a user was created using [`time`](https://docs.rs/time):
    ///
    /// ```
    /// use time::{format_description::well_known::Rfc3339, Duration, OffsetDateTime};
    /// use twilight_model::id::{marker::UserMarker, Id};
    /// use twilight_util::snowflake::Snowflake;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let id = Id::<UserMarker>::new(105484726235607040);
    /// // Convert milliseconds to seconds or nanoseconds.
    /// let dur = Duration::milliseconds(id.timestamp());
    ///
    /// let ts = OffsetDateTime::from_unix_timestamp(dur.whole_seconds())?;
    /// let ts_milli = OffsetDateTime::from_unix_timestamp_nanos(dur.whole_nanoseconds())?;
    ///
    /// assert_eq!("2015-10-19T01:58:38Z", ts.format(&Rfc3339)?);
    /// assert_eq!("2015-10-19T01:58:38.546Z", ts_milli.format(&Rfc3339)?);
    /// # Ok(()) }
    /// ```
    #[allow(clippy::cast_possible_wrap)]
    fn timestamp(&self) -> i64 {
        // Discord's custom epoch, the unix time in milliseconds for the first second of 2015.
        const DISCORD_EPOCH: u64 = 1_420_070_400_000;

        ((self.id() >> 22) + DISCORD_EPOCH) as i64
    }

    /// The id of the internal worker that generated the Snowflake.
    ///
    /// Derived from bits 17..21 of the id.
    #[allow(clippy::cast_possible_truncation)]
    fn worker_id(&self) -> u8 {
        ((self.id() & 0x003E_0000) >> 17) as u8
    }

    /// The id of the internal process that generated the Snowflake.
    ///
    /// Derived from bits 12..16 of the id.
    #[allow(clippy::cast_possible_truncation)]
    fn process_id(&self) -> u8 {
        ((self.id() & 0x1F000) >> 12) as u8
    }

    /// The increment of the Snowflake. For every id that is generated on a process, this number is
    /// incremented.
    ///
    /// Derived from bits 0..11 of the id.
    #[allow(clippy::cast_possible_truncation)]
    fn increment(&self) -> u16 {
        (self.id() & 0xFFF) as u16
    }
}

impl Snowflake for Id<ApplicationMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<AttachmentMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<AuditLogEntryMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<ChannelMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<CommandMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<CommandVersionMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<EmojiMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<GenericMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<GuildMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<IntegrationMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<InteractionMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<MessageMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<OauthSkuMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<OauthTeamMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<RoleMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<RoleSubscriptionSkuMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<ScheduledEventMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<ScheduledEventEntityMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<StageMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<StickerMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<StickerPackMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<StickerPackSkuMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<UserMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

impl Snowflake for Id<WebhookMarker> {
    fn id(&self) -> u64 {
        self.get()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use static_assertions::{assert_impl_all, assert_obj_safe};

    assert_impl_all!(Id<ApplicationMarker>: Snowflake);
    assert_impl_all!(Id<AttachmentMarker>: Snowflake);
    assert_impl_all!(Id<AuditLogEntryMarker>: Snowflake);
    assert_impl_all!(Id<ChannelMarker>: Snowflake);
    assert_impl_all!(Id<CommandMarker>: Snowflake);
    assert_impl_all!(Id<CommandVersionMarker>: Snowflake);
    assert_impl_all!(Id<EmojiMarker>: Snowflake);
    assert_impl_all!(Id<GenericMarker>: Snowflake);
    assert_impl_all!(Id<GuildMarker>: Snowflake);
    assert_impl_all!(Id<IntegrationMarker>: Snowflake);
    assert_impl_all!(Id<InteractionMarker>: Snowflake);
    assert_impl_all!(Id<MessageMarker>: Snowflake);
    assert_impl_all!(Id<OauthSkuMarker>: Snowflake);
    assert_impl_all!(Id<OauthTeamMarker>: Snowflake);
    assert_impl_all!(Id<RoleMarker>: Snowflake);
    assert_impl_all!(Id<RoleSubscriptionSkuMarker>: Snowflake);
    assert_impl_all!(Id<ScheduledEventMarker>: Snowflake);
    assert_impl_all!(Id<ScheduledEventEntityMarker>: Snowflake);
    assert_impl_all!(Id<StageMarker>: Snowflake);
    assert_impl_all!(Id<StickerMarker>: Snowflake);
    assert_impl_all!(Id<StickerPackMarker>: Snowflake);
    assert_impl_all!(Id<StickerPackSkuMarker>: Snowflake);
    assert_impl_all!(Id<UserMarker>: Snowflake);
    assert_impl_all!(Id<WebhookMarker>: Snowflake);
    assert_obj_safe!(Snowflake);

    #[test]
    fn timestamp() {
        let expected: i64 = 1_445_219_918_546;
        let id = Id::<GenericMarker>::new(105_484_726_235_607_040);

        assert_eq!(expected, id.timestamp());
    }

    #[test]
    fn worker_id() {
        let expected: u8 = 8;
        let id = Id::<GenericMarker>::new(762_022_344_856_174_632);

        assert_eq!(expected, id.worker_id());
    }

    #[test]
    fn process_id() {
        let expected: u8 = 1;
        let id = Id::<GenericMarker>::new(61_189_081_970_774_016);

        assert_eq!(expected, id.process_id());
    }

    #[test]
    fn increment() {
        let expected: u16 = 40;
        let id = Id::<GenericMarker>::new(762_022_344_856_174_632);

        assert_eq!(expected, id.increment());
    }
}