How can I generate "quote :: Tokens" from both a constant value and a set of values?

I am creating custom output that works with enumeration. I would like to generate code like

match *enum_instance {
    EnumName::VariantName1 => "dummy",
    EnumName::VariantName2 => "dummy",
    EnumName::VariantName3 => "dummy",
}

I managed to get this to work using the following code:

let enum_name = &ast.ident;
let mut q = quote! {};

q.append_all(e.iter().map(|variant| {
    let variant_name = &variant.ident;
    quote! { #enum_name::#variant_name => "dummy", }
}));

quote! {
    impl FullName for #name {
        fn full_name(&self) -> &'static str {
            match *self {
                #q
            }
        }
    }
}

Requiring a temporary variable and executing append_allfeels very inelegant. Is there a cleaner solution?

Code for creating MCVE

Src / main.rs

#[macro_use]
extern crate my_derive;

#[derive(FullName)]
enum Example {
    One,
    Two,
    Three,
}

trait FullName {
    fn full_name(&self) -> &'static str;
}

fn main() {
    assert_eq!(Example::One.full_name(), "dummy");
}

Cargo.toml

[package]
name = "example"
version = "0.0.0"

[dependencies]
my-derive = { path = "my-derive" }

mine-print / Cargo.toml

[package]
name = "my-derive"
version = "0.0.0"

[lib]
proc-macro = true

[dependencies]
quote = "0.3.12"
syn = "0.11.10"

mine-print / src / lib.rs

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;

#[proc_macro_derive(FullName)]
pub fn has_extent_derive(input: TokenStream) -> TokenStream {
    let s = input.to_string();
    let ast = syn::parse_macro_input(&s).expect("Unable to parse input");
    let gen = impl_full_name(&ast);
    gen.parse().expect("Unable to generate")
}

fn impl_full_name(ast: &syn::MacroInput) -> quote::Tokens {
    use syn::Body;

    let name = &ast.ident;

    match ast.body {
        Body::Enum(ref e) => {
            let enum_name = &ast.ident;
            let mut q = quote! {};

            q.append_all(e.iter().map(|variant| {
                let variant_name = &variant.ident;
                quote! { #enum_name::#variant_name => "dummy", }
            }));

            quote! {
                impl FullName for #name {
                    fn full_name(&self) -> &'static str {
                        match *self {
                            #q
                        }
                    }
                }
            }
        }
        _ => {
            panic!("Only implemented for enums");
        }
    }
}
+4
source share
1 answer

xs = [x1, x2, …, xN], #( #xs & stuff );*, x1 & stuff; x2 & stuff; …; xN & stuff quote!.

, , . #(#ks => #vs,)* k1 => v1, k2 => v2, …, kN => vN,.

quote Rust, $ #. :

Body::Enum(ref e) => {
    let enum_name = &ast.ident;
    let variant_names = e.iter().map(|variant| &variant.ident);

    quote! {
        impl FullName for #name {
            fn full_name(&self) -> &'static str {
                match *self {
                    #(#enum_name::#variant_names => "dummy",)*
//                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                }
            }
        }
    }
}

quote , #(…)* : , E0599. quote. , , , enum_name, std::iter::repeat():

Body::Enum(ref e) => {
    use std::iter::repeat;

    let enum_names = repeat(&ast.ident);
    //               ^~~~~~~~~~~~~~~~~~ 
    //                creates the iterator [Example, Example, Example, Example, …]
    let variant_names = e.iter().map(|variant| &variant.ident);

    quote! {
        impl FullName for #name {
            fn full_name(&self) -> &'static str {
                match *self {
                    #(#enum_names::#variant_names => "dummy",)*
                }
            }
        }
    }
}

impl FullName for Example {
    fn full_name(&self) -> &'static str {
        match *self {
            Example::One => "dummy",
            Example::Two => "dummy",
            Example::Three => "dummy",
        }
    }
}

.

+3

Source: https://habr.com/ru/post/1684904/


All Articles