Skip to content

Commit

Permalink
feat: add derives where appropriate
Browse files Browse the repository at this point in the history
Also some rustfmt updates.
  • Loading branch information
compressed committed Sep 17, 2018
1 parent 66765a1 commit d45da34
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 39 deletions.
10 changes: 5 additions & 5 deletions src/attachment.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use error::{Error, Result};
use {HexColor, SlackText, SlackTime, TryInto};
use chrono::NaiveDateTime;
use error::{Error, Result};
use reqwest::Url;
use {HexColor, SlackText, SlackTime, TryInto};

/// Slack allows for attachments to be added to messages. See
/// https://api.slack.com/docs/attachments for more information.
#[derive(Serialize, Debug, Default)]
#[derive(Serialize, Debug, Default, Clone, PartialEq)]
pub struct Attachment {
/// Required text for attachment.
/// Slack will use this text to display on devices that don't support markup.
Expand Down Expand Up @@ -82,7 +82,7 @@ pub enum Section {

/// Fields are defined as an array, and hashes contained within it will
/// be displayed in a table inside the message attachment.
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct Field {
/// Shown as a bold heading above the value text.
/// It cannot contain markup and will be escaped for you.
Expand All @@ -106,7 +106,7 @@ impl Field {
Field {
title: title.into(),
value: value.into(),
short: short,
short,
}
}
}
Expand Down
25 changes: 8 additions & 17 deletions src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use TryFrom;
/// 2. Any valid hex color code: e.g. `#b13d41` or `#000`.
///
/// hex color codes will be checked to ensure a valid hex number is provided
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct HexColor(String);
impl HexColor {
fn new<S: Into<String>>(s: S) -> HexColor {
Expand All @@ -30,7 +30,7 @@ impl ::std::fmt::Display for HexColor {

/// Default slack colors built-in to the API
/// See: https://api.slack.com/docs/attachments
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SlackColor {
/// green
Good,
Expand All @@ -40,14 +40,7 @@ pub enum SlackColor {
Danger,
}

// can't seem to convert enum to slice despite trait being implemented
// need this to support passing in the string directly
const SLACK_COLORS: [&'static str; 3] = [
// SlackColor::Good.as_slice(),
"good",
"warning",
"danger",
];
const SLACK_COLORS: [&str; 3] = ["good", "warning", "danger"];

impl ::std::fmt::Display for SlackColor {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
Expand Down Expand Up @@ -84,13 +77,11 @@ where

let num_chars = s.chars().count();
if num_chars != 7 && num_chars != 4 {
return Err(
ErrorKind::HexColor(format!(
"Must be 4 or 7 characters long (including #): \
found `{}`",
s
)).into(),
);
return Err(ErrorKind::HexColor(format!(
"Must be 4 or 7 characters long (including #): \
found `{}`",
s
)).into());
}
if !s.starts_with('#') {
return Err(ErrorKind::HexColor(format!("No leading #: found `{}`", s)).into());
Expand Down
32 changes: 15 additions & 17 deletions src/slack.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::str;
use error::{Error, ErrorKind, Result};
use {Payload, TryInto};
use serde::{Serialize, Serializer};
use chrono::NaiveDateTime;
use error::{Error, ErrorKind, Result};
use reqwest::{Client, StatusCode, Url};
use serde::{Serialize, Serializer};
use std::str;
use {Payload, TryInto};

/// Handles sending messages to slack
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Slack {
hook: Url,
client: Client,
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Slack {
}

/// Slack timestamp
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct SlackTime(NaiveDateTime);

impl SlackTime {
Expand All @@ -56,7 +56,7 @@ impl Serialize for SlackTime {

/// Representation of any text sent through slack
/// the text must be processed to escape specific characters
#[derive(Serialize, Debug, Default, Clone)]
#[derive(Serialize, Debug, Default, Clone, PartialEq)]
pub struct SlackText(String);

impl SlackText {
Expand Down Expand Up @@ -96,7 +96,7 @@ impl<'a> From<String> for SlackText {
/// Enum used for constructing a text field having both `SlackText`(s) and `SlackLink`(s). The
/// variants should be used together in a `Vec` on any function having a `Into<SlackText>` trait
/// bound. The combined text will be space-separated.
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub enum SlackTextContent {
/// Text that will be escaped via slack api rules
Text(SlackText),
Expand All @@ -108,13 +108,13 @@ pub enum SlackTextContent {

impl<'a> From<&'a [SlackTextContent]> for SlackText {
fn from(v: &[SlackTextContent]) -> SlackText {
let st = v.iter()
let st = v
.iter()
.map(|item| match *item {
SlackTextContent::Text(ref s) => format!("{}", s),
SlackTextContent::Link(ref link) => format!("{}", link),
SlackTextContent::User(ref u) => format!("{}", u),
})
.collect::<Vec<String>>()
}).collect::<Vec<String>>()
.join(" ");
SlackText::new_raw(st)
}
Expand All @@ -127,7 +127,7 @@ impl ::std::fmt::Display for SlackText {
}

/// Representation of a link sent in slack
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq)]
pub struct SlackLink {
/// URL for link.
///
Expand Down Expand Up @@ -163,12 +163,11 @@ impl Serialize for SlackLink {
}
}


/// Representation of a user id link sent in slack
///
/// Cannot do @UGUID|handle links using SlackLink in the future due to
/// https://api.slack.com/changelog/2017-09-the-one-about-usernames
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct SlackUserLink {
/// User ID (U1231232123) style
pub uid: String,
Expand Down Expand Up @@ -198,14 +197,13 @@ impl Serialize for SlackUserLink {
}
}


#[cfg(test)]
mod test {
use chrono::NaiveDateTime;
use slack::{Slack, SlackLink};
#[cfg(feature = "unstable")]
use test::Bencher;
use slack::{Slack, SlackLink};
use {serde_json, AttachmentBuilder, Field, Parse, PayloadBuilder, SlackText};
use chrono::NaiveDateTime;

#[test]
fn slack_incoming_url_test() {
Expand Down

0 comments on commit d45da34

Please sign in to comment.