Skip to content

Commit

Permalink
Implement From<Embed> for CreateEmbed
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Hellyer committed Dec 8, 2016
1 parent f69512b commit 7914274
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 9 deletions.
16 changes: 8 additions & 8 deletions definitions/structs/embed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 70 additions & 1 deletion src/utils/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -189,6 +190,74 @@ impl Default for CreateEmbed {
}
}

impl From<Embed> 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(&timestamp);
}

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.
///
Expand Down Expand Up @@ -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))
}
}

Expand Down
77 changes: 77 additions & 0 deletions tests/test_create_embed.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 7914274

Please sign in to comment.