aboutsummaryrefslogtreecommitdiff
path: root/src/model/invite.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-04-11 08:15:37 -0700
committerZeyla Hellyer <[email protected]>2017-04-11 10:52:43 -0700
commitf6b27eb39c042e6779edc2d5d4b6e6c27d133eaf (patch)
treea6169fee3bf9ea75391101577dcb2982e3daa388 /src/model/invite.rs
parentClippy lints + permission byte literals (diff)
downloadserenity-f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf.tar.xz
serenity-f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf.zip
Switch to using serde for deserialization
The current build system is rudimentary, incomplete, and rigid, offering little in the way of customizing decoding options. To solve this, switch to using serde-derive with custom Deserialization implementations. This allows very simple deserialization when special logic does not need to be applied, yet allows us to implement our own deserialization logic when required. The problem with the build system was that it built enums and structs from YAML files. This is not so good, because it requires creating a custom build system (which was rudimentary), creating "special struct configs" when logic needed to be ever so slightly extended (rigid), and if special logic needed to be applied, a custom deserialization method would have been needed to be made anyway (incomplete). To solve this, switch to serde-derive and implementing Deserialize ourselves where required. This reduces YAML definitions that might look like: ```yaml --- name: Group description: > A group channel, potentially including other users, separate from a [`Guild`]. [`Guild`]: struct.Guild.html fields: - name: channel_id description: The Id of the group channel. from: id type: ChannelId - name: icon description: The optional icon of the group channel. optional: true type: string - name: last_message_id description: The Id of the last message sent. optional: true type: MessageId - name: last_pin_timestamp description: Timestamp of the latest pinned message. optional: true type: string - name: name description: The name of the group channel. optional: true type: string - name: owner_id description: The Id of the group channel creator. type: UserId - name: recipients description: Group channel's members. custom: decode_users t: UserId, Arc<RwLock<User>> type: hashmap ``` to: ```rs /// A group channel - potentially including other [`User`]s - separate from a /// [`Guild`]. /// /// [`Guild`]: struct.Guild.html /// [`User`]: struct.User.html pub struct Group { /// The Id of the group channel. #[serde(rename="id")] pub channel_id: ChannelId, /// The optional icon of the group channel. pub icon: Option<String>, /// The Id of the last message sent. pub last_message_id: Option<MessageId>, /// Timestamp of the latest pinned message. pub last_pin_timestamp: Option<String>, /// The name of the group channel. pub name: Option<String>, /// The Id of the group owner. pub owner_id: UserId, /// A map of the group's recipients. #[serde(deserialize_with="deserialize_users")] pub recipients: HashMap<UserId, Arc<RwLock<User>>>, } ``` This is much simpler and does not have as much boilerplate. There should not be any backwards incompatible changes other than the old, public - yet undocumented (and hidden from documentation) - decode methods being removed. Due to the nature of this commit, field names may be incorrect, and will need to be corrected as deserialization errors are found.
Diffstat (limited to 'src/model/invite.rs')
-rw-r--r--src/model/invite.rs83
1 files changed, 80 insertions, 3 deletions
diff --git a/src/model/invite.rs b/src/model/invite.rs
index 4e33a3f..d041d55 100644
--- a/src/model/invite.rs
+++ b/src/model/invite.rs
@@ -1,7 +1,6 @@
-use super::{Invite, RichInvite};
+use super::*;
use ::client::rest;
use ::internal::prelude::*;
-use ::model::ChannelId;
use ::utils::builder::CreateInvite;
use ::utils;
@@ -10,6 +9,25 @@ use super::permissions;
#[cfg(feature="cache")]
use super::utils as model_utils;
+/// Information about an invite code.
+///
+/// Information can not be accessed for guilds the current user is banned from.
+#[derive(Clone, Debug, Deserialize)]
+pub struct Invite {
+ /// The unique code for the invite.
+ pub code: String,
+ /// A representation of the minimal amount of information needed about the
+ /// [`GuildChannel`] being invited to.
+ ///
+ /// [`GuildChannel`]: struct.GuildChannel.html
+ pub channel: InviteChannel,
+ /// a representation of the minimal amount of information needed about the
+ /// [`Guild`] being invited to.
+ ///
+ /// [`Guild`]: struct.Guild.html
+ pub guild: InviteGuild,
+}
+
impl Invite {
/// Creates an invite for a [`GuildChannel`], providing a builder so that
/// fields may optionally be set.
@@ -42,7 +60,7 @@ impl Invite {
}
}
- rest::create_invite(channel_id.0, &f(CreateInvite::default()).0.build())
+ rest::create_invite(channel_id.0, &f(CreateInvite::default()).0)
}
/// Deletes the invite.
@@ -76,6 +94,65 @@ impl Invite {
}
}
+/// A inimal information about the channel an invite points to.
+#[derive(Clone, Debug, Deserialize)]
+pub struct InviteChannel {
+ pub id: ChannelId,
+ pub name: String,
+ #[serde(rename="type")]
+ pub kind: ChannelType,
+}
+
+/// A minimal amount of information about the guild an invite points to.
+#[derive(Clone, Debug, Deserialize)]
+pub struct InviteGuild {
+ pub id: GuildId,
+ pub icon: Option<String>,
+ pub name: String,
+ pub splash_hash: Option<String>,
+}
+
+/// Detailed information about an invite.
+/// This information can only be retrieved by anyone with the [Manage Guild]
+/// permission. Otherwise, a minimal amount of information can be retrieved via
+/// the [`Invite`] struct.
+///
+/// [`Invite`]: struct.Invite.html
+/// [Manage Guild]: permissions/constant.MANAGE_GUILD.html
+#[derive(Clone, Debug, Deserialize)]
+pub struct RichInvite {
+ /// A representation of the minimal amount of information needed about the
+ /// channel being invited to.
+ pub channel: InviteChannel,
+ /// The unique code for the invite.
+ pub code: String,
+ /// When the invite was created.
+ pub created_at: String,
+ /// A representation of the minimal amount of information needed about the
+ /// guild being invited to.
+ pub guild: InviteGuild,
+ /// The user that created the invite.
+ pub inviter: User,
+ /// The maximum age of the invite in seconds, from when it was created.
+ pub max_age: u64,
+ /// The maximum number of times that an invite may be used before it expires.
+
+ /// Note that this does not supercede the [`max_age`] value, if the value of
+ /// [`temporary`] is `true`. If the value of `temporary` is `false`, then the
+ /// invite _will_ self-expire after the given number of max uses.
+
+ /// If the value is `0`, then the invite is permanent.
+ ///
+ /// [`max_age`]: #structfield.max_age
+ /// [`temporary`]: #structfield.temporary
+ pub max_uses: u64,
+ /// Indicator of whether the invite self-expires after a certain amount of
+ /// time or uses.
+ pub temporary: bool,
+ /// The amount of times that an invite has been used.
+ pub uses: u64,
+}
+
impl RichInvite {
/// Deletes the invite.
///