From b0012349cca2a5c7c62bb6d2c99106d245b6c55a Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Sun, 18 Dec 2016 20:46:11 -0800 Subject: [PATCH] Allow time::Tm to be passed into embed timestamp --- src/utils/builder/create_embed.rs | 53 +++++++++++++++++++++++++------ tests/test_create_embed.rs | 2 +- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/utils/builder/create_embed.rs b/src/utils/builder/create_embed.rs index 95da366dfac..e311dc36237 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 time::Tm; use ::model::Embed; use ::utils::Colour; @@ -160,15 +161,29 @@ impl CreateEmbed { /// Set the timestamp. /// - /// **Note:** This timestamp must be in ISO-8601 format. It must also be + /// **Note**: This timestamp must be in ISO-8601 format. It must also be /// in UTC format. - /// - /// # Examples - /// - /// `2017-01-03T23:00:00` - /// `2004-06-08T16:04:23` - pub fn timestamp(mut self, timestamp: &str) -> Self { - self.0.insert("timestamp".to_owned(), Value::String(timestamp.to_owned())); + /// + /// # Examples + /// + /// You may pass a direct string: + /// + /// - `2017-01-03T23:00:00` + /// - `2004-06-08T16:04:23` + /// - `2004-06-08T16:04:23` + /// + /// Or a `time::Tm`: + /// + /// ```rust,no_run + /// extern crate time; + /// + /// let now = time::now(); + /// + /// embed = embed.timestamp(now); + /// // ... + /// ``` + pub fn timestamp>(mut self, timestamp: T) -> Self { + self.0.insert("timestamp".to_owned(), Value::String(timestamp.into().ts)); CreateEmbed(self.0) } @@ -240,7 +255,7 @@ impl From for CreateEmbed { } if let Some(timestamp) = embed.timestamp { - b = b.timestamp(×tamp); + b = b.timestamp(timestamp); } if let Some(thumbnail) = embed.thumbnail { @@ -405,3 +420,23 @@ impl CreateEmbedThumbnail { CreateEmbedThumbnail(self.0.insert("width", width)) } } + +pub struct Timestamp { + pub ts: String, +} + +impl From for Timestamp { + fn from(ts: String) -> Self { + Timestamp { + ts: ts, + } + } +} + +impl From for Timestamp { + fn from(tm: Tm) -> Self { + Timestamp { + ts: tm.to_utc().rfc3339().to_string(), + } + } +} diff --git a/tests/test_create_embed.rs b/tests/test_create_embed.rs index e229ee9667b..ec499e16c80 100644 --- a/tests/test_create_embed.rs +++ b/tests/test_create_embed.rs @@ -8,7 +8,7 @@ use serenity::utils::builder::CreateEmbed; use serenity::utils::Colour; #[test] -fn from_embed() { +fn test_from_embed() { let embed = Embed { author: None, colour: Colour::new(0xFF0011),