Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to 2018 edition #33

Merged
merged 8 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
# FIXME: currently `error_chain` causes some deprecation warnings, so
# temporaily allow warnings until that gets sorted out
# args: -- -D warnings
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ name = "slack-hook"
readme = "README.md"
repository = "https://github.com/frostly/rust-slack"
version = "0.8.0"
edition = "2018"

[dependencies]
chrono = "0.4"
Expand Down
8 changes: 5 additions & 3 deletions src/attachment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::error::{Error, Result};
use crate::{HexColor, SlackText, SlackTime};
use chrono::NaiveDateTime;
use error::{Error, Result};
use reqwest::Url;
use {HexColor, SlackText, SlackTime, TryInto};
use serde::Serialize;
use std::convert::TryInto;

/// Slack allows for attachments to be added to messages. See
/// https://api.slack.com/docs/attachments for more information.
Expand Down Expand Up @@ -192,7 +194,7 @@ impl AttachmentBuilder {
/// 3. Any valid hex color code: e.g. `#b13d41` or `#000`.
///
/// hex color codes will be checked to ensure a valid hex number is provided
pub fn color<C: TryInto<HexColor, Err = Error>>(self, color: C) -> AttachmentBuilder {
pub fn color<C: TryInto<HexColor, Error = Error>>(self, color: C) -> AttachmentBuilder {
match self.inner {
Ok(mut inner) => match color.try_into() {
Ok(c) => {
Expand Down
6 changes: 5 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
error_chain! {
types {
Error, ErrorKind, ResultExt, Result;
}

foreign_links {
Utf8(::std::str::Utf8Error) #[doc = "utf8 error, slack responses should be valid utf8"];
Serialize(::serde_json::error::Error) #[doc = "`serde_json::error::Error`"];
FromHex(::hexx::FromHexError) #[doc = "`rustc_serialize::hex::FromHexError`"];
FromHex(::hex::FromHexError) #[doc = "`rustc_serialize::hex::FromHexError`"];
Reqwest(::reqwest::Error) #[doc = "`reqwest::Error`"];
Url(::reqwest::UrlError) #[doc = "`reqwest::UrlError`"];
Io(::std::io::Error) #[doc = "`std::io::Error`"];
Expand Down
7 changes: 0 additions & 7 deletions src/helper.rs

This file was deleted.

39 changes: 20 additions & 19 deletions src/hex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use error::{Error, ErrorKind};
use hexx::FromHex;
use TryFrom;
use crate::error::{Error, ErrorKind};
use std::{convert::TryFrom, str::FromStr};

use hex::FromHex;
use serde::Serialize;

/// A `HexColor` `String` can be one of:
///
Expand All @@ -23,11 +25,19 @@ impl Default for HexColor {
}

impl ::std::fmt::Display for HexColor {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl TryFrom<&str> for HexColor {
type Error = Error;

fn try_from(s: &str) -> Result<Self, Self::Error> {
s.parse()
}
}

/// Default slack colors built-in to the API
/// See: https://api.slack.com/docs/attachments
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand All @@ -43,7 +53,7 @@ pub enum SlackColor {
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 {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "{}", self.as_ref())
}
}
Expand All @@ -64,12 +74,10 @@ impl From<SlackColor> for HexColor {
}
}

impl<S> TryFrom<S> for HexColor
where
S: Into<String>,
{
impl FromStr for HexColor {
type Err = Error;
fn try_from(s: S) -> ::std::result::Result<Self, Self::Err> {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let s: String = s.into();
if SLACK_COLORS.contains(&&s[..]) {
return Ok(HexColor(s));
Expand Down Expand Up @@ -107,18 +115,11 @@ where
}
}

// even though this will always succeed, it simplifies the trait bound in the builder
impl TryFrom<SlackColor> for HexColor {
type Err = Error;
fn try_from(color: SlackColor) -> ::std::result::Result<Self, Self::Err> {
Ok(color.into())
}
}

#[cfg(test)]
mod test {
use super::*;
use {HexColor, TryFrom};
use crate::HexColor;
use std::convert::TryFrom;

#[test]
fn test_hex_color_too_short() {
Expand Down
75 changes: 8 additions & 67 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,28 @@
unstable_features,
unused_import_braces,
unused_qualifications,
unused_results
unused_results,
rust_2018_idioms
)]
#![cfg_attr(test, deny(warnings))]

//! Library to send messages to slack rooms
//! supports entire messaging API, including attachments and fields
//! also support for built-in colors as well as any hex colors

extern crate reqwest;

extern crate chrono;
#[macro_use]
extern crate error_chain;
extern crate hex as hexx;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate url_serde;

pub use attachment::{Action, Attachment, AttachmentBuilder, Field, Section};
pub use error::{Error, Result};
pub use hex::{HexColor, SlackColor};
pub use payload::{Parse, Payload, PayloadBuilder};
pub use slack::{Slack, SlackLink, SlackText, SlackTextContent, SlackTime, SlackUserLink};
pub use crate::attachment::{Action, Attachment, AttachmentBuilder, Field, Section};
pub use crate::error::{Error, Result};
pub use crate::hex::{HexColor, SlackColor};
pub use crate::payload::{Parse, Payload, PayloadBuilder};
pub use crate::slack::{Slack, SlackLink, SlackText, SlackTextContent, SlackTime, SlackUserLink};

#[macro_use]
mod macros;

mod attachment;
mod error;
mod helper;
mod hex;
mod payload;
mod slack;

/// Waiting to stabilize: https://github.com/rust-lang/rust/issues/33417
///
/// An attempted conversion that consumes `self`, which may or may not be expensive.
///
/// Library authors should not directly implement this trait, but should prefer implementing
/// the [`TryFrom`] trait, which offers greater flexibility and provides an equivalent `TryInto`
/// implementation for free, thanks to a blanket implementation in the standard library.
///
/// [`TryFrom`]: trait.TryFrom.html
pub trait TryInto<T>: Sized {
/// The type returned in the event of a conversion error.
type Err;

/// Performs the conversion.
fn try_into(self) -> ::std::result::Result<T, Self::Err>;
}

/// Waiting to stabilize: https://github.com/rust-lang/rust/issues/33417
///
/// Attempt to construct `Self` via a conversion.
pub trait TryFrom<T>: Sized {
/// The type returned in the event of a conversion error.
type Err;

/// Performs the conversion.
fn try_from(_: T) -> ::std::result::Result<Self, Self::Err>;
}

impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,
{
type Err = U::Err;

fn try_into(self) -> ::std::result::Result<U, U::Err> {
U::try_from(self)
}
}

impl<'a> TryFrom<&'a str> for reqwest::Url {
type Err = Error;

fn try_from(s: &str) -> ::std::result::Result<Self, Self::Err> {
match s.parse() {
Ok(u) => Ok(u),
Err(e) => Err(e.into()),
}
}
}
6 changes: 3 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ macro_rules! url_builder_fn {
$name:ident, $builder:ident
} => {
$(#[$meta])+
pub fn $name<U: TryInto<::reqwest::Url, Err = Error>>(self, $name: U) -> $builder {
pub fn $name<U: ::reqwest::IntoUrl>(self, $name: U) -> $builder {
match self.inner {
Ok(mut inner) => {
match $name.try_into() {
match $name.into_url() {
Ok(url) => {
inner.$name = Some(url);
$builder { inner: Ok(inner) }
}
Err(e) => $builder { inner: Err(e) },
Err(e) => $builder { inner: Err(e.into()) },
}
}
_ => self,
Expand Down
7 changes: 3 additions & 4 deletions src/payload.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use error::{Error, Result};
use helper::bool_to_u8;
use crate::error::Result;
use crate::{Attachment, SlackText};
use reqwest::Url;
use serde::{Serialize, Serializer};
use {Attachment, SlackText, TryInto};

/// Payload to send to slack
/// https://api.slack.com/incoming-webhooks
Expand Down Expand Up @@ -178,7 +177,7 @@ impl PayloadBuilder {
pub fn link_names(self, b: bool) -> PayloadBuilder {
match self.inner {
Ok(mut inner) => {
inner.link_names = Some(bool_to_u8(b));
inner.link_names = Some(u8::from(b));
PayloadBuilder { inner: Ok(inner) }
}
_ => self,
Expand Down
18 changes: 9 additions & 9 deletions src/slack.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::error::{ErrorKind, Result};
use crate::Payload;
use chrono::NaiveDateTime;
use error::{Error, ErrorKind, Result};
use reqwest::{Client, Url};
use serde::{Serialize, Serializer};
use std::fmt;
use {Payload, TryInto};

/// Handles sending messages to slack
#[derive(Debug, Clone)]
Expand All @@ -14,9 +14,9 @@ pub struct Slack {

impl Slack {
/// Construct a new instance of slack for a specific incoming url endpoint.
pub fn new<T: TryInto<Url, Err = Error>>(hook: T) -> Result<Slack> {
pub fn new<T: reqwest::IntoUrl>(hook: T) -> Result<Slack> {
Ok(Slack {
hook: hook.try_into()?,
hook: hook.into_url()?,
client: Client::new(),
})
}
Expand Down Expand Up @@ -121,7 +121,7 @@ impl<'a> From<&'a [SlackTextContent]> for SlackText {
}

impl fmt::Display for SlackText {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ impl SlackLink {
}

impl fmt::Display for SlackLink {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<{}|{}>", self.url, self.text)
}
}
Expand Down Expand Up @@ -183,7 +183,7 @@ impl SlackUserLink {
}

impl fmt::Display for SlackUserLink {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<{}>", self.uid)
}
}
Expand All @@ -199,11 +199,11 @@ impl Serialize for SlackUserLink {

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

#[test]
fn slack_incoming_url_test() {
Expand Down