-
Notifications
You must be signed in to change notification settings - Fork 103
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
base: current
Are you sure you want to change the base?
Conversation
Adds the poise::group macro, which lets you group commands in a struct. Use `MyStruct::commands()` to get a vec of the commands.
There was a problem hiding this 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.
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()), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
||
Ok(quote! { | ||
impl #name { | ||
fn commands() -> Vec<Command<Data, Error>> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>>;
}
slash_command: bool, | ||
context_menu_command: Option<String>, | ||
|
||
// When changing these, document it in parent file! |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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";
There was a problem hiding this comment.
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
Instead of quote!(), use proc_macro2::TokenStream::new()
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.