Nested repetition inside macro

Hi there!

I'm currently building a macro which contains the following snippet:

        $(::paste::paste! {
            #[derive(::async_graphql::SimpleObject, ::serde::Serialize, Debug, Clone)]
            pub struct [<$name $pat_name GraphQL>] {
                $($field_name: $field_type),+

Where $outer is a meta-variable. The problem is that this doesn't work as I get the error:

meta-variable `outer` repeats 1 time, but `pat_name` repeats 5 times

Indeed, in one call site I have five $pat_name expansions.

The question is : how can I write this macro to get the nested expansion of $outer?

I found out some infos about TT munchers and other things but I can't wrap my head around the actual implementation.

Here is the link to the playground with the full macro in case it's needed: Rust Playground

Thanks in advance for your help!

Could you also include in the playground snippet how you are trying to call it? The code you posted so far doesn't trigger any compiler errors.

Yes sorry, you can trigger it with:

graphql_enum! {
    #[derive(PartialEq, Eq)]
    pub enum A {
        Variant1 { value: u64 }

Link to the playground: Rust Playground

Thanks, that's useful. Unfortunately, I still can't understand your goal.

The immediate cause of the error is that your $outer is wrapped in two levels of repeats: first the outer one before paste::paste, and then the inner one immediately surrounding it. So this basically tries to expand the repetition twice, which simply doesn't make sense, hence the error.

However, removing the outer $()+ would lead to the meta-variables $pat_name etc. not being expanded into a repetition, which is also wrong. This leads me to believe that there's something I am missing.

What are you trying to achieve with this code? What do you want the final expanded code to look like? Are you trying to expand the repetition itself several times, sort of "multiplying" it? Because that's not going to work if the matching specifiers weren't nested in the first place.

Concretely what I want to is to apply all the derive attributes I provided on every struct generated by this macro.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.