How to loop within quote::quote

Problem Description and Proof of Concept

In my attempts of learning new concepts in programming, I usually like to re-implement something which has already been done. In this case I’m trying to learn Rust procedual macros by implementing a csv-like parser. However I ran into some problems.

Assuming a struct

struct MyStruct {
    _first_field: String,
    _second_field: String,
    _third_field: String,
}

I would like to use a derive macro

#[derive(PopulateFields)]
struct MyStruct {
    ...
}

whose goal is to add an impl like

impl PopulateFields for MyStruct {
    fn populate_fields(vec: Vec<String>) -> Self {
        Self {
            _first_field: vec[0],
            _second_field: vec[1],
            _third_field: vec[2]
        }
    }
}

i.e. taking a vector of values and returning a Self with the values mapped to the fields. I know that this is approximately what e.g. serde-crate can do, and I know that there is a specific csv-crate (using serde). But as part of the learning process, I thought this was a good example to re-implement this feature…

The code below is a proof of concept macro which works, but is not generic at all.

// Proof of concept, with hardcoded indices.

use proc_macro::{TokenStream, Ident, Span};
use quote::quote;
use syn::{parse_macro_input, ItemStruct};

#[proc_macro_derive(PopulateFields)]
pub fn derive_populate_fields(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);

    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    
    let output = quote! {
        impl #impl_generics PopulateFields for #name #ty_generics #where_clause {
            fn populate_fields(vec: Vec<String>) -> Self {
                Self {
                    _first_field: vec[0],
                    _second_field: vec[1],
                    _third_field: vec[2]
                }
            }
        }
    };

    TokenStream::from(output)
}

My problem which the below ideas tried to address is to find a way of making the content of the quote! more general to e.g. loop over the fields rather than hard-coding the indices for every new struct/csv format.

General Disclaimer

The snippets below are perhaps naively written and without any checks what so ever (e.g. bound checks on accessing the vec elements). However I prefered to keep the code as simple as I could but still showing the thought process. A more realistic implementation would likely return a Result(Self) object rather than Self on its own.

I have also looked at the general proc_macro documentation, and several SO answers but cannot a way of solving my problem.

Idea 1

Inspired by this anwser I managed to get a count of the number of fields of the current struct.

My idea is to just get this field number and loop over the entries of the vec if the length of vec is the same as the number of fields. However I cannot find a way of looping within the quote! part of the proc_macro.

// Idea 1
use proc_macro::{TokenStream, Ident, Span};
use quote::quote;
use syn::{parse_macro_input, ItemStruct};

#[proc_macro_derive(PopulateFields)]
pub fn derive_populate_fields(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);

    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    let field_count = input.fields.iter().count();  // as by the linked answer
    
    let output = quote! {
        impl #impl_generics PopulateFields for #name #ty_generics #where_clause {
            fn populate_fields(vec: Vec<String>) -> Self {
                Self {
                    // pseudo-code since I could not get any attempt to work...
                    for i, field in enumerate(#input.fields) {  // coming from a python background...
                        #field: vec[#i],
                    }
                }
            }
        }
    };

    TokenStream::from(output)
}

Idea 2

Another idea was to build the _i_field: vec[i]-part of the code outside of the quote!-context and store them in a new vec, as inspired by this answer. Then inside the quote! the full Self {"_i_field: vec[i]"-part} would be built by joining the new vec.

// Idea 2
use proc_macro::{TokenStream, Ident, Span};
use quote::quote;
use syn::{parse_macro_input, ItemStruct};

#[proc_macro_derive(PopulateFields)]
pub fn derive_populate_fields(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);

    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    
    let new_fields: Vec<_> = input.fields
        .iter()
        .map(|field| {
            quote! {
                #field: vec[#i] // somehow to get the index as well, I just used a hard-coded index in my testing
            }
        })
        .collect();
    
    let output = quote! {
        impl #impl_generics PopulateFields for #name #ty_generics #where_clause {
            fn populate_fields(vec: Vec<String>) -> Self {
                Self {
                    #(#new_fields)*
                }
            }
        }
    };

    TokenStream::from(output)
}

Idea 3

The final idea I have so far is to simply build a String with the correct return-shape and then somehow turn that into code inside the quote!. This approach works in the sense that the produced string is correctly formatted, but I cannot transfer it “as code” into the quote!-area (it gets pasted as a String).

// Idea 3
#[proc_macro_derive(PopulateFields)]
pub fn derive_populate_fields(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);

    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    
    // Extract the fields, create a vec of the field names, format them nicely and push to vec.
    let fields = input.fields.iter().collect::<Vec<_>>();
    let mut field_assigns: Vec<String> = vec![];
    let mut idx: usize = 0;
    for f in &fields {
        let fname: String = f.ident.clone().unwrap().to_string();
        field_assigns.push(format!("    {fname}: vec[{idx}]"));
        idx += 1;
    }

    // Join the string and put it into the final string.
    let joined: String = field_assigns.join(",n");
    let inside: String = "Self {n".to_string() + format!("{}", joined).as_str() + "n}";
    println!("{}", inside); // this prints a correctly formatted string

    let output = quote! {
        impl #impl_generics PopulateFields for #name #ty_generics #where_clause {
            fn populate_fields(vec: Vec<String>) -> Self {
                // paste code from `inside` here...
            }
        }
    };

    TokenStream::from(output)
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật