In this lesson, you will learn how to leverage the syn crate to transform opaque TokenStream input into a structured Abstract Syntax Tree (AST) that you can inspect programmatically.
While procedural macros receive raw TokenStream inputs, manipulating tokens directly is error-prone and brittle. syn provides a robust set of data structures that mirror Rust's grammar. By parsing a TokenStream into a syn::DeriveInput or a custom syn::parse::Parse implementation, you gain the ability to traverse Rust code as a tree of nodes rather than a sequence of characters.
Extracting Actionable Data
Consider you are building a derive macro to automatically generate a JsonSerializable implementation. To generate code, you first need to know the fields of the struct:
use syn::{parse_macro_input, Data, DeriveInput};
let input = parse_macro_input!(token_stream as DeriveInput);
// Inspect the AST to extract field names
if let Data::Struct(data_struct) = input.data {
for field in data_struct.fields {
if let Some(ident) = field.ident {
println!("Found field: {}", ident);
}
}
}
By pattern matching against input.data, you navigate the hierarchy of the AST. The syn crate organizes the code into intuitive types: Fields, Generics, Visibility, and Attributes. Because syn implements the Parse trait, you can write declarative logic to map specific Rust syntax constructs to your internal code-generation structures.
Instead of manual token manipulation, you treat the input as a typed object. You can verify if a field is public, check if a struct uses generic parameters, or inspect attribute macros attached to specific struct members—all while benefiting from the compiler's rigorous parsing rules. Once you have traversed this tree and collected the necessary identifiers and metadata, you can safely pass this data into quote! to generate the corresponding boilerplate.
Key Takeaway: Use syn to parse raw TokenStreams into a type-safe AST, allowing you to extract complex code metadata through pattern matching and structural traversal rather than fragile string or token manipulation.