From 79142745cb571ba2d4284fd1dcbe53c14a0ed623 Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Thu, 8 Dec 2016 10:22:46 -0800 Subject: [PATCH] Implement From for CreateEmbed --- definitions/structs/embed.yml | 16 +++---- src/utils/builder/create_embed.rs | 71 +++++++++++++++++++++++++++- tests/test_create_embed.rs | 77 +++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 9 deletions(-) create mode 100644 tests/test_create_embed.rs diff --git a/definitions/structs/embed.yml b/definitions/structs/embed.yml index 158e374132e..76919dd337b 100644 --- a/definitions/structs/embed.yml +++ b/definitions/structs/embed.yml @@ -46,22 +46,22 @@ fields: description: The provider information for the embed. optional: true type: EmbedProvider - - name: timestamp - description: Timestamp of embed content. - optional: true - type: string - - name: url - description: The URL of the embed. - optional: true - type: string - name: thumbnail description: The thumbnail provided for the embed. optional: true type: EmbedThumbnail + - name: timestamp + description: Timestamp of embed content. + optional: true + type: string - name: title description: The title of the embed. optional: true type: string + - name: url + description: The URL of the embed. + optional: true + type: string - name: video description: The embed's video information. optional: true diff --git a/src/utils/builder/create_embed.rs b/src/utils/builder/create_embed.rs index f9b7dcfe3eb..2b318df2045 100644 --- a/src/utils/builder/create_embed.rs +++ b/src/utils/builder/create_embed.rs @@ -19,6 +19,7 @@ use serde_json::builder::ObjectBuilder; use serde_json::Value; use std::collections::BTreeMap; use std::default::Default; +use ::model::Embed; use ::utils::Colour; /// A builder to create a fake [`Embed`] object, for use with the @@ -189,6 +190,74 @@ impl Default for CreateEmbed { } } +impl From for CreateEmbed { + /// Converts the fields of an embed into the values for a new embed builder. + /// + /// Some values - such as Proxy URLs - are not preserved. + fn from(embed: Embed) -> CreateEmbed { + let mut b = CreateEmbed::default() + .colour(embed.colour); + + if let Some(author) = embed.author { + b = b.author(move |mut a| { + a = a.name(&author.name); + + if let Some(icon_url) = author.icon_url { + a = a.icon_url(&icon_url); + } + + if let Some(url) = author.url { + a = a.url(&url); + } + + a + }); + } + + if let Some(description) = embed.description { + b = b.description(&description); + } + + if let Some(fields) = embed.fields { + for field in fields { + b = b.field(move |f| f + .inline(field.inline) + .name(&field.name) + .value(&field.value)); + } + } + + if let Some(image) = embed.image { + b = b.image(move |i| i + .height(image.height) + .url(&image.url) + .width(image.width)); + } + + if let Some(timestamp) = embed.timestamp { + b = b.timestamp(×tamp); + } + + if let Some(thumbnail) = embed.thumbnail { + b = b.thumbnail(move |t| t + .height(thumbnail.height) + .url(&thumbnail.url) + .width(thumbnail.width)); + } + + if let Some(url) = embed.url { + b = b.url(&url); + } + + if let Some(title) = embed.title { + b = b.title(&title); + } + + b + } +} + + /// A builder to create a fake [`Embed`] object's author, for use with the /// [`CreateEmbed::author`] method. /// @@ -308,7 +377,7 @@ impl CreateEmbedImage { /// Set the display width of the image. pub fn width(self, width: u64) -> Self { - CreateEmbedImage(self.0.insert("widht", width)) + CreateEmbedImage(self.0.insert("width", width)) } } diff --git a/tests/test_create_embed.rs b/tests/test_create_embed.rs new file mode 100644 index 00000000000..93a56a6e60d --- /dev/null +++ b/tests/test_create_embed.rs @@ -0,0 +1,77 @@ +extern crate serde_json; +extern crate serenity; + +use serde_json::builder::ObjectBuilder; +use serde_json::Value; +use serenity::model::{Embed, EmbedField, EmbedImage}; +use serenity::utils::builder::CreateEmbed; +use serenity::utils::Colour; + +#[test] +fn from_embed() { + let embed = Embed { + author: None, + colour: Colour::new(0xFF0011), + description: Some("This is a test description".to_owned()), + fields: Some(vec![ + EmbedField { + inline: false, + name: "a".to_owned(), + value: "b".to_owned(), + }, + EmbedField { + inline: true, + name: "c".to_owned(), + value: "z".to_owned(), + }, + ]), + image: Some(EmbedImage { + height: 213, + proxy_url: "a".to_owned(), + url: "https://i.imgur.com/q9MqLqZ.png".to_owned(), + width: 224, + }), + kind: "rich".to_owned(), + provider: None, + thumbnail: None, + timestamp: None, + title: Some("funny cat meme".to_owned()), + url: Some("https://i.imgur.com/q9MqLqZ.png".to_owned()), + video: None, + }; + + let builder = CreateEmbed::from(embed) + .colour(0xFF0000) + .description("This is a cat description") + .title("still a funny cat meme") + .url("https://i.imgur.com/q9MqLqZ.jpg") + .image(|i| i + .height(426) + .url("https://i.imgur.com/q9MqLqZ.jpg") + .width(448)); + + let built = Value::Object(builder.0); + + let obj = ObjectBuilder::new() + .insert("color", 0xFF0000) + .insert("description", "This is a cat description") + .insert_array("fields", |a| a + .push_object(|o| o + .insert("inline", false) + .insert("name", "a") + .insert("value", "b")) + .push_object(|o| o + .insert("inline", true) + .insert("name", "c") + .insert("value", "z"))) + .insert_object("image", |o| o + .insert("height", 426) + .insert("url", "https://i.imgur.com/q9MqLqZ.jpg") + .insert("width", 448)) + .insert("title", "still a funny cat meme") + .insert("type", "rich") + .insert("url", "https://i.imgur.com/q9MqLqZ.jpg") + .build(); + + assert_eq!(built, obj); +}