| 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 darlingrust-analyzer cargo derive_builder_core 0.20.2 darling/; |
| 23 | |
| 24 | extern crate proc_macrorust-analyzer cargo derive_builder_core 0.20.2 proc_macro/; |
| 25 | extern crate proc_macro2rust-analyzer cargo derive_builder_core 0.20.2 proc_macro2/; |
| 26 | #[macro_use] |
| 27 | extern crate synrust-analyzer cargo derive_builder_core 0.20.2 syn/; |
| 28 | #[macro_use] |
| 29 | extern crate quoterust-analyzer cargo derive_builder_core 0.20.2 quote/; |
| 30 | mod blockrust-analyzer cargo derive_builder_core 0.20.2 block/; |
| 31 | mod build_methodrust-analyzer cargo derive_builder_core 0.20.2 build_method/; |
| 32 | mod builderrust-analyzer cargo derive_builder_core 0.20.2 builder/; |
| 33 | mod builder_fieldrust-analyzer cargo derive_builder_core 0.20.2 builder_field/; |
| 34 | mod change_spanrust-analyzer cargo derive_builder_core 0.20.2 change_span/; |
| 35 | mod default_expressionrust-analyzer cargo derive_builder_core 0.20.2 default_expression/; |
| 36 | mod doc_commentrust-analyzer cargo derive_builder_core 0.20.2 doc_comment/; |
| 37 | mod initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/; |
| 38 | mod macro_optionsrust-analyzer cargo derive_builder_core 0.20.2 macro_options/; |
| 39 | mod optionsrust-analyzer cargo derive_builder_core 0.20.2 options/; |
| 40 | mod setterrust-analyzer cargo derive_builder_core 0.20.2 setter/; |
| 41 | |
| 42 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use blockrust-analyzer cargo derive_builder_core 0.20.2 block/::BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#; |
| 43 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use build_methodrust-analyzer cargo derive_builder_core 0.20.2 build_method/::BuildMethodrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#; |
| 44 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use builderrust-analyzer cargo derive_builder_core 0.20.2 builder/::Builderrust-analyzer cargo derive_builder_core 0.20.2 builder/Builder#; |
| 45 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use builder_fieldrust-analyzer cargo derive_builder_core 0.20.2 builder_field/::{BuilderFieldrust-analyzer cargo derive_builder_core 0.20.2 builder_field/BuilderField#, BuilderFieldTyperust-analyzer cargo derive_builder_core 0.20.2 builder_field/BuilderFieldType#}; |
| 46 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use change_spanrust-analyzer cargo derive_builder_core 0.20.2 change_span/::change_spanrust-analyzer cargo derive_builder_core 0.20.2 change_span/change_span().; |
| 47 | use darlingrust-analyzer cargo darling 0.23.0 crate/::FromDeriveInputrust-analyzer cargo darling_core 0.23.0 from_derive_input/FromDeriveInput#; |
| 48 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use default_expressionrust-analyzer cargo derive_builder_core 0.20.2 default_expression/::DefaultExpressionrust-analyzer cargo derive_builder_core 0.20.2 default_expression/DefaultExpression#; |
| 49 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use doc_commentrust-analyzer cargo derive_builder_core 0.20.2 doc_comment/::doc_comment_fromrust-analyzer cargo derive_builder_core 0.20.2 doc_comment/doc_comment_from().; |
| 50 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/::{FieldConversionrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#, Initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#}; |
| 51 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use optionsrust-analyzer cargo derive_builder_core 0.20.2 options/::{BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#, Eachrust-analyzer cargo derive_builder_core 0.20.2 options/Each#}; |
| 52 | pub(craterust-analyzer cargo derive_builder_core 0.20.2 crate/) use setterrust-analyzer cargo derive_builder_core 0.20.2 setter/::Setterrust-analyzer cargo derive_builder_core 0.20.2 setter/Setter#; |
| 53 | |
| 54 | const DEFAULT_STRUCT_NAMErust-analyzer cargo derive_builder_core 0.20.2 DEFAULT_STRUCT_NAME.: &str = "__default"; |
| 55 | |
| 56 | /// Derive a builder for a struct |
| 57 | pub fn builder_for_structrust-analyzer cargo derive_builder_core 0.20.2 builder_for_struct().(ast: synrust-analyzer cargo syn 2.0.119 crate/::DeriveInputrust-analyzer cargo syn 2.0.119 derive/DeriveInput#) -> proc_macro2rust-analyzer cargo proc-macro2 1.0.107 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream# { |
| 58 | let opts = match macro_optionsrust-analyzer cargo derive_builder_core 0.20.2 macro_options/::Optionsrust-analyzer cargo derive_builder_core 0.20.2 macro_options/darling_opts/Options#::from_derive_inputrust-analyzer cargo derive_builder_core 0.20.2 macro_options/darling_opts/impl#[Options][FromDeriveInput]from_derive_input().(&ast) { |
| 59 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(val) => val, |
| 60 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => { |
| 61 | return err.write_errorsrust-analyzer cargo darling_core 0.23.0 error/impl#[Error]write_errors().(); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | let mut builder = opts.as_builderrust-analyzer cargo derive_builder_core 0.20.2 macro_options/darling_opts/impl#[Options]as_builder().(); |
| 66 | let mut build_fn = opts.as_build_methodrust-analyzer cargo derive_builder_core 0.20.2 macro_options/darling_opts/impl#[Options]as_build_method().(); |
| 67 | |
| 68 | builder.doc_commentrust-analyzer cargo derive_builder_core 0.20.2 builder/impl#[`Builder<'a>`]doc_comment().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 69 | include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("doc_tpl/builder_struct.md"), |
| 70 | struct_name = ast.identrust-analyzer cargo syn 2.0.119 derive/DeriveInput#ident. |
| 71 | )); |
| 72 | build_fn.doc_commentrust-analyzer cargo derive_builder_core 0.20.2 build_method/impl#[`BuildMethod<'a>`]doc_comment().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 73 | include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("doc_tpl/builder_method.md"), |
| 74 | struct_name = ast.identrust-analyzer cargo syn 2.0.119 derive/DeriveInput#ident. |
| 75 | )); |
| 76 | |
| 77 | for field in opts.fieldsrust-analyzer cargo derive_builder_core 0.20.2 macro_options/darling_opts/impl#[Options]fields().() { |
| 78 | builder.push_fieldrust-analyzer cargo derive_builder_core 0.20.2 builder/impl#[`Builder<'a>`]push_field().(field.as_builder_fieldrust-analyzer cargo derive_builder_core 0.20.2 macro_options/darling_opts/impl#[`FieldWithDefaults<'a>`]as_builder_field().()); |
| 79 | builder.push_setter_fnrust-analyzer cargo derive_builder_core 0.20.2 builder/impl#[`Builder<'a>`]push_setter_fn().(field.as_setterrust-analyzer cargo derive_builder_core 0.20.2 macro_options/darling_opts/impl#[`FieldWithDefaults<'a>`]as_setter().()); |
| 80 | build_fn.push_initializerrust-analyzer cargo derive_builder_core 0.20.2 build_method/impl#[`BuildMethod<'a>`]push_initializer().(field.as_initializerrust-analyzer cargo derive_builder_core 0.20.2 macro_options/darling_opts/impl#[`FieldWithDefaults<'a>`]as_initializer().()); |
| 81 | } |
| 82 | |
| 83 | builder.push_build_fnrust-analyzer cargo derive_builder_core 0.20.2 builder/impl#[`Builder<'a>`]push_build_fn().(build_fn); |
| 84 | |
| 85 | quoterust-analyzer cargo quote 1.0.47 quote!!(#builder) |
| 86 | } |