Skip to content

Commit

Permalink
Format the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Jan 7, 2021
1 parent ae1411b commit 1a0bf04
Show file tree
Hide file tree
Showing 96 changed files with 5,366 additions and 3,495 deletions.
19 changes: 15 additions & 4 deletions benches/bench_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
mod benches {
extern crate test;

use serenity::framework::standard::{Args, Delimiter};
use self::test::Bencher;
use serenity::framework::standard::{Args, Delimiter};

#[bench]
fn single_with_one_delimiter(b: &mut Bencher) {
Expand Down Expand Up @@ -68,15 +68,26 @@ mod benches {
fn iter_with_one_delimiter(b: &mut Bencher) {
b.iter(|| {
let mut args = Args::new("1,2,3,4,5,6,7,8,9,10", &[Delimiter::Single(',')]);
args.iter::<String>().collect::<Result<Vec<_>, _>>().unwrap();
args.iter::<String>()
.collect::<Result<Vec<_>, _>>()
.unwrap();
})
}

#[bench]
fn iter_with_three_delimiters(b: &mut Bencher) {
b.iter(|| {
let mut args = Args::new("1-2<3,4,5,6,7<8,9,10", &[Delimiter::Single(','), Delimiter::Single('-'), Delimiter::Single('<')]);
args.iter::<String>().collect::<Result<Vec<_>, _>>().unwrap();
let mut args = Args::new(
"1-2<3,4,5,6,7<8,9,10",
&[
Delimiter::Single(','),
Delimiter::Single('-'),
Delimiter::Single('<'),
],
);
args.iter::<String>()
.collect::<Result<Vec<_>, _>>()
.unwrap();
})
}
}
12 changes: 8 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#[cfg(all(any(feature = "http", feature = "gateway"),
not(any(feature = "rustls_backend", feature = "native_tls_backend"))))]
compile_error!("You have the `http` or `gateway` feature enabled, \
#[cfg(all(
any(feature = "http", feature = "gateway"),
not(any(feature = "rustls_backend", feature = "native_tls_backend"))
))]
compile_error!(
"You have the `http` or `gateway` feature enabled, \
either the `rustls_backend` or `native_tls_backend` feature must be
selected to let Serenity use `http` or `gateway`.\n\
- `rustls_backend` uses Rustls, a pure Rust TLS-implemenation.\n\
- `native_tls_backend` uses SChannel on Windows, Secure Transport on macOS, \
and OpenSSL on other platforms.\n\
If you are unsure, go with `rustls_backend`.");
If you are unsure, go with `rustls_backend`."
);

fn main() {}
28 changes: 20 additions & 8 deletions src/builder/create_allowed_mentions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::model::id::{UserId, RoleId};
use crate::model::id::{RoleId, UserId};

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use serde::{Serialize, Deserialize};

use std::collections::HashMap;

Expand Down Expand Up @@ -102,9 +102,15 @@ impl CreateAllowedMentions {
/// Sets the users that will be allowed to be mentioned.
#[inline]
pub fn users<U: Into<UserId>>(&mut self, users: impl IntoIterator<Item = U>) -> &mut Self {
self.0.insert("users", Value::Array({
users.into_iter().map(|i| json!(i.into().to_string())).collect::<Vec<_>>()
}));
self.0.insert(
"users",
Value::Array({
users
.into_iter()
.map(|i| json!(i.into().to_string()))
.collect::<Vec<_>>()
}),
);
self
}

Expand All @@ -122,9 +128,15 @@ impl CreateAllowedMentions {
/// Sets the roles that will be allowed to be mentioned.
#[inline]
pub fn roles<R: Into<RoleId>>(&mut self, users: impl IntoIterator<Item = R>) -> &mut Self {
self.0.insert("roles", Value::Array({
users.into_iter().map(|i| json!(i.into().to_string())).collect::<Vec<_>>()
}));
self.0.insert(
"roles",
Value::Array({
users
.into_iter()
.map(|i| json!(i.into().to_string()))
.collect::<Vec<_>>()
}),
);
self
}

Expand Down
45 changes: 27 additions & 18 deletions src/builder/create_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ impl CreateChannel {
}
/// Specify what type the channel is, whether it's a text, voice, category or news channel.
pub fn kind(&mut self, kind: ChannelType) -> &mut Self {
self.0.insert("type", Value::Number(Number::from(kind as u8)));
self.0
.insert("type", Value::Number(Number::from(kind as u8)));

self
}

/// Specifiy the category, the "parent" of this channel.
pub fn category<I: Into<ChannelId>>(&mut self, id: I) -> &mut Self {
self.0.insert("parent_id", Value::Number(Number::from(id.into().0)));
self.0
.insert("parent_id", Value::Number(Number::from(id.into().0)));

self
}
Expand Down Expand Up @@ -63,7 +65,8 @@ impl CreateChannel {

/// [Voice-only] Set how many users may occupy this voice channel.
pub fn user_limit(&mut self, limit: u32) -> &mut Self {
self.0.insert("user_limit", Value::Number(Number::from(limit)));
self.0
.insert("user_limit", Value::Number(Number::from(limit)));

self
}
Expand All @@ -75,7 +78,8 @@ impl CreateChannel {
///
/// **Note**: Must be between 0 and 21600 seconds (360 minutes or 6 hours).
pub fn rate_limit(&mut self, limit: u64) -> &mut Self {
self.0.insert("rate_limit_per_user", Value::Number(Number::from(limit)));
self.0
.insert("rate_limit_per_user", Value::Number(Number::from(limit)));

self
}
Expand Down Expand Up @@ -121,23 +125,28 @@ impl CreateChannel {
/// # }
/// ```
pub fn permissions<I>(&mut self, perms: I) -> &mut Self
where I: IntoIterator<Item=PermissionOverwrite>
where
I: IntoIterator<Item = PermissionOverwrite>,
{
let overwrites = perms.into_iter().map(|perm| {
let (id, kind) = match perm.kind {
PermissionOverwriteType::Member(id) => (id.0, "member"),
PermissionOverwriteType::Role(id) => (id.0, "role"),
};

json!({
"allow": perm.allow.bits(),
"deny": perm.deny.bits(),
"id": id,
"type": kind,
let overwrites = perms
.into_iter()
.map(|perm| {
let (id, kind) = match perm.kind {
PermissionOverwriteType::Member(id) => (id.0, "member"),
PermissionOverwriteType::Role(id) => (id.0, "role"),
};

json!({
"allow": perm.allow.bits(),
"deny": perm.deny.bits(),
"id": id,
"type": kind,
})
})
}).collect();
.collect();

self.0.insert("permission_overwrites", Value::Array(overwrites));
self.0
.insert("permission_overwrites", Value::Array(overwrites));

self
}
Expand Down
73 changes: 42 additions & 31 deletions src/builder/create_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::utils;
use chrono::{DateTime, TimeZone};
use serde_json::{json, Value};

use std::fmt::Display;
use std::collections::HashMap;
use std::fmt::Display;

#[cfg(feature = "utils")]
use crate::utils::Colour;
Expand All @@ -47,7 +47,9 @@ impl CreateEmbed {
/// Refer to the documentation for [`CreateEmbedAuthor`] for more
/// information.
pub fn author<F>(&mut self, f: F) -> &mut Self
where F: FnOnce(&mut CreateEmbedAuthor) -> &mut CreateEmbedAuthor {
where
F: FnOnce(&mut CreateEmbedAuthor) -> &mut CreateEmbedAuthor,
{
let mut author = CreateEmbedAuthor::default();
f(&mut author);
self.set_author(author)
Expand Down Expand Up @@ -83,10 +85,8 @@ impl CreateEmbed {

#[cfg(feature = "utils")]
fn _colour(&mut self, colour: Colour) {
self.0.insert(
"color",
Value::Number(Number::from(u64::from(colour.0))),
);
self.0
.insert("color", Value::Number(Number::from(u64::from(colour.0))));
}

/// Set the colour of the left-hand side of the embed.
Expand All @@ -113,7 +113,8 @@ impl CreateEmbed {
/// **Note**: This can't be longer than 2048 characters.
#[inline]
pub fn description<D: ToString>(&mut self, description: D) -> &mut Self {
self.0.insert("description", Value::String(description.to_string()));
self.0
.insert("description", Value::String(description.to_string()));
self
}

Expand All @@ -123,15 +124,19 @@ impl CreateEmbed {
/// **Note**: Maximum amount of characters you can put is 256 in a field
/// name and 1024 in a field value.
#[inline]
pub fn field<T, U>(&mut self, name: T, value: U, inline: bool) -> &mut Self
where T: ToString, U: ToString {
pub fn field<T, U>(&mut self, name: T, value: U, inline: bool) -> &mut Self
where
T: ToString,
U: ToString,
{
self._field(name.to_string(), value.to_string(), inline);
self
}

fn _field(&mut self, name: String, value: String, inline: bool) {
{
let entry = self.0
let entry = self
.0
.entry("fields")
.or_insert_with(|| Value::Array(vec![]));

Expand All @@ -151,9 +156,11 @@ impl CreateEmbed {
///
/// [`field`]: Self::field
pub fn fields<T, U, It>(&mut self, fields: It) -> &mut Self
where It: IntoIterator<Item=(T, U, bool)>,
T: ToString,
U: ToString {
where
It: IntoIterator<Item = (T, U, bool)>,
T: ToString,
U: ToString,
{
for (name, value, inline) in fields {
self.field(name, value, inline);
}
Expand All @@ -166,7 +173,9 @@ impl CreateEmbed {
/// Refer to the documentation for [`CreateEmbedFooter`] for more
/// information.
pub fn footer<F>(&mut self, f: F) -> &mut Self
where F: FnOnce(&mut CreateEmbedFooter) -> &mut CreateEmbedFooter {
where
F: FnOnce(&mut CreateEmbedFooter) -> &mut CreateEmbedFooter,
{
let mut create_embed_footer = CreateEmbedFooter::default();
f(&mut create_embed_footer);
self.set_footer(create_embed_footer)
Expand Down Expand Up @@ -439,7 +448,8 @@ pub struct CreateEmbedAuthor(pub HashMap<&'static str, Value>);
impl CreateEmbedAuthor {
/// Set the URL of the author's icon.
pub fn icon_url<S: ToString>(&mut self, icon_url: S) -> &mut Self {
self.0.insert("icon_url", Value::String(icon_url.to_string()));
self.0
.insert("icon_url", Value::String(icon_url.to_string()));
self
}

Expand Down Expand Up @@ -468,7 +478,8 @@ pub struct CreateEmbedFooter(pub HashMap<&'static str, Value>);
impl CreateEmbedFooter {
/// Set the icon URL's value. This only supports HTTP(S).
pub fn icon_url<S: ToString>(&mut self, icon_url: S) -> &mut Self {
self.0.insert("icon_url", Value::String(icon_url.to_string()));
self.0
.insert("icon_url", Value::String(icon_url.to_string()));
self
}

Expand All @@ -486,22 +497,20 @@ pub struct Timestamp {

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

impl<'a> From<&'a str> for Timestamp {
fn from(ts: &'a str) -> Self {
Self {
ts: ts.to_string(),
}
Self { ts: ts.to_string() }
}
}

impl<'a, Tz: TimeZone> From<&'a DateTime<Tz>> for Timestamp
where Tz::Offset: Display {
where
Tz::Offset: Display,
{
fn from(dt: &'a DateTime<Tz>) -> Self {
Self {
ts: dt.to_rfc3339(),
Expand All @@ -511,10 +520,12 @@ impl<'a, Tz: TimeZone> From<&'a DateTime<Tz>> for Timestamp

#[cfg(test)]
mod test {
use crate::{model::channel::{Embed, EmbedField, EmbedFooter, EmbedImage, EmbedVideo},
utils::{self, Colour}};
use serde_json::{json, Value};
use super::CreateEmbed;
use crate::{
model::channel::{Embed, EmbedField, EmbedFooter, EmbedImage, EmbedVideo},
utils::{self, Colour},
};
use serde_json::{json, Value};

#[test]
fn test_from_embed() {
Expand Down Expand Up @@ -559,11 +570,11 @@ mod test {
};

let mut builder = CreateEmbed::from(embed);
builder.colour(0xFF0011);
builder.description("This is a hakase description");
builder.image("https://i.imgur.com/XfWpfCV.gif");
builder.title("still a hakase");
builder.url("https://i.imgur.com/XfWpfCV.gif");
builder.colour(0xFF0011);
builder.description("This is a hakase description");
builder.image("https://i.imgur.com/XfWpfCV.gif");
builder.title("still a hakase");
builder.url("https://i.imgur.com/XfWpfCV.gif");

let built = Value::Object(utils::hashmap_to_json_map(builder.0));

Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_invite.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::internal::prelude::*;
use std::collections::HashMap;
use serde_json::Value;
use std::collections::HashMap;

/// A builder to create a [`RichInvite`] for use via [`GuildChannel::create_invite`].
///
Expand Down
Loading

0 comments on commit 1a0bf04

Please sign in to comment.