Skip to content

Commit

Permalink
Add more emoji features + valid url testing
Browse files Browse the repository at this point in the history
  • Loading branch information
1Git2Clone committed Dec 15, 2024
1 parent 9c06170 commit a47d8fd
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ chrono = "0.4.35"
rayon = "1.10.0"
regex = "1.11.1"
reqwest = "0.12.9"
serenity_discord_bot_derive = { path = "./serenity_discord_bot_derive" }

[features]
serde = ["dep:serde"]
Expand Down
46 changes: 46 additions & 0 deletions serenity_discord_bot_derive/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions serenity_discord_bot_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "serenity_discord_bot_derive"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
quote = "1.0.37"
syn = "2.0.90"
30 changes: 30 additions & 0 deletions serenity_discord_bot_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;

// Credit:
// https://stackoverflow.com/questions/68025264/how-to-get-all-the-variants-of-an-enum-in-a-vect-with-a-proc-macro/69812881#69812881
#[proc_macro_derive(IterateVariants)]
pub fn derive_all_variants(input: TokenStream) -> TokenStream {
let ast = syn::parse_macro_input!(input as syn::DeriveInput);

let syn::Data::Enum(enum_item) = ast.data else {
return quote!(compile_error!("AllVariants only works on enums")).into();
};

let enum_name = ast.ident;
let variant_idents = enum_item.variants.into_iter().map(|v| v.ident);

quote! {
impl #enum_name {
pub fn variants() -> &'static[Self] {
&[ #(#enum_name::#variant_idents),* ]
}
pub fn iter_variants() -> impl Iterator<Item = &'static Self> {
#enum_name::variants().iter()
}
}
}
.into()
}
27 changes: 27 additions & 0 deletions src/enums/emojis.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serenity_discord_bot_derive::IterateVariants;

/// Discord emojis are sent like this:
///
/// `<EmojiName:EmojiId>`
Expand Down Expand Up @@ -45,13 +47,38 @@ macro_rules! display_emoji_impl {
}
}
}
impl $crate::enums::emojis::GetId for $enum_name {
fn get_id(&self) -> &'static str {
match self {
$(Self::$variant => $id,)*
}
}
}
impl $crate::enums::emojis::GetVariantStr for $enum_name {
fn get_variant_str(&self) -> &'static str {
match self {
$(Self::$variant => stringify!($variant),)*
}
}
}
};
}

#[allow(dead_code)]
pub trait GetId {
fn get_id(&self) -> &'static str;
}

#[allow(dead_code)]
pub trait GetVariantStr {
fn get_variant_str(&self) -> &'static str;
}

/// NOTE: This allows non-PascalCase because the emoji itself could have a non-PascalCase name. I'd
/// still try to have them all be PascalCase though.
#[allow(non_camel_case_types)]
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, IterateVariants)]
pub enum Emojis {
HuTaoHeh,
}
Expand Down
24 changes: 24 additions & 0 deletions src/tests/emojis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use reqwest::StatusCode;

use crate::data::command_data::Error;
use crate::enums::emojis::{Emojis, GetId};

#[tokio::test]
async fn test_valid_emoji_urls() -> Result<(), Error> {
use reqwest::Client;

let client = Client::new();

let make_discord_emoji_url =
|emoji_id: &str| format!("https://cdn.discordapp.com/emojis/{}", emoji_id);

for variant in Emojis::iter_variants() {
let response = client
.head(make_discord_emoji_url(variant.get_id()))
.send()
.await?;
assert_eq!(response.status(), StatusCode::OK);
}

Ok(())
}
2 changes: 2 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg(test)]

#[cfg(feature = "network_test")]
mod emojis;
#[cfg(feature = "network_test")]
mod urls;

Expand Down

0 comments on commit a47d8fd

Please sign in to comment.