Skip to content

Commit

Permalink
Format the repository and add a workflow for formatting and linting (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis authored Jan 18, 2021
1 parent abfbb9f commit 9bbb25a
Show file tree
Hide file tree
Showing 112 changed files with 5,544 additions and 4,322 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copied from Twilight's Lint workflow.
#
# https://github.com/twilight-rs/twilight/blob/trunk/.github/workflows/lint.yml
name: Lint

on: [push, pull_request]

jobs:
clippy:
name: Clippy
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
id: toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: clippy
profile: minimal
override: true

- name: Setup cache
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-clippy-rustc-${{ steps.toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }}

- name: Run clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --workspace --tests

rustfmt:
name: Format
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: rustfmt
profile: minimal
override: true

- name: Run cargo fmt
run: cargo fmt --all -- --check
36 changes: 18 additions & 18 deletions benches/bench_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod benches {
extern crate test;

use serenity::framework::standard::{Args, Delimiter};

use self::test::Bencher;

#[bench]
Expand All @@ -18,40 +19,35 @@ mod benches {
#[bench]
fn single_with_one_delimiter_and_long_string(b: &mut Bencher) {
b.iter(|| {
let mut args = Args::new(
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25",
&[Delimiter::Single(',')],
);
let mut args =
Args::new("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25", &[
Delimiter::Single(','),
]);
args.single::<String>().unwrap();
})
}

#[bench]
fn single_with_three_delimiters(b: &mut Bencher) {
b.iter(|| {
let mut args = Args::new(
"1,2 @3@4 5,",
&[
Delimiter::Single(','),
Delimiter::Single(' '),
Delimiter::Single('@'),
],
);
let mut args = Args::new("1,2 @3@4 5,", &[
Delimiter::Single(','),
Delimiter::Single(' '),
Delimiter::Single('@'),
]);
args.single::<String>().unwrap();
})
}

#[bench]
fn single_with_three_delimiters_and_long_string(b: &mut Bencher) {
b.iter(|| {
let mut args = Args::new(
"1,2 @3@4 5,1,2 @3@4 5,1,2 @3@4 5,1,2 @3@4 5,1,2 @3@4 5,1,2 @3@4 5,",
&[
let mut args =
Args::new("1,2 @3@4 5,1,2 @3@4 5,1,2 @3@4 5,1,2 @3@4 5,1,2 @3@4 5,1,2 @3@4 5,", &[
Delimiter::Single(','),
Delimiter::Single(' '),
Delimiter::Single('@'),
],
);
]);
args.single::<String>().unwrap();
})
}
Expand All @@ -75,7 +71,11 @@ mod benches {
#[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('<')]);
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();
})
}
Expand Down
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_marker", feature = "native_tls_backend_marker"))))]
compile_error!("You have the `http` or `gateway` feature enabled, \
#[cfg(all(
any(feature = "http", feature = "gateway"),
not(any(feature = "rustls_backend_marker", feature = "native_tls_backend_marker"))
))]
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() {}
44 changes: 12 additions & 32 deletions command_attr/src/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::{self, Write};

use proc_macro2::Span;
use syn::parse::{Error, Result};
use syn::spanned::Spanned;
Expand All @@ -6,8 +8,6 @@ use syn::{Attribute, Ident, Lit, LitStr, Meta, NestedMeta, Path};
use crate::structures::{Checks, Colour, HelpBehaviour, OnlyIn, Permissions};
use crate::util::{AsOption, LitExt};

use std::fmt::{self, Write};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ValueKind {
// #[<name>]
Expand Down Expand Up @@ -36,24 +36,15 @@ impl fmt::Display for ValueKind {

fn to_ident(p: Path) -> Result<Ident> {
if p.segments.is_empty() {
return Err(Error::new(
p.span(),
"cannot convert an empty path to an identifier",
));
return Err(Error::new(p.span(), "cannot convert an empty path to an identifier"));
}

if p.segments.len() > 1 {
return Err(Error::new(
p.span(),
"the path must not have more than one segment",
));
return Err(Error::new(p.span(), "the path must not have more than one segment"));
}

if !p.segments[0].arguments.is_empty() {
return Err(Error::new(
p.span(),
"the singular path segment must not have any arguments",
));
return Err(Error::new(p.span(), "the singular path segment must not have any arguments"));
}

Ok(p.segments[0].ident.clone())
Expand Down Expand Up @@ -87,7 +78,7 @@ pub fn parse_values(attr: &Attribute) -> Result<Values> {
let name = to_ident(path)?;

Ok(Values::new(name, ValueKind::Name, Vec::new(), attr.span()))
}
},
Meta::List(meta) => {
let name = to_ident(meta.path)?;
let nested = meta.nested;
Expand All @@ -113,20 +104,16 @@ pub fn parse_values(attr: &Attribute) -> Result<Values> {
}
}

let kind = if lits.len() == 1 {
ValueKind::SingleList
} else {
ValueKind::List
};
let kind = if lits.len() == 1 { ValueKind::SingleList } else { ValueKind::List };

Ok(Values::new(name, kind, lits, attr.span()))
}
},
Meta::NameValue(meta) => {
let name = to_ident(meta.path)?;
let lit = meta.lit;

Ok(Values::new(name, ValueKind::Equals, vec![lit], attr.span()))
}
},
}
}

Expand All @@ -146,7 +133,7 @@ impl<'a, T: fmt::Display> fmt::Display for DisplaySlice<'a, T> {
f.write_char('\n')?;
write!(f, "{}: {}", idx, elem)?;
}
}
},
}

Ok(())
Expand All @@ -168,10 +155,7 @@ fn validate(values: &Values, forms: &[ValueKind]) -> Result<()> {
return Err(Error::new(
values.span,
// Using the `_args` version here to avoid an allocation.
format_args!(
"the attribute must be in of these forms:\n{}",
DisplaySlice(forms)
),
format_args!("the attribute must be in of these forms:\n{}", DisplaySlice(forms)),
));
}

Expand All @@ -191,11 +175,7 @@ impl AttributeOption for Vec<String> {
fn parse(values: Values) -> Result<Self> {
validate(&values, &[ValueKind::List])?;

Ok(values
.literals
.into_iter()
.map(|lit| lit.to_str())
.collect())
Ok(values.literals.into_iter().map(|lit| lit.to_str()).collect())
}
}

Expand Down
Loading

0 comments on commit 9bbb25a

Please sign in to comment.