Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(model)!: revert moving snowflake to model #1471

Merged
merged 3 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 0 additions & 91 deletions model/src/id/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,94 +237,3 @@ pub struct UserMarker;
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct WebhookMarker;

/// Snowflake marker trait.
///
/// A snowflake is a globally unique ID made up of a timestamp, worker ID, process ID and increment.
/// Types implementing this trait enable methods for extracting this information from [`Id`]s.
///
/// [`Id`]: super::Id
pub trait Snowflake: private::Sealed {}

impl Snowflake for ApplicationMarker {}

impl Snowflake for AttachmentMarker {}

impl Snowflake for AuditLogEntryMarker {}

impl Snowflake for ChannelMarker {}

impl Snowflake for CommandMarker {}

impl Snowflake for CommandVersionMarker {}

impl Snowflake for EmojiMarker {}

impl Snowflake for GenericMarker {}

impl Snowflake for GuildMarker {}

impl Snowflake for IntegrationMarker {}

impl Snowflake for InteractionMarker {}

impl Snowflake for MessageMarker {}

impl Snowflake for RoleMarker {}

impl Snowflake for ScheduledEventMarker {}

impl Snowflake for ScheduledEventEntityMarker {}

impl Snowflake for StageMarker {}

impl Snowflake for UserMarker {}

impl Snowflake for WebhookMarker {}

mod private {
use super::{
ApplicationMarker, AttachmentMarker, AuditLogEntryMarker, ChannelMarker, CommandMarker,
CommandVersionMarker, EmojiMarker, GenericMarker, GuildMarker, IntegrationMarker,
InteractionMarker, MessageMarker, RoleMarker, ScheduledEventEntityMarker,
ScheduledEventMarker, StageMarker, UserMarker, WebhookMarker,
};

pub trait Sealed {}

impl Sealed for ApplicationMarker {}

impl Sealed for AttachmentMarker {}

impl Sealed for AuditLogEntryMarker {}

impl Sealed for ChannelMarker {}

impl Sealed for CommandMarker {}

impl Sealed for CommandVersionMarker {}

impl Sealed for EmojiMarker {}

impl Sealed for GenericMarker {}

impl Sealed for GuildMarker {}

impl Sealed for IntegrationMarker {}

impl Sealed for InteractionMarker {}

impl Sealed for MessageMarker {}

impl Sealed for RoleMarker {}

impl Sealed for ScheduledEventMarker {}

impl Sealed for ScheduledEventEntityMarker {}

impl Sealed for StageMarker {}

impl Sealed for UserMarker {}

impl Sealed for WebhookMarker {}
}
141 changes: 17 additions & 124 deletions model/src/id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ mod r#type;

pub use self::r#type::*;

use marker::Snowflake;
use serde::{
de::{Deserialize, Deserializer, Error as DeError, Unexpected, Visitor},
ser::{Serialize, Serializer},
Expand Down Expand Up @@ -188,65 +187,6 @@ impl<T> Id<T> {
}
}

impl<T: Snowflake> Id<T> {
/// The Unix epoch of the Snowflake in milliseconds, indicating when it was generated.
///
/// Derived from bits 22..63 of the id.
///
/// # Examples
///
/// ```
/// use twilight_model::{
/// datetime::Timestamp,
/// id::{marker::UserMarker, Id},
/// };
///
/// let id = Id::<UserMarker>::new(105484726235607040);
///
/// assert_eq!(id.timestamp(), 1445219918546);
///
/// assert_eq!(
/// "2015-10-19T01:58:38.546000+00:00",
/// Timestamp::from_micros(id.timestamp() * 1000)?
/// .iso_8601()
/// .to_string()
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[allow(clippy::cast_possible_wrap)]
pub 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.get() >> 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)]
pub fn worker_id(self) -> u8 {
((self.get() & 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)]
pub fn process_id(self) -> u8 {
((self.get() & 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)]
pub fn increment(self) -> u16 {
(self.get() & 0xFFF) as u16
}
}

impl<T> Debug for Id<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str("Id")?;
Expand Down Expand Up @@ -457,8 +397,7 @@ mod tests {
marker::{
ApplicationMarker, AttachmentMarker, AuditLogEntryMarker, ChannelMarker, CommandMarker,
CommandVersionMarker, EmojiMarker, GenericMarker, GuildMarker, IntegrationMarker,
InteractionMarker, MessageMarker, RoleMarker, Snowflake, StageMarker, UserMarker,
WebhookMarker,
InteractionMarker, MessageMarker, RoleMarker, StageMarker, UserMarker, WebhookMarker,
},
Id, IdStringDisplay,
};
Expand All @@ -474,36 +413,22 @@ mod tests {
str::FromStr,
};

assert_impl_all!(ApplicationMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(AttachmentMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(
AuditLogEntryMarker: Clone,
Copy,
Debug,
Send,
Snowflake,
Sync
);
assert_impl_all!(ChannelMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(CommandMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(
CommandVersionMarker: Clone,
Copy,
Debug,
Send,
Snowflake,
Sync
);
assert_impl_all!(EmojiMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(GenericMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(GuildMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(IntegrationMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(InteractionMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(MessageMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(RoleMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(StageMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(UserMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(WebhookMarker: Clone, Copy, Debug, Send, Snowflake, Sync);
assert_impl_all!(ApplicationMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(AttachmentMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(AuditLogEntryMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(ChannelMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(CommandMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(CommandVersionMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(EmojiMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(GenericMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(GuildMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(IntegrationMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(InteractionMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(MessageMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(RoleMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(StageMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(UserMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(WebhookMarker: Clone, Copy, Debug, Send, Sync);
assert_impl_all!(Id<GenericMarker>:
Clone, Copy, Debug, Deserialize<'static>, Display, Eq, From<NonZeroU64>,
FromStr, Hash, Ord, PartialEq, PartialEq<i64>, PartialEq<u64>, PartialOrd, Send, Serialize, Sync,
Expand Down Expand Up @@ -557,38 +482,6 @@ mod tests {
assert_eq!(123_u64, id.cast::<RoleMarker>());
}

#[test]
fn test_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 test_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 test_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 test_increment() {
let expected: u16 = 40;
let id = Id::<GenericMarker>::new(762_022_344_856_174_632);

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

/// Test that debugging IDs formats the generic and value as a newtype.
#[test]
fn test_debug() {
Expand Down
5 changes: 4 additions & 1 deletion util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ version = "0.8.2"
twilight-model = { default-features = false, optional = true, path = "../model" }

[dev-dependencies]
chrono = { default-features = false, features = ["std"], version = "0.4" }
static_assertions = { default-features = false, version = "1" }
time = { default-features = false, features = ["formatting"], version = "0.3" }

[features]
default = []
builder = ["twilight-model"]
link = ["twilight-model"]
permission-calculator = ["twilight-model"]
full = ["builder", "link", "permission-calculator"]
snowflake = ["twilight-model"]
full = ["builder", "link", "permission-calculator", "snowflake"]

[package.metadata.docs.rs]
all-features = true
Expand Down
5 changes: 5 additions & 0 deletions util/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ webhook URLs.
Allows the use of a calculator to determine the permissions of a member in
a guild or channel.

### `snowflake`

Allows the use of the `Snowflake` trait, which provides methods for the extraction of
structured information from [Discord snowflakes].

[`twilight-rs`]: https://github.com/twilight-rs/twilight
[codecov badge]: https://img.shields.io/codecov/c/gh/twilight-rs/twilight?logo=codecov&style=for-the-badge&token=E9ERLJL0L2
[codecov link]: https://app.codecov.io/gh/twilight-rs/twilight/
Expand Down
9 changes: 9 additions & 0 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
//! Allows the use of a calculator to determine the permissions of a member in
//! a guild or channel.
//!
//! ### `snowflake`
//!
//! Allows the use of the `Snowflake` trait, which provides methods for the extraction of
//! structured information from [Discord snowflakes].
//!
//! [`twilight-rs`]: https://github.com/twilight-rs/twilight
//! [codecov badge]: https://img.shields.io/codecov/c/gh/twilight-rs/twilight?logo=codecov&style=for-the-badge&token=E9ERLJL0L2
//! [codecov link]: https://app.codecov.io/gh/twilight-rs/twilight/
Expand Down Expand Up @@ -56,3 +61,7 @@ pub mod link;

#[cfg(feature = "permission-calculator")]
pub mod permission_calculator;

#[cfg(feature = "snowflake")]
#[cfg_attr(docsrs, doc(cfg(feature = "snowflake")))]
7596ff marked this conversation as resolved.
Show resolved Hide resolved
pub mod snowflake;
Loading