| 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 ] |
| 22 | extern crate darling; |
| 23 | |
| 24 | extern crate proc_macro; |
| 25 | extern crate proc_macro2; |
| 26 | #[macro_use ] |
| 27 | extern crate syn; |
| 28 | #[macro_use ] |
| 29 | extern crate quote; |
| 30 | mod block; |
| 31 | mod build_method; |
| 32 | mod builder; |
| 33 | mod builder_field; |
| 34 | mod change_span; |
| 35 | mod default_expression; |
| 36 | mod doc_comment; |
| 37 | mod initializer; |
| 38 | mod macro_options; |
| 39 | mod options; |
| 40 | mod setter; |
| 41 | |
| 42 | pub(crate) use block::BlockContents; |
| 43 | pub(crate) use build_method::BuildMethod; |
| 44 | pub(crate) use builder::Builder; |
| 45 | pub(crate) use builder_field::{BuilderField, BuilderFieldType}; |
| 46 | pub(crate) use change_span::change_span; |
| 47 | use darling::FromDeriveInput; |
| 48 | pub(crate) use default_expression::DefaultExpression; |
| 49 | pub(crate) use doc_comment::doc_comment_from; |
| 50 | pub(crate) use initializer::{FieldConversion, Initializer}; |
| 51 | pub(crate) use options::{BuilderPattern, Each}; |
| 52 | pub(crate) use setter::Setter; |
| 53 | |
| 54 | const DEFAULT_STRUCT_NAME: &str = "__default" ; |
| 55 | |
| 56 | /// Derive a builder for a struct |
| 57 | pub 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 | } |
| 87 | |