typesafe_builders_derive/
lib.rs

1/*
2 * SPDX-FileCopyrightText: Oliver Tale-Yazdi <oliver@tasty.limo>
3 * SPDX-License-Identifier: GPL-3.0-only
4 */
5
6#![doc = include_str!("../README.md")]
7#![deny(unsafe_code)]
8
9extern crate proc_macro;
10use proc_macro::TokenStream;
11
12/// Derive a builder for your struct via `#[derive(Builder)]`.
13///
14/// ## Field Attributes
15///
16/// All attributes must be wrapped `builder`, eg. `builder(optional)`.
17///
18/// - `optional` - A field can be set, but is not required to.
19/// - `constructor` - A field must already be set in the `builder` function.
20/// - `decay` - TODO explain
21#[proc_macro_derive(Builder, attributes(builder))]
22pub fn derive_builder(stream: TokenStream) -> TokenStream {
23	let ast = syn::parse_macro_input!(stream as syn::DeriveInput);
24	let ts2 = typesafe_builders_core::impl_derive_builder(&ast);
25
26	// The magical part: convert the proc macro error to a compiler error:
27	ts2.unwrap_or_else(syn::Error::into_compile_error).into()
28}