udoprog / genco

A whitespace-aware quasiquoter for beautiful code generation.
Apache License 2.0
181 stars 11 forks source link

Doc Comments #25

Closed wyatt-herkamp closed 2 years ago

wyatt-herkamp commented 2 years ago

So this is my code

    pub fn generate_variant_def(&self) -> Tokens<Rust> {
        let name = &self.name.to_case(Case::UpperCamel);
        let requirement = &self.requirement;
        let switch_variant_type = self.switch_variant_type.generate_variant_type();
        quote! {
            ///This switch variant requires a value #requirement in the compare_to field
            #<line>
            #name #switch_variant_type,
        }
    }

It generates

# [ doc = "This switch variant requires a value #requirement in the compare_to field" ]

 MyVariant {

 test: i32 ,

 } ,

Requirement is equal to "12"

udoprog commented 2 years ago

I'm not following. Could you try and elaborate on what the issue is?

wyatt-herkamp commented 2 years ago

I'm not following. Could you try and elaborate on what the issue is?

Variables don't seem to be replacing themselves inside quote when on a comment

udoprog commented 2 years ago

Gotcha, so interpolation does indeed not work inside of rust-style comments because they are passed into the macros as literal strings (as you can see in how they are expanded).

The way you have to work around this is by formatting the comment yourself:

quote! {
    #(format!("/// This switch variant requires a value {requirement} in the compare_to field"))
    #<line>
    #name #switch_variant_type,
}

I've also opened #26 which will allow you to use format_args! directly once merged, all though it would fundamentally accomplish the same thing.

wyatt-herkamp commented 2 years ago
    #(format!("/// This switch variant requires a value {requirement} in the compare_to field"))

Cool it works!. Thank you