derive_builder_core/
lib.rs

1//! Internal helper library for the `derive_builder` crate.
2//!
3//! **Important Note**:
4//!
5//! * You are probably looking for the [`derive_builder`] crate,
6//!   which wraps this crate and is much more ergonomic to use.
7//!
8//! ## Purpose
9//!
10//! This is an internal helper library of [`derive_builder`], which allows for
11//! all the logic of builder creation to be decoupled from the proc-macro entry
12//! point.
13//!
14//!
15//! [`derive_builder`]: https://!crates.io/crates/derive_builder
16//! [`derive_builder_core`]: https://!crates.io/crates/derive_builder_core
17
18#![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
56/// Derive a builder for a struct
57pub 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}