Skip to content

Commit

Permalink
Allow time::Tm to be passed into embed timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Hellyer committed Dec 19, 2016
1 parent dd7a32d commit b001234
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
53 changes: 44 additions & 9 deletions src/utils/builder/create_embed.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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<T: Into<Timestamp>>(mut self, timestamp: T) -> Self {
self.0.insert("timestamp".to_owned(), Value::String(timestamp.into().ts));

CreateEmbed(self.0)
}
Expand Down Expand Up @@ -240,7 +255,7 @@ impl From<Embed> for CreateEmbed {
}

if let Some(timestamp) = embed.timestamp {
b = b.timestamp(&timestamp);
b = b.timestamp(timestamp);
}

if let Some(thumbnail) = embed.thumbnail {
Expand Down Expand Up @@ -405,3 +420,23 @@ impl CreateEmbedThumbnail {
CreateEmbedThumbnail(self.0.insert("width", width))
}
}

pub struct Timestamp {
pub ts: String,
}

impl From<String> for Timestamp {
fn from(ts: String) -> Self {
Timestamp {
ts: ts,
}
}
}

impl From<Tm> for Timestamp {
fn from(tm: Tm) -> Self {
Timestamp {
ts: tm.to_utc().rfc3339().to_string(),
}
}
}
2 changes: 1 addition & 1 deletion tests/test_create_embed.rs
Expand Up @@ -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),
Expand Down

0 comments on commit b001234

Please sign in to comment.