typestate_builder_macro/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// Copyright (c) 2024 Andy Allison
//
// Licensed under either of
//
// * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
// * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
//
// at your option.
//
// Unless you explicitly state otherwise, any contribution intentionally submitted
// for inclusion in the work by you, as defined in the Apache-2.0 license, shall
// be dual licensed as above, without any additional terms or conditions.
//! This crate provides the `TypestateBuilder` derive macro for generating a
//! typestate-pattern builder for structs.
//!
//! This is the helper crate of [typestate-builder](https://docs.rs/typestate-builder/latest/typestate_builder/).
#![warn(missing_docs)]
mod analyze;
mod analyze2;
mod graph;
mod helper;
mod parse;
mod produce;
use graph::{StructElement, StructRelation};
use helper::write_graph_to_file;
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
/// The `TypestateBuilder` derive macro generates builder pattern code based on the
/// typestate pattern. It provides compile-time guarantees that all necessary fields
/// are initialized before building the final struct.
///
/// For more information, read [the document of the consumer crate](https://docs.rs/typestate-builder/latest/typestate_builder/).
///
/// # Panics
/// This macro will panic if applied to a non-struct type (such as an enum or union).
#[proc_macro_derive(TypestateBuilder)]
#[proc_macro_error]
pub fn typestate_builder_derive(input: TokenStream) -> TokenStream {
// Parse the input token stream into a `DeriveInput` structure.
let input = parse_macro_input!(input as DeriveInput);
let (mut graph, mut map) = parse::run(input);
analyze::run(&mut graph, &map);
analyze2::run(&mut graph, &mut map);
let res = produce::run(&graph, &map);
// Combine the generated code into a final token stream.
let output = quote! {
#(#res)*
};
#[cfg(debug_assertions)]
{
write_graph_to_file(&graph, "example.dot").unwrap();
}
output.into()
}