diff options
| author | Zeyla Hellyer <[email protected]> | 2017-04-11 08:15:37 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-04-11 10:52:43 -0700 |
| commit | f6b27eb39c042e6779edc2d5d4b6e6c27d133eaf (patch) | |
| tree | a6169fee3bf9ea75391101577dcb2982e3daa388 /src/model/channel/embed.rs | |
| parent | Clippy lints + permission byte literals (diff) | |
| download | serenity-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/embed.rs')
| -rw-r--r-- | src/model/channel/embed.rs | 148 |
1 files changed, 146 insertions, 2 deletions
diff --git a/src/model/channel/embed.rs b/src/model/channel/embed.rs index 32c3722..b278622 100644 --- a/src/model/channel/embed.rs +++ b/src/model/channel/embed.rs @@ -1,6 +1,57 @@ -use serde_json::Value; -use ::model::Embed; use ::utils::builder::CreateEmbed; +use ::utils::Colour; +use ::internal::prelude::*; + +/// Represents a rich embed which allows using richer markdown, multiple fields +/// and more. This was heavily inspired by [slack's attachments]. +/// +/// You can include an attachment in your own message by a user or a bot, or in +/// a webhook. +/// +/// **Note**: Maximum amount of characters you can put is 256 in a field name, +/// 1024 in a field value, and 2048 in a description. +/// +/// [slack's attachments]: https://api.slack.com/docs/message-attachments +#[derive(Clone, Debug, Deserialize)] +pub struct Embed { + /// Information about the author of the embed. + pub author: Option<EmbedAuthor>, + /// The colour code of the embed. + #[serde(default, rename="color")] + pub colour: Colour, + /// The description of the embed. + /// + /// The maximum value for this field is 2048 unicode codepoints. + pub description: Option<String>, + /// The array of fields. + /// + /// The maximum number of fields is 25. + #[serde(default)] + pub fields: Vec<EmbedField>, + /// Image information of the embed. + pub image: Option<EmbedImage>, + /// The type of the embed. For embeds not generated by Discord's backend, + /// this will always be "rich". + #[serde(rename="type")] + pub kind: String, + /// Provider information for the embed. + /// + /// For example, if the embed [`kind`] is `"video"`, the provider might + /// contain information about YouTube. + pub provider: Option<EmbedProvider>, + /// Thumbnail information of the embed. + pub thumbnail: Option<EmbedThumbnail>, + /// Timestamp information. + pub timestamp: Option<String>, + /// The title of the embed. + pub title: Option<String>, + /// The URL of the embed. + pub url: Option<String>, + /// The embed's video information. + /// + /// This is present if the [`kind`] is `"video"`. + pub video: Option<EmbedVideo>, +} impl Embed { /// Creates a fake Embed, giving back a `serde_json` map. @@ -13,3 +64,96 @@ impl Embed { Value::Object(f(CreateEmbed::default()).0) } } + +/// An author object in an embed. +#[derive(Clone, Debug, Deserialize)] +pub struct EmbedAuthor { + /// The URL of the author icon. + /// + /// This only supports HTTP(S). + pub icon_url: Option<String>, + /// The name of the author. + pub name: String, + /// A proxied URL of the author icon. + pub proxy_icon_url: Option<String>, + /// The URL of the author. + pub url: Option<String>, +} + +/// A field object in an embed. +#[derive(Clone, Debug, Deserialize)] +pub struct EmbedField { + /// Indicator of whether the field should display as inline. + pub inline: bool, + /// The name of the field. + /// + /// The maximum length of this field is 512 unicode codepoints. + pub name: String, + /// The value of the field. + /// + /// The maxiumum length of this field is 1024 unicode codepoints. + pub value: String, +} + +/// Footer information for an embed. +#[derive(Clone, Debug, Deserialize)] +pub struct EmbedFooter { + /// The URL of the footer icon. + /// + /// This only supports HTTP(S). + pub icon_url: String, + /// A proxied URL of the footer icon. + pub proxy_icon_url: String, + /// The associated text with the footer. + pub text: String, +} + +/// An image object in an embed. +#[derive(Clone, Debug, Deserialize)] +pub struct EmbedImage { + /// The height of the image. + pub height: u64, + /// A proxied URL of the image. + pub proxy_url: String, + /// Source URL of the image. + /// + /// This only supports HTTP(S). + pub url: String, + /// The width of the image. + pub width: u64, +} + +/// The provider of an embed. +#[derive(Clone, Debug, Deserialize)] +pub struct EmbedProvider { + /// The name of the provider. + pub name: String, + /// The URL of the provider. + pub url: Option<String>, +} + +/// The dimensions and URL of an embed thumbnail. +#[derive(Clone, Debug, Deserialize)] +pub struct EmbedThumbnail { + /// The height of the thumbnail in pixels. + pub height: u64, + /// A proxied URL of the thumbnail. + pub proxy_url: String, + /// The source URL of the thumbnail. + /// + /// This only supports HTTP(S). + pub url: String, + /// The width of the thumbnail in pixels. + pub width: u64, +} + +/// Video information for an embed. +#[derive(Clone, Debug, Deserialize)] +pub struct EmbedVideo { + /// The height of the video in pixels. + pub height: u64, + /// The source URL of the video. + pub url: String, + /// The width of the video in pixels. + pub width: u64, +} |