#syn #meta-programming #quote

syn_builder

Builder functions for syn structures and enums to ease the generation of Rust code

2 unstable releases

0.2.0 Sep 21, 2023
0.1.0 Jul 16, 2023

#947 in Procedural macros

Download history 8/week @ 2024-02-23 4/week @ 2024-03-01 31/week @ 2024-03-29 29/week @ 2024-04-05

60 downloads per month

MIT/Apache

135KB
4.5K SLoC

Syn builder

Builder functions for syn structures and enums to ease the generation of Rust code.

Note: only syn structures are creating using the builder methods - there no intermediate structs to manage.

Usage

  1. Add to your Cargo.toml file
[dependencies]
syn_builder = "0.2.0"
  1. Import builder functions and create syn objects
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn_builder::*;

fn main() {
    //generate code using the builder api
    let code = item_enum("my_enum").variants([
        variant("A").fields(
            fields_unamed([field(type_path("A")), field(type_path("B"))]),
        ),
        variant("B"),
        variant("C").fields(
            fields_named([
                field(type_path("A")).ident("other"),
                field(type_path("B")).ident("one"),
            ]),
        ),
    ]);

    let mut token_stream = TokenStream::new();
    code.to_tokens(&mut token_stream);

    println!("{token_stream}");
}

Alternatives

  • quote - generate syn structs by writing Rust code and using variable interpolation

Dependencies

~310–740KB
~18K SLoC