diff options
| author | Zeyla Hellyer <[email protected]> | 2017-12-16 08:39:36 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-12-16 08:45:26 -0800 |
| commit | bcd16dddb8cc3086a13524c79676f3a8bebbc524 (patch) | |
| tree | 42d254fb4738df957c4b7d9e5766d1cb5bd47323 /src/model/id.rs | |
| parent | Fix guild deserialization tests (diff) | |
| download | serenity-bcd16dddb8cc3086a13524c79676f3a8bebbc524.tar.xz serenity-bcd16dddb8cc3086a13524c79676f3a8bebbc524.zip | |
Break up the model module
The `model` module has historically been one giant module re-exporting
all of the model types, which is somewhere around 100 types. This can be
a lot to look at for a new user and somewhat overwhelming, especially
with a large number of fine-grained imports from the module.
The module is now neatly split up into submodules, mostly like it has
been internally since the early versions of the library. The submodules
are:
- application
- channel
- error
- event
- gateway
- guild
- id
- invite
- misc
- permissions
- prelude
- user
- voice
- webhook
Each submodule contains types that are "owned" by the module. For
example, the `guild` submodule contains, but not limited to, Emoji,
AuditLogsEntry, Role, and Member. `channel` contains, but not limited
to, Attachment, Embed, Message, and Reaction.
Upgrade path:
Instead of glob importing the models via `use serenity::model::*;`,
instead glob import via the prelude:
```rust
use serenity::model::prelude::*;
```
Instead of importing from the root model module:
```rust
use serenity::model::{Guild, Message, OnlineStatus, Role, User};
```
instead import from the submodules like so:
```rust
use serenity::model::channel::Message;
use serenity::model::guild::{Guild, Role};
use serenity::model::user::{OnlineStatus, User};
```
Diffstat (limited to 'src/model/id.rs')
| -rw-r--r-- | src/model/id.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/model/id.rs b/src/model/id.rs new file mode 100644 index 0000000..3d8c20c --- /dev/null +++ b/src/model/id.rs @@ -0,0 +1,89 @@ +//! A collection of newtypes defining type-strong IDs. + +use chrono::NaiveDateTime; +use internal::prelude::*; +use serde::de::{Deserialize, Deserializer}; +use std::fmt::{Display, Formatter, Result as FmtResult}; +use super::utils::U64Visitor; + +macro_rules! id_u64 { + ($(#[$attr:meta] $name:ident;)*) => { + $( + #[$attr] + #[derive(Copy, Clone, Default, Debug, Eq, Hash, PartialOrd, Ord, Serialize)] + #[allow(derive_hash_xor_eq)] + pub struct $name(pub u64); + + impl $name { + /// Retrieves the time that the Id was created at. + pub fn created_at(&self) -> NaiveDateTime { + let offset = (self.0 >> 22) / 1000; + + NaiveDateTime::from_timestamp(1_420_070_400 + offset as i64, 0) + } + } + + // This is a hack so functions can accept iterators that either: + // 1. return the id itself (e.g: `MessageId`) + // 2. return a reference to it (`&MessageId`). + impl AsRef<$name> for $name { + fn as_ref(&self) -> &Self { + self + } + } + + impl From<u64> for $name { + fn from(id_as_u64: u64) -> $name { + $name(id_as_u64) + } + } + + impl PartialEq for $name { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } + } + + impl PartialEq<u64> for $name { + fn eq(&self, u: &u64) -> bool { + self.0 == *u + } + } + + impl Display for $name { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + Display::fmt(&self.0, f) + } + } + + impl<'de> Deserialize<'de> for $name { + fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> { + deserializer.deserialize_any(U64Visitor).map($name) + } + } + )* + } +} + +id_u64! { + /// An identifier for an Application. + ApplicationId; + /// An identifier for a Channel + ChannelId; + /// An identifier for an Emoji + EmojiId; + /// An identifier for a Guild + GuildId; + /// An identifier for an Integration + IntegrationId; + /// An identifier for a Message + MessageId; + /// An identifier for a Role + RoleId; + /// An identifier for a User + UserId; + /// An identifier for a [`Webhook`](struct.Webhook.html). + WebhookId; + /// An identifier for an audit log entry. + AuditLogEntryId; +} |