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

feat(macro): add group macro #267

Open
wants to merge 10 commits into
base: current
Choose a base branch
from

Conversation

TitaniumBrain
Copy link

Adds the poise::group macro, which lets you group commands in a struct.

Use MyStruct::commands() to get a vec of the commands.

See the doc of poise::group for more details.

Needs more documentation and examples.

Adds the poise::group macro, which lets you group commands in a struct.

Use `MyStruct::commands()` to get a vec of the commands.
Copy link
Member

@GnomedDev GnomedDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay, just a few nitpicks that would improve readability before I dive too deep into it.

macros/src/lib.rs Outdated Show resolved Hide resolved
macros/src/lib.rs Show resolved Hide resolved
macros/src/lib.rs Outdated Show resolved Hide resolved
macros/src/lib.rs Outdated Show resolved Hide resolved
Removes a redundant use std::vec;
Moves the body of group into a separate function and removes match in favour of ?
More direct comparison, without iter, map and format!.
Used a match over iterator items.
Will now correctly handle paths with more than 2 segments.
// let item_impl = syn::parse_macro_input!(input_item as syn::ItemImpl);
let item_impl = match syn::parse::<syn::ItemImpl>(input_item) {
Ok(syntax_tree) => syntax_tree,
Err(err) => return Err(err.into()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling this is exactly what ? does, it already calls into, but if this stops an error sure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticing I left that comment by accident.
What I have is almost what the macro would expand into, but iirc it wouldn't return the right error type (return type may need work, a Box dyn or whatever)

macros/src/lib.rs Outdated Show resolved Hide resolved

Ok(quote! {
impl #name {
fn commands() -> Vec<Command<Data, Error>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these Data and Error idents coming from? Is this just assuming something is in scope with Data and Error? There is most likely a better way to handle this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what I did to please the compiler, since iirc from the examples, the user should define their own Data and Error.

Maybe these can be trait bounds?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combining this with the CommandGroup suggestion, CommandGroup could be generic over Data and Error, then this can be

pub trait CommandGroup {
    type Data;
    type Error;
    fn commands() -> Vec<Command<Self::Data, Self::Error>>;
}

impl<D, E> CommandGroup for #name {
    type Data = D;
    type Error = E;
    fn commands() -> Vec<Command<D, E>>;
}

macros/src/lib.rs Show resolved Hide resolved
macros/src/lib.rs Outdated Show resolved Hide resolved
macros/src/lib.rs Outdated Show resolved Hide resolved
slash_command: bool,
context_menu_command: Option<String>,

// When changing these, document it in parent file!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these actually documented anywhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied that from CommandArgs, I'm not sure what it really refers to.

Edit: nvm, it's probably the doc comment on command.

I think it makes sense to leave that comment there, but I want to know how you prefer to document group:

  • list all allowed attributes, which is copy pasting from command with a few removals;
    or
  • list the differences between this and command, then add a "Refer to poise::command for a full list of attributes";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second option would be preferred

macros/src/lib.rs Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants