aboutsummaryrefslogtreecommitdiff
path: root/src/model/channel/mod.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/channel/mod.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/channel/mod.rs')
-rw-r--r--src/model/channel/mod.rs162
1 files changed, 135 insertions, 27 deletions
diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs
index 3ea765a..4877785 100644
--- a/src/model/channel/mod.rs
+++ b/src/model/channel/mod.rs
@@ -16,11 +16,31 @@ pub use self::message::*;
pub use self::private_channel::*;
pub use self::reaction::*;
+use serde::de::Error as DeError;
+use serde_json;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::io::Read;
use ::model::*;
use ::utils::builder::{CreateMessage, GetMessages};
+/// A container for any channel.
+#[derive(Clone, Debug)]
+pub enum Channel {
+ /// A group. A group comprises of only one channel.
+ Group(Arc<RwLock<Group>>),
+ /// A [text] or [voice] channel within a [`Guild`].
+ ///
+ /// [`Guild`]: struct.Guild.html
+ /// [text]: enum.ChannelType.html#variant.Text
+ /// [voice]: enum.ChannelType.html#variant.Voice
+ Guild(Arc<RwLock<GuildChannel>>),
+ /// A private channel to another [`User`]. No other users may access the
+ /// channel. For multi-user "private channels", use a group.
+ ///
+ /// [`User`]: struct.User.html
+ Private(Arc<RwLock<PrivateChannel>>),
+}
+
impl Channel {
/// React to a [`Message`] with a custom [`Emoji`] or unicode character.
///
@@ -40,21 +60,6 @@ impl Channel {
self.id().create_reaction(message_id, reaction_type)
}
- #[doc(hidden)]
- pub fn decode(value: Value) -> Result<Channel> {
- let map = into_map(value)?;
- match req!(map.get("type").and_then(|x| x.as_u64())) {
- 0 | 2 => GuildChannel::decode(Value::Object(map))
- .map(|x| Channel::Guild(Arc::new(RwLock::new(x)))),
- 1 => PrivateChannel::decode(Value::Object(map))
- .map(|x| Channel::Private(Arc::new(RwLock::new(x)))),
- 3 => Group::decode(Value::Object(map))
- .map(|x| Channel::Group(Arc::new(RwLock::new(x)))),
- other => Err(Error::Decode("Expected value Channel type",
- Value::U64(other))),
- }
- }
-
/// Deletes the inner channel.
///
/// **Note**: There is no real function as _deleting_ a [`Group`]. The
@@ -307,6 +312,30 @@ impl Channel {
}
}
+impl Deserialize for Channel {
+ fn deserialize<D: Deserializer>(deserializer: D) -> StdResult<Self, D::Error> {
+ let v = JsonMap::deserialize(deserializer)?;
+ let kind = {
+ let kind = v.get("type").ok_or_else(|| DeError::missing_field("type"))?;
+
+ kind.as_u64().unwrap()
+ };
+
+ match kind {
+ 0 | 2 => serde_json::from_value::<GuildChannel>(Value::Object(v))
+ .map(|x| Channel::Guild(Arc::new(RwLock::new(x))))
+ .map_err(DeError::custom),
+ 1 => serde_json::from_value::<PrivateChannel>(Value::Object(v))
+ .map(|x| Channel::Private(Arc::new(RwLock::new(x))))
+ .map_err(DeError::custom),
+ 3 => serde_json::from_value::<Group>(Value::Object(v))
+ .map(|x| Channel::Group(Arc::new(RwLock::new(x))))
+ .map_err(DeError::custom),
+ _ => Err(DeError::custom("Unknown channel type")),
+ }
+ }
+}
+
impl Display for Channel {
/// Formats the channel into a "mentioned" string.
///
@@ -339,22 +368,101 @@ impl Display for Channel {
}
}
-impl PermissionOverwrite {
- #[doc(hidden)]
- pub fn decode(value: Value) -> Result<PermissionOverwrite> {
- let mut map = into_map(value)?;
- let id = remove(&mut map, "id").and_then(decode_id)?;
- let kind = remove(&mut map, "type").and_then(into_string)?;
- let kind = match &*kind {
- "member" => PermissionOverwriteType::Member(UserId(id)),
- "role" => PermissionOverwriteType::Role(RoleId(id)),
- _ => return Err(Error::Decode("Expected valid PermissionOverwrite type", Value::String(kind))),
+enum_number!(
+ /// A representation of a type of channel.
+ ChannelType {
+ #[doc="An indicator that the channel is a text [`GuildChannel`].
+
+[`GuildChannel`]: struct.GuildChannel.html"]
+ Text = 0,
+ #[doc="An indicator that the channel is a [`PrivateChannel`].
+
+[`PrivateChannel`]: struct.PrivateChannel.html"]
+ Private = 1,
+ #[doc="An indicator that the channel is a voice [`GuildChannel`].
+
+[`GuildChannel`]: struct.GuildChannel.html"]
+ Voice = 2,
+ #[doc="An indicator that the channel is the channel of a [`Group`].
+
+[`Group`]: struct.Group.html"]
+ Group = 3,
+ }
+);
+
+impl ChannelType {
+ pub fn name(&self) -> &str {
+ match *self {
+ ChannelType::Group => "group",
+ ChannelType::Private => "private",
+ ChannelType::Text => "text",
+ ChannelType::Voice => "voice",
+ }
+ }
+}
+
+#[derive(Deserialize)]
+struct PermissionOverwriteData {
+ allow: Permissions,
+ deny: Permissions,
+ id: u64,
+ #[serde(rename="type")]
+ kind: String,
+}
+
+/// A channel-specific permission overwrite for a member or role.
+#[derive(Clone, Debug)]
+pub struct PermissionOverwrite {
+ pub allow: Permissions,
+ pub deny: Permissions,
+ pub kind: PermissionOverwriteType,
+}
+
+impl Deserialize for PermissionOverwrite {
+ fn deserialize<D: Deserializer>(deserializer: D) -> StdResult<PermissionOverwrite, D::Error> {
+ let data = PermissionOverwriteData::deserialize(deserializer)?;
+
+ let kind = match &data.kind[..] {
+ "member" => PermissionOverwriteType::Member(UserId(data.id)),
+ "role" => PermissionOverwriteType::Role(RoleId(data.id)),
+ _ => return Err(DeError::custom("Unknown PermissionOverwriteType")),
};
Ok(PermissionOverwrite {
+ allow: data.allow,
+ deny: data.deny,
kind: kind,
- allow: remove(&mut map, "allow").and_then(Permissions::decode)?,
- deny: remove(&mut map, "deny").and_then(Permissions::decode)?,
})
}
}
+
+/// The type of edit being made to a Channel's permissions.
+///
+/// This is for use with methods such as `Context::create_permission`.
+///
+/// [`Context::create_permission`]: ../client/
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+pub enum PermissionOverwriteType {
+ /// A member which is having its permission overwrites edited.
+ Member(UserId),
+ /// A role which is having its permission overwrites edited.
+ Role(RoleId),
+}
+
+/// The results of a search, including the total results and a vector of
+/// messages.
+#[derive(Clone, Debug, Deserialize)]
+pub struct SearchResult {
+ /// An amount of messages returned from the result.
+ ///
+ /// Note that this is a vectof of a vector of messages. Each "set" of
+ /// messages contains the "found" message, as well as optional surrounding
+ /// messages for context.
+ #[serde(rename="messages")]
+ pub results: Vec<Vec<Message>>,
+ /// The number of messages directly related to the search.
+ ///
+ /// This does not count contextual messages.
+ #[serde(rename="total_results")]
+ pub total: u64,
+}