Skip to content

Commit

Permalink
feat: builders everwhere
Browse files Browse the repository at this point in the history
  • Loading branch information
vidhanio committed Aug 31, 2024
1 parent 8538065 commit bb14b33
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ members = [".", "macros"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/vidhanio/serenity-commands"
version = "0.4.5"
version = "0.5.0"

[workspace.dependencies]
serenity-commands-macros = { version = "0.4", path = "macros" }
serenity-commands-macros = { version = "0.5", path = "macros" }

serenity = { version = "0.12", default-features = false, features = [
"builder",
Expand Down
6 changes: 6 additions & 0 deletions macros/src/basic_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{Generics, Ident, Lit, LitStr, Type};

use crate::BuilderMethodList;

#[derive(Debug, PartialEq, FromMeta)]
enum OptionType {
String,
Expand Down Expand Up @@ -37,6 +39,8 @@ pub struct Args {
data: Data<Variant, Type>,

option_type: SpannedValue<OptionType>,

builder: Option<BuilderMethodList>,
}

impl Args {
Expand All @@ -51,6 +55,7 @@ impl Args {

let command_option_type = self.option_type.command_option_type();
let method_name = self.option_type.method_name(self.option_type.span());
let builder_methods = &self.builder;

quote! {
fn create_option(
Expand All @@ -64,6 +69,7 @@ impl Args {
)
#(.#method_name(#choices))*
.required(true)
#builder_methods
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion macros/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{Generics, Ident};

use crate::{Field, Variant};
use crate::{BuilderMethodList, Field, Variant};

#[derive(Debug, FromDeriveInput)]
#[darling(
Expand All @@ -25,6 +25,8 @@ pub struct Args {
ident: Ident,
generics: Generics,
data: Data<Variant, Field>,

builder: Option<BuilderMethodList>,
}

impl Args {
Expand Down Expand Up @@ -71,12 +73,15 @@ impl Args {
}
};

let builder_methods = &self.builder;

quote! {
fn create_command(
name: impl ::std::convert::Into<::std::string::String>,
description: impl ::std::convert::Into<::std::string::String>,
) -> ::serenity::all::CreateCommand {
#body
#builder_methods
}
}
}
Expand Down
63 changes: 45 additions & 18 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::iter;
use darling::{
ast::{Fields, NestedMeta, Style},
error::Accumulator,
util::{Flag, SpannedValue},
util::SpannedValue,
Error, FromDeriveInput, FromField, FromMeta, FromVariant,
};
use heck::ToKebabCase;
Expand All @@ -38,6 +38,7 @@ struct Variant {
attrs: Vec<Attribute>,

name: Option<SpannedValue<String>>,
builder: Option<BuilderMethodList>,
}

impl Variant {
Expand All @@ -49,7 +50,7 @@ impl Variant {
let name = self.name();
let description = documentation_string(&self.attrs, &self.ident, acc);

match self.fields.style {
let body = match self.fields.style {
Style::Struct => {
let fields = self.fields.iter().map(|field| field.create_option(acc));

Expand Down Expand Up @@ -77,14 +78,21 @@ impl Variant {
.description(#description)
}
}
};

let builder_methods = &self.builder;

quote! {
#body
#builder_methods
}
}

fn create_sub_command_or_group(&self, acc: &mut Accumulator) -> TokenStream {
let name = self.name();
let description = documentation_string(&self.attrs, &self.ident, acc);

match self.fields.style {
let body = match self.fields.style {
Style::Struct => {
let fields = self.fields.iter().map(|field| field.create_option(acc));

Expand Down Expand Up @@ -121,14 +129,21 @@ impl Variant {
)
}
}
};

let builder_methods = &self.builder;

quote! {
#body
#builder_methods
}
}

fn create_sub_command(&self, acc: &mut Accumulator) -> TokenStream {
let name = self.name();
let description = documentation_string(&self.attrs, &self.ident, acc);

match self.fields.style {
let body = match self.fields.style {
Style::Struct => {
let fields = self.fields.iter().map(|field| field.create_option(acc));

Expand Down Expand Up @@ -165,6 +180,13 @@ impl Variant {
)
}
}
};

let builder_methods = &self.builder;

quote! {
#body
#builder_methods
}
}

Expand Down Expand Up @@ -383,6 +405,23 @@ impl FromMeta for BuilderMethodList {
}
}

impl ToTokens for BuilderMethodList {
fn to_tokens(&self, tokens: &mut TokenStream) {
let methods = self.methods.iter().map(|method| {
let method_name = &method.method;
let args = &method.args;

quote_spanned! {method_name.span()=>
.#method_name(#args)
}
});

tokens.extend(quote! {
#(#methods)*
});
}
}

#[derive(Debug, FromField)]
#[darling(attributes(command), forward_attrs(doc))]
struct Field {
Expand All @@ -391,7 +430,6 @@ struct Field {
attrs: Vec<Attribute>,

name: Option<SpannedValue<String>>,
autocomplete: Flag,

builder: Option<BuilderMethodList>,
}
Expand All @@ -413,25 +451,14 @@ impl Field {

let name = self.name();
let description = documentation_string(&self.attrs, ident, acc);
let autocomplete = self.autocomplete.is_present();
let builder = self.builder.as_ref().into_iter().flat_map(|list| {
list.methods.iter().map(|method| {
let method_name = &method.method;
let args = &method.args;

quote_spanned! {method_name.span()=>
.#method_name(#args)
}
})
});
let builder_methods = &self.builder;

quote! {
<#ty as ::serenity_commands::BasicOption>::create_option(
#name,
#description,
)
.set_autocomplete(#autocomplete)
#(#builder)*
#builder_methods
}
}

Expand Down
7 changes: 6 additions & 1 deletion macros/src/sub_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{Generics, Ident};

use crate::Field;
use crate::{BuilderMethodList, Field};

#[derive(Debug, FromDeriveInput)]
#[darling(
Expand All @@ -19,6 +19,8 @@ pub struct Args {
ident: Ident,
generics: Generics,
data: Data<Ignored, Field>,

builder: Option<BuilderMethodList>,
}

impl Args {
Expand Down Expand Up @@ -57,12 +59,15 @@ impl Args {
}
};

let builder_methods = &self.builder;

quote! {
fn create_option(
name: impl ::std::convert::Into<::std::string::String>,
description: impl ::std::convert::Into<::std::string::String>,
) -> ::serenity::all::CreateCommandOption {
#body
#builder_methods
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion macros/src/sub_command_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{Generics, Ident};

use crate::Variant;
use crate::{BuilderMethodList, Variant};

#[derive(Debug, FromDeriveInput)]
#[darling(attributes(command), supports(enum_named, enum_newtype, enum_unit))]
pub struct Args {
ident: Ident,
generics: Generics,
data: Data<Variant, Ignored>,

builder: Option<BuilderMethodList>,
}

impl Args {
Expand All @@ -21,6 +23,8 @@ impl Args {
.iter()
.map(|variant| variant.create_sub_command(acc));

let builder_methods = &self.builder;

quote! {
fn create_option(
name: impl ::std::convert::Into<::std::string::String>,
Expand All @@ -32,6 +36,7 @@ impl Args {
description,
)
#(.add_sub_option(#body))*
#builder_methods
}
}
}
Expand Down

0 comments on commit bb14b33

Please sign in to comment.