| 1 | use proc_macro2rust-analyzer cargo proc-macro2 1.0.107 crate/::{Spanrust-analyzer cargo proc-macro2 1.0.107 Span#, TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#}; |
| 2 | use quoterust-analyzer cargo quote 1.0.47 crate/::{ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens#, TokenStreamExtrust-analyzer cargo quote 1.0.47 ext/TokenStreamExt#}; |
| 3 | |
| 4 | use craterust-analyzer cargo derive_builder_core 0.20.2 crate/::{change_spanrust-analyzer cargo derive_builder_core 0.20.2 change_span/, BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#, BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#, DefaultExpressionrust-analyzer cargo derive_builder_core 0.20.2 default_expression/DefaultExpression#, DEFAULT_STRUCT_NAMErust-analyzer cargo derive_builder_core 0.20.2 DEFAULT_STRUCT_NAME.}; |
| 5 | |
| 6 | /// Initializer for the target struct fields, implementing `quote::ToTokens`. |
| 7 | /// |
| 8 | /// Lives in the body of `BuildMethod`. |
| 9 | /// |
| 10 | /// # Examples |
| 11 | /// |
| 12 | /// Will expand to something like the following (depending on settings): |
| 13 | /// |
| 14 | /// ```rust,ignore |
| 15 | /// # extern crate proc_macro2; |
| 16 | /// # #[macro_use] |
| 17 | /// # extern crate quote; |
| 18 | /// # extern crate syn; |
| 19 | /// # #[macro_use] |
| 20 | /// # extern crate derive_builder_core; |
| 21 | /// # use derive_builder_core::{DeprecationNotes, Initializer, BuilderPattern}; |
| 22 | /// # fn main() { |
| 23 | /// # let mut initializer = default_initializer!(); |
| 24 | /// # initializer.default_value = Some("42".parse().unwrap()); |
| 25 | /// # initializer.builder_pattern = BuilderPattern::Owned; |
| 26 | /// # |
| 27 | /// # assert_eq!(quote!(#initializer).to_string(), quote!( |
| 28 | /// foo: match self.foo { |
| 29 | /// Some(value) => value, |
| 30 | /// None => { 42 }, |
| 31 | /// }, |
| 32 | /// # ).to_string()); |
| 33 | /// # } |
| 34 | /// ``` |
| 35 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 36 | pub struct Initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#<'a> { |
| 37 | /// Path to the root of the derive_builder crate. |
| 38 | pub crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#crate_root.: &'a synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#, |
| 39 | /// Name of the target field. |
| 40 | pub field_identrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#field_ident.: &'a synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#, |
| 41 | /// Whether the builder implements a setter for this field. |
| 42 | pub field_enabledrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#field_enabled.: bool, |
| 43 | /// How the build method takes and returns `self` (e.g. mutably). |
| 44 | pub builder_patternrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#builder_pattern.: BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#, |
| 45 | /// Default value for the target field. |
| 46 | /// |
| 47 | /// This takes precedence over a default struct identifier. |
| 48 | pub default_valuerust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#default_value.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a DefaultExpressionrust-analyzer cargo derive_builder_core 0.20.2 default_expression/DefaultExpression#>, |
| 49 | /// Whether the build_method defines a default struct. |
| 50 | pub use_default_structrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#use_default_struct.: bool, |
| 51 | /// Span where the macro was told to use a preexisting error type, instead of creating one, |
| 52 | /// to represent failures of the `build` method. |
| 53 | /// |
| 54 | /// An initializer can force early-return if a field has no set value and no default is |
| 55 | /// defined. In these cases, it will convert from `derive_builder::UninitializedFieldError` |
| 56 | /// into the return type of its enclosing `build` method. That conversion is guaranteed to |
| 57 | /// work for generated error types, but if the caller specified an error type to use instead |
| 58 | /// they may have forgotten the conversion from `UninitializedFieldError` into their specified |
| 59 | /// error type. |
| 60 | pub custom_error_type_spanrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#custom_error_type_span.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Spanrust-analyzer cargo proc-macro2 1.0.107 Span#>, |
| 61 | /// Method to use to to convert the builder's field to the target field |
| 62 | /// |
| 63 | /// For sub-builder fields, this will be `build` (or similar) |
| 64 | pub conversionrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#conversion.: FieldConversionrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#<'a>, |
| 65 | } |
| 66 | |
| 67 | impl<'a> ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens# for Initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#<'a> { |
| 68 | fn to_tokensrust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`Initializer<'a>`][ToTokens]to_tokens().(&self, tokens: &mut TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#) { |
| 69 | let struct_field = &self.field_identrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#field_ident.; |
| 70 | let builder_field = struct_field; |
| 71 | |
| 72 | // This structure prevents accidental failure to add the trailing `,` due to incautious `return` |
| 73 | let append_rhs = |tokens: &mut TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#| { |
| 74 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.field_enabledrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#field_enabled. { |
| 75 | let default = self.defaultrust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`Initializer<'a>`]default().(); |
| 76 | tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 77 | #default |
| 78 | )); |
| 79 | } else { |
| 80 | match &self.conversionrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#conversion. { |
| 81 | FieldConversionrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#::Blockrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#Block#(conv) => { |
| 82 | conv.to_tokensrust-analyzer cargo quote 1.0.47 to_tokens/impl#[`&T`][ToTokens]to_tokens().(tokens); |
| 83 | } |
| 84 | FieldConversionrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#::Moverust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#Move# => tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!( self.#builder_field )), |
| 85 | FieldConversionrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#::OptionOrDefaultrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#OptionOrDefault# => { |
| 86 | let match_some = self.match_somerust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`Initializer<'a>`]match_some().(); |
| 87 | let match_none = self.match_nonerust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`Initializer<'a>`]match_none().(); |
| 88 | tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 89 | match self.#builder_field { |
| 90 | #match_some, |
| 91 | #match_none, |
| 92 | } |
| 93 | )); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!(#struct_field:)); |
| 100 | append_rhs(tokens); |
| 101 | tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!(,)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl<'a> Initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#<'a> { |
| 106 | /// To be used inside of `#struct_field: match self.#builder_field { ... }` |
| 107 | fn match_somerust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`Initializer<'a>`]match_some().(&'a self) -> MatchSomerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#<'a> { |
| 108 | match self.builder_patternrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#builder_pattern. { |
| 109 | BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#::Ownedrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#Owned# => MatchSomerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#::Moverust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Move#, |
| 110 | BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#::Mutablerust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#Mutable# | BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#::Immutablerust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#Immutable# => MatchSomerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#::Clonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Clone# { |
| 111 | crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Clone#crate_root.: self.crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#crate_root., |
| 112 | }, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// To be used inside of `#struct_field: match self.#builder_field { ... }` |
| 117 | fn match_nonerust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`Initializer<'a>`]match_none().(&'a self) -> MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#<'a> { |
| 118 | match self.default_valuerust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#default_value. { |
| 119 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(expr) => MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#::DefaultTorust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo# { |
| 120 | exprrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo#expr., |
| 121 | crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo#crate_root.: self.crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#crate_root., |
| 122 | }, |
| 123 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 124 | if self.use_default_structrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#use_default_struct. { |
| 125 | MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#::UseDefaultStructFieldrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#UseDefaultStructField#(self.field_identrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#field_ident.) |
| 126 | } else { |
| 127 | MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#::ReturnErrorrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError# { |
| 128 | crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#crate_root.: self.crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#crate_root., |
| 129 | field_namerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#field_name.: self.field_identrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#field_ident..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 130 | spanrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#span.: self.custom_error_type_spanrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#custom_error_type_span., |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn defaultrust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`Initializer<'a>`]default().(&'a self) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream# { |
| 138 | let crate_root = self.crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#crate_root.; |
| 139 | match self.default_valuerust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#default_value. { |
| 140 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(expr) => expr.with_crate_rootrust-analyzer cargo derive_builder_core 0.20.2 default_expression/impl#[DefaultExpression]with_crate_root().(crate_root).into_token_streamrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens#into_token_stream().(), |
| 141 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# if self.use_default_structrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#use_default_struct. => { |
| 142 | let struct_ident = synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#::newrust-analyzer cargo proc-macro2 1.0.107 impl#[Ident]new().(DEFAULT_STRUCT_NAMErust-analyzer cargo derive_builder_core 0.20.2 DEFAULT_STRUCT_NAME., Spanrust-analyzer cargo proc-macro2 1.0.107 Span#::call_siterust-analyzer cargo proc-macro2 1.0.107 impl#[Span]call_site().()); |
| 143 | let field_ident = self.field_identrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#field_ident.; |
| 144 | quoterust-analyzer cargo quote 1.0.47 quote!!(#struct_ident.#field_ident) |
| 145 | } |
| 146 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 147 | quoterust-analyzer cargo quote 1.0.47 quote!!(#crate_root::export::core::default::Default::default()) |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 154 | pub enum FieldConversionrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#<'a> { |
| 155 | /// Usual conversion: unwrap the Option from the builder, or (hope to) use a default value |
| 156 | OptionOrDefaultrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#OptionOrDefault#, |
| 157 | /// Custom conversion is a block contents expression |
| 158 | Blockrust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#Block#(&'a BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#), |
| 159 | /// Custom conversion is just to move the field from the builder |
| 160 | Moverust-analyzer cargo derive_builder_core 0.20.2 initializer/FieldConversion#Move#, |
| 161 | } |
| 162 | |
| 163 | /// To be used inside of `#struct_field: match self.#builder_field { ... }` |
| 164 | enum MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#<'a> { |
| 165 | /// Inner value must be a valid Rust expression |
| 166 | DefaultTorust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo# { |
| 167 | exprrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo#expr.: &'a DefaultExpressionrust-analyzer cargo derive_builder_core 0.20.2 default_expression/DefaultExpression#, |
| 168 | crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo#crate_root.: &'a synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#, |
| 169 | }, |
| 170 | /// Inner value must be the field identifier |
| 171 | /// |
| 172 | /// The default struct must be in scope in the build_method. |
| 173 | UseDefaultStructFieldrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#UseDefaultStructField#(&'a synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#), |
| 174 | /// Inner value must be the field name |
| 175 | ReturnErrorrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError# { |
| 176 | crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#crate_root.: &'a synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#, |
| 177 | field_namerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#field_name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 178 | spanrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#span.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Spanrust-analyzer cargo proc-macro2 1.0.107 Span#>, |
| 179 | }, |
| 180 | } |
| 181 | |
| 182 | impl<'a> ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens# for MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#<'a> { |
| 183 | fn to_tokensrust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`MatchNone<'a>`][ToTokens]to_tokens().(&self, tokens: &mut TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#) { |
| 184 | match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self { |
| 185 | MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#::DefaultTorust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo# { exprrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo#expr., crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#DefaultTo#crate_root. } => { |
| 186 | let expr = expr.with_crate_rootrust-analyzer cargo derive_builder_core 0.20.2 default_expression/impl#[DefaultExpression]with_crate_root().(crate_root); |
| 187 | tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!(None => #expr)); |
| 188 | } |
| 189 | MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#::UseDefaultStructFieldrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#UseDefaultStructField#(field_ident) => { |
| 190 | let struct_ident = synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#::newrust-analyzer cargo proc-macro2 1.0.107 impl#[Ident]new().(DEFAULT_STRUCT_NAMErust-analyzer cargo derive_builder_core 0.20.2 DEFAULT_STRUCT_NAME., Spanrust-analyzer cargo proc-macro2 1.0.107 Span#::call_siterust-analyzer cargo proc-macro2 1.0.107 impl#[Span]call_site().()); |
| 191 | tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 192 | None => #struct_ident.#field_ident |
| 193 | )) |
| 194 | } |
| 195 | MatchNonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#::ReturnErrorrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError# { |
| 196 | ref field_namerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#field_name., |
| 197 | ref spanrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#span., |
| 198 | crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchNone#ReturnError#crate_root., |
| 199 | } => { |
| 200 | let conv_span = span.unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(Spanrust-analyzer cargo proc-macro2 1.0.107 Span#::call_siterust-analyzer cargo proc-macro2 1.0.107 impl#[Span]call_site().); |
| 201 | // If the conversion fails, the compiler error should point to the error declaration |
| 202 | // rather than the crate root declaration, but the compiler will see the span of #crate_root |
| 203 | // and produce an undesired behavior (possibly because that's the first span in the bad expression?). |
| 204 | // Creating a copy with deeply-rewritten spans preserves the desired error behavior. |
| 205 | let crate_root = change_spanrust-analyzer cargo derive_builder_core 0.20.2 change_span/change_span().(crate_root.into_token_streamrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens#into_token_stream().(), conv_span); |
| 206 | let err_conv = quote_spannedrust-analyzer cargo quote 1.0.47 quote_spanned!!(conv_span => #crate_root::export::core::convert::Into::into( |
| 207 | #crate_root::UninitializedFieldError::from(#field_name) |
| 208 | )); |
| 209 | tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 210 | None => return #crate_root::export::core::result::Result::Err(#err_conv) |
| 211 | )); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /// To be used inside of `#struct_field: match self.#builder_field { ... }` |
| 218 | enum MatchSomerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#<'a> { |
| 219 | Moverust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Move#, |
| 220 | Clonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Clone# { crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Clone#crate_root.: &'a synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path# }, |
| 221 | } |
| 222 | |
| 223 | impl ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens# for MatchSomerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#<'_> { |
| 224 | fn to_tokensrust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`MatchSome<'_>`][ToTokens]to_tokens().(&self, tokens: &mut TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#) { |
| 225 | match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self { |
| 226 | Selfrust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`MatchSome<'_>`][ToTokens]::Moverust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Move# => tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 227 | Some(value) => value |
| 228 | )), |
| 229 | Selfrust-analyzer cargo derive_builder_core 0.20.2 initializer/impl#[`MatchSome<'_>`][ToTokens]::Clonerust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Clone# { crate_rootrust-analyzer cargo derive_builder_core 0.20.2 initializer/MatchSome#Clone#crate_root. } => tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 230 | Some(ref value) => #crate_root::export::core::clone::Clone::clone(value) |
| 231 | )), |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /// Helper macro for unit tests. This is _only_ public in order to be accessible |
| 237 | /// from doc-tests too. |
| 238 | #[doc(hidden)] |
| 239 | #[macro_export] |
| 240 | macro_rules! default_initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/default_initializer! { |
| 241 | () => { |
| 242 | Initializer { |
| 243 | // Deliberately don't use the default value here - make sure |
| 244 | // that all test cases are passing crate_root through properly. |
| 245 | crate_root: &parse_quote!(::db), |
| 246 | field_ident: &syn::Ident::new("foo", ::proc_macro2::Span::call_site()), |
| 247 | field_enabled: true, |
| 248 | builder_pattern: BuilderPattern::Mutable, |
| 249 | default_value: None, |
| 250 | use_default_struct: false, |
| 251 | conversion: FieldConversion::OptionOrDefault, |
| 252 | custom_error_type_span: None, |
| 253 | } |
| 254 | }; |
| 255 | } |
| 256 | |
| 257 | #[cfg(test)] |
| 258 | mod testsrust-analyzer cargo derive_builder_core 0.20.2 initializer/tests/ { |
| 259 | #[allow(unused_imports)] |
| 260 | use superrust-analyzer cargo derive_builder_core 0.20.2 initializer/::*; |
| 261 | |
| 262 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 263 | fn immutablerust-analyzer cargo derive_builder_core 0.20.2 initializer/tests/immutable().() { |
| 264 | let mut initializer = default_initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/default_initializer!!(); |
| 265 | initializer.builder_patternrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#builder_pattern. = BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#::Immutablerust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#Immutable#; |
| 266 | |
| 267 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 268 | quoterust-analyzer cargo quote 1.0.47 quote!!(#initializer).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 269 | quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 270 | foo: match self.foo { |
| 271 | Some(ref value) => ::db::export::core::clone::Clone::clone(value), |
| 272 | None => return ::db::export::core::result::Result::Err(::db::export::core::convert::Into::into( |
| 273 | ::db::UninitializedFieldError::from("foo") |
| 274 | )), |
| 275 | }, |
| 276 | ) |
| 277 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 278 | ); |
| 279 | } |
| 280 | |
| 281 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 282 | fn mutablerust-analyzer cargo derive_builder_core 0.20.2 initializer/tests/mutable().() { |
| 283 | let mut initializer = default_initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/default_initializer!!(); |
| 284 | initializer.builder_patternrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#builder_pattern. = BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#::Mutablerust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#Mutable#; |
| 285 | |
| 286 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 287 | quoterust-analyzer cargo quote 1.0.47 quote!!(#initializer).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 288 | quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 289 | foo: match self.foo { |
| 290 | Some(ref value) => ::db::export::core::clone::Clone::clone(value), |
| 291 | None => return ::db::export::core::result::Result::Err(::db::export::core::convert::Into::into( |
| 292 | ::db::UninitializedFieldError::from("foo") |
| 293 | )), |
| 294 | }, |
| 295 | ) |
| 296 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 301 | fn ownedrust-analyzer cargo derive_builder_core 0.20.2 initializer/tests/owned().() { |
| 302 | let mut initializer = default_initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/default_initializer!!(); |
| 303 | initializer.builder_patternrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#builder_pattern. = BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#::Ownedrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#Owned#; |
| 304 | |
| 305 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 306 | quoterust-analyzer cargo quote 1.0.47 quote!!(#initializer).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 307 | quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 308 | foo: match self.foo { |
| 309 | Some(value) => value, |
| 310 | None => return ::db::export::core::result::Result::Err(::db::export::core::convert::Into::into( |
| 311 | ::db::UninitializedFieldError::from("foo") |
| 312 | )), |
| 313 | }, |
| 314 | ) |
| 315 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 320 | fn default_valuerust-analyzer cargo derive_builder_core 0.20.2 initializer/tests/default_value().() { |
| 321 | let mut initializer = default_initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/default_initializer!!(); |
| 322 | let default_value = DefaultExpressionrust-analyzer cargo derive_builder_core 0.20.2 default_expression/DefaultExpression#::explicitrust-analyzer cargo derive_builder_core 0.20.2 default_expression/impl#[DefaultExpression]explicit().::<synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#>(parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(42)); |
| 323 | initializer.default_valuerust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#default_value. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&default_value); |
| 324 | |
| 325 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 326 | quoterust-analyzer cargo quote 1.0.47 quote!!(#initializer).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 327 | quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 328 | foo: match self.foo { |
| 329 | Some(ref value) => ::db::export::core::clone::Clone::clone(value), |
| 330 | None => { 42 }, |
| 331 | }, |
| 332 | ) |
| 333 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 334 | ); |
| 335 | } |
| 336 | |
| 337 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 338 | fn default_structrust-analyzer cargo derive_builder_core 0.20.2 initializer/tests/default_struct().() { |
| 339 | let mut initializer = default_initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/default_initializer!!(); |
| 340 | initializer.use_default_structrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#use_default_struct. = true; |
| 341 | |
| 342 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 343 | quoterust-analyzer cargo quote 1.0.47 quote!!(#initializer).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 344 | quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 345 | foo: match self.foo { |
| 346 | Some(ref value) => ::db::export::core::clone::Clone::clone(value), |
| 347 | None => __default.foo, |
| 348 | }, |
| 349 | ) |
| 350 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 355 | fn setter_disabledrust-analyzer cargo derive_builder_core 0.20.2 initializer/tests/setter_disabled().() { |
| 356 | let mut initializer = default_initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/default_initializer!!(); |
| 357 | initializer.field_enabledrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#field_enabled. = false; |
| 358 | |
| 359 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 360 | quoterust-analyzer cargo quote 1.0.47 quote!!(#initializer).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 361 | quoterust-analyzer cargo quote 1.0.47 quote!!(foo: ::db::export::core::default::Default::default(),).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 366 | fn no_stdrust-analyzer cargo derive_builder_core 0.20.2 initializer/tests/no_std().() { |
| 367 | let initializer = default_initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/default_initializer!!(); |
| 368 | |
| 369 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 370 | quoterust-analyzer cargo quote 1.0.47 quote!!(#initializer).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 371 | quoterust-analyzer cargo quote 1.0.47 quote!!( |
| 372 | foo: match self.foo { |
| 373 | Some(ref value) => ::db::export::core::clone::Clone::clone(value), |
| 374 | None => return ::db::export::core::result::Result::Err(::db::export::core::convert::Into::into( |
| 375 | ::db::UninitializedFieldError::from("foo") |
| 376 | )), |
| 377 | }, |
| 378 | ) |
| 379 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 380 | ); |
| 381 | } |
| 382 | } |