derive_builder_core/
lib.rs
1#![deny(warnings, missing_docs)]
19#![cfg_attr(test, recursion_limit = "100")]
20
21#[macro_use]
22extern crate darling;
23
24extern crate proc_macro;
25extern crate proc_macro2;
26#[macro_use]
27extern crate syn;
28#[macro_use]
29extern crate quote;
30mod block;
31mod build_method;
32mod builder;
33mod builder_field;
34mod change_span;
35mod default_expression;
36mod doc_comment;
37mod initializer;
38mod macro_options;
39mod options;
40mod setter;
41
42pub(crate) use block::BlockContents;
43pub(crate) use build_method::BuildMethod;
44pub(crate) use builder::Builder;
45pub(crate) use builder_field::{BuilderField, BuilderFieldType};
46pub(crate) use change_span::change_span;
47use darling::FromDeriveInput;
48pub(crate) use default_expression::DefaultExpression;
49pub(crate) use doc_comment::doc_comment_from;
50pub(crate) use initializer::{FieldConversion, Initializer};
51pub(crate) use options::{BuilderPattern, Each};
52pub(crate) use setter::Setter;
53
54const DEFAULT_STRUCT_NAME: &str = "__default";
55
56pub fn builder_for_struct(ast: syn::DeriveInput) -> proc_macro2::TokenStream {
58 let opts = match macro_options::Options::from_derive_input(&ast) {
59 Ok(val) => val,
60 Err(err) => {
61 return err.write_errors();
62 }
63 };
64
65 let mut builder = opts.as_builder();
66 let mut build_fn = opts.as_build_method();
67
68 builder.doc_comment(format!(
69 include_str!("doc_tpl/builder_struct.md"),
70 struct_name = ast.ident
71 ));
72 build_fn.doc_comment(format!(
73 include_str!("doc_tpl/builder_method.md"),
74 struct_name = ast.ident
75 ));
76
77 for field in opts.fields() {
78 builder.push_field(field.as_builder_field());
79 builder.push_setter_fn(field.as_setter());
80 build_fn.push_initializer(field.as_initializer());
81 }
82
83 builder.push_build_fn(build_fn);
84
85 quote!(#builder)
86}