Debian Codegraph @

src/build_method.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
2
3 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#};
4 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#};
5 use synrust-analyzer cargo syn 2.0.119 crate/::spannedrust-analyzer cargo syn 2.0.119 spanned/::Spannedrust-analyzer cargo syn 2.0.119 spanned/Spanned#;
6
7 use craterust-analyzer cargo derive_builder_core 0.20.2 crate/::{
8 doc_comment_fromrust-analyzer cargo derive_builder_core 0.20.2 doc_comment/doc_comment_from()., BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#, DefaultExpressionrust-analyzer cargo derive_builder_core 0.20.2 default_expression/DefaultExpression#, Initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#, DEFAULT_STRUCT_NAMErust-analyzer cargo derive_builder_core 0.20.2 DEFAULT_STRUCT_NAME.,
9 };
10
11 /// Initializer for the struct fields in the build method, implementing
12 /// `quote::ToTokens`.
13 ///
14 /// # Examples
15 ///
16 /// Will expand to something like the following (depending on settings):
17 ///
18 /// ```rust,ignore
19 /// # extern crate proc_macro2;
20 /// # #[macro_use]
21 /// # extern crate quote;
22 /// # extern crate syn;
23 /// # #[macro_use(default_build_method)]
24 /// # extern crate derive_builder_core;
25 /// # use derive_builder_core::{BuildMethod, BuilderPattern};
26 /// # fn main() {
27 /// # let build_method = default_build_method!();
28 /// #
29 /// # assert_eq!(quote!(#build_method).to_string(), quote!(
30 /// pub fn build(&self) -> ::derive_builder::export::core::result::Result<Foo, FooBuilderError> {
31 /// Ok(Foo {
32 /// foo: self.foo,
33 /// })
34 /// }
35 /// # ).to_string());
36 /// # }
37 /// ```
38 #[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:)]
39 pub struct BuildMethodrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#<'a> {
40 /// Path to the root of the derive_builder crate.
41 pub crate_rootrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#crate_root.: &'a synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#,
42 /// Enables code generation for this build method.
43 pub enabledrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#enabled.: bool,
44 /// Name of this build fn.
45 pub identrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#ident.: &'a synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#,
46 /// Visibility of the build method, e.g. `syn::Visibility::Public`.
47 pub visibilityrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#visibility.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, synrust-analyzer cargo syn 2.0.119 crate/::Visibilityrust-analyzer cargo syn 2.0.119 restriction/Visibility#>,
48 /// How the build method takes and returns `self` (e.g. mutably).
49 pub patternrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#pattern.: BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#,
50 /// Type of the target field.
51 ///
52 /// The corresonding builder field will be `Option<field_type>`.
53 pub target_tyrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#target_ty.: &'a synrust-analyzer cargo syn 2.0.119 crate/::Identrust-analyzer cargo proc-macro2 1.0.107 Ident#,
54 /// Type parameters and lifetimes attached to this builder struct.
55 pub target_ty_genericsrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#target_ty_generics.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.119 crate/::TypeGenericsrust-analyzer cargo syn 2.0.119 generics/TypeGenerics#<'a>>,
56 /// Type of error.
57 pub error_tyrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#error_ty.: synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#,
58 /// Field initializers for the target type.
59 pub initializersrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#initializers.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#>,
60 /// Doc-comment of the builder struct.
61 pub doc_commentrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#doc_comment.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<synrust-analyzer cargo syn 2.0.119 crate/::Attributerust-analyzer cargo syn 2.0.119 attr/Attribute#>,
62 /// Default value for the whole struct.
63 ///
64 /// This will be in scope for all initializers as `__default`.
65 pub default_structrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#default_struct.: 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#>,
66 /// Validation function with signature `&FooBuilder -> Result<(), String>`
67 /// to call before the macro-provided struct buildout.
68 pub validate_fnrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#validate_fn.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#>,
69 }
70
71 impl<'a> ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens# for BuildMethodrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#<'a> {
72 fn to_tokensrust-analyzer cargo derive_builder_core 0.20.2 build_method/impl#[`BuildMethod<'a>`][ToTokens]to_tokens().(&self, tokens: &mut TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#) {
73 let ident = &self.identrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#ident.;
74 let vis = &self.visibilityrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#visibility.;
75 let target_ty = &self.target_tyrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#target_ty.;
76 let target_ty_generics = &self.target_ty_genericsrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#target_ty_generics.;
77 let initializers = &self.initializersrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#initializers.;
78 let self_param = match self.patternrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#pattern. {
79 BuilderPatternrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#::Ownedrust-analyzer cargo derive_builder_core 0.20.2 options/BuilderPattern#Owned# => quoterust-analyzer cargo quote 1.0.47 quote!!(self),
80 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# => quoterust-analyzer cargo quote 1.0.47 quote!!(&self),
81 };
82 let doc_comment = &self.doc_commentrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#doc_comment.;
83 let default_struct = self.default_structrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#default_struct..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|default_expr| {
84 let default_expr = default_expr.with_crate_rootrust-analyzer cargo derive_builder_core 0.20.2 default_expression/impl#[DefaultExpression]with_crate_root().(self.crate_rootrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#crate_root.);
85 let 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().());
86 quoterust-analyzer cargo quote 1.0.47 quote!!(let #ident: #target_ty #target_ty_generics = #default_expr;)
87 });
88 let validate_fn = self
89 .validate_fnrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#validate_fn.
90 .as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().()
91 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|vfn| quote_spannedrust-analyzer cargo quote 1.0.47 quote_spanned!!(vfn.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().() => #vfn(&self)?;));
92 let error_ty = &self.error_tyrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#error_ty.;
93
94 if self.enabledrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#enabled. {
95 let crate_root = &self.crate_rootrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#crate_root.;
96 tokens.append_allrust-analyzer cargo quote 1.0.47 ext/impl#[TokenStream][TokenStreamExt]append_all().(quoterust-analyzer cargo quote 1.0.47 quote!!(
97 #doc_comment
98 #vis fn #ident(#self_param)
99 -> #crate_root::export::core::result::Result<#target_ty #target_ty_generics, #error_ty>
100 {
101 #validate_fn
102 #default_struct
103 Ok(#target_ty {
104 #(#initializers)*
105 })
106 }
107 ))
108 }
109 }
110 }
111
112 impl<'a> BuildMethodrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#<'a> {
113 /// Set a doc-comment for this item.
114 pub fn doc_commentrust-analyzer cargo derive_builder_core 0.20.2 build_method/impl#[`BuildMethod<'a>`]doc_comment().(&mut self, s: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> &mut Selfrust-analyzer cargo derive_builder_core 0.20.2 build_method/impl#[`BuildMethod<'a>`] {
115 self.doc_commentrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#doc_comment. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(doc_comment_fromrust-analyzer cargo derive_builder_core 0.20.2 doc_comment/doc_comment_from().(s));
116 self
117 }
118
119 /// Populate the `BuildMethod` with appropriate initializers of the
120 /// underlying struct.
121 ///
122 /// For each struct field this must be called with the appropriate
123 /// initializer.
124 pub fn push_initializerrust-analyzer cargo derive_builder_core 0.20.2 build_method/impl#[`BuildMethod<'a>`]push_initializer().(&mut self, init: Initializerrust-analyzer cargo derive_builder_core 0.20.2 initializer/Initializer#) -> &mut Selfrust-analyzer cargo derive_builder_core 0.20.2 build_method/impl#[`BuildMethod<'a>`] {
125 self.initializersrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#initializers..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(quoterust-analyzer cargo quote 1.0.47 quote!!(#init));
126 self
127 }
128 }
129
130 // pub struct BuildMethodError {
131 // is_generated: bool,
132 // ident: syn::Ident,
133 // }
134
135 /// Helper macro for unit tests. This is _only_ public in order to be accessible
136 /// from doc-tests too.
137 #[doc(hidden)]
138 #[macro_export]
139 macro_rules! default_build_methodrust-analyzer cargo derive_builder_core 0.20.2 build_method/default_build_method! {
140 () => {
141 BuildMethod {
142 // Deliberately don't use the default value here - make sure
143 // that all test cases are passing crate_root through properly.
144 crate_root: &parse_quote!(::db),
145 enabled: true,
146 ident: &syn::Ident::new("build", ::proc_macro2::Span::call_site()),
147 visibility: ::std::borrow::Cow::Owned(syn::parse_quote!(pub)),
148 pattern: BuilderPattern::Mutable,
149 target_ty: &syn::Ident::new("Foo", ::proc_macro2::Span::call_site()),
150 target_ty_generics: None,
151 error_ty: syn::parse_quote!(FooBuilderError),
152 initializers: vec![quote!(foo: self.foo,)],
153 doc_comment: None,
154 default_struct: None,
155 validate_fn: None,
156 }
157 };
158 }
159
160 #[cfg(test)]
161 mod testsrust-analyzer cargo derive_builder_core 0.20.2 build_method/tests/ {
162 #[allow(unused_imports)]
163 use superrust-analyzer cargo derive_builder_core 0.20.2 build_method/::*;
164
165 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
166 fn stdrust-analyzer cargo derive_builder_core 0.20.2 build_method/tests/std().() {
167 let build_method = default_build_methodrust-analyzer cargo derive_builder_core 0.20.2 build_method/default_build_method!!();
168
169 #[rustfmt::skip]
170 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
171 quoterust-analyzer cargo quote 1.0.47 quote!!(#build_method).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
172 quoterust-analyzer cargo quote 1.0.47 quote!!(
173 pub fn build(&self) -> ::db::export::core::result::Result<Foo, FooBuilderError> {
174 Ok(Foo {
175 foo: self.foo,
176 })
177 }
178 )
179 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
180 );
181 }
182
183 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
184 fn default_structrust-analyzer cargo derive_builder_core 0.20.2 build_method/tests/default_struct().() {
185 let mut build_method = default_build_methodrust-analyzer cargo derive_builder_core 0.20.2 build_method/default_build_method!!();
186 let alt_default =
187 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!!(Default::default()));
188 build_method.default_structrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#default_struct. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&alt_default);
189
190 #[rustfmt::skip]
191 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
192 quoterust-analyzer cargo quote 1.0.47 quote!!(#build_method).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
193 quoterust-analyzer cargo quote 1.0.47 quote!!(
194 pub fn build(&self) -> ::db::export::core::result::Result<Foo, FooBuilderError> {
195 let __default: Foo = { Default::default() };
196 Ok(Foo {
197 foo: self.foo,
198 })
199 }
200 )
201 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
202 );
203 }
204
205 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
206 fn skiprust-analyzer cargo derive_builder_core 0.20.2 build_method/tests/skip().() {
207 let mut build_method = default_build_methodrust-analyzer cargo derive_builder_core 0.20.2 build_method/default_build_method!!();
208 build_method.enabledrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#enabled. = false;
209 build_method.enabledrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#enabled. = false;
210
211 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(quoterust-analyzer cargo quote 1.0.47 quote!!(#build_method).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), quoterust-analyzer cargo quote 1.0.47 quote!!().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
212 }
213
214 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
215 fn renamerust-analyzer cargo derive_builder_core 0.20.2 build_method/tests/rename().() {
216 let 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().("finish", Spanrust-analyzer cargo proc-macro2 1.0.107 Span#::call_siterust-analyzer cargo proc-macro2 1.0.107 impl#[Span]call_site().());
217 let mut build_method: BuildMethodrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod# = default_build_methodrust-analyzer cargo derive_builder_core 0.20.2 build_method/default_build_method!!();
218 build_method.identrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#ident. = &ident;
219
220 #[rustfmt::skip]
221 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
222 quoterust-analyzer cargo quote 1.0.47 quote!!(#build_method).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
223 quoterust-analyzer cargo quote 1.0.47 quote!!(
224 pub fn finish(&self) -> ::db::export::core::result::Result<Foo, FooBuilderError> {
225 Ok(Foo {
226 foo: self.foo,
227 })
228 }
229 )
230 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
231 );
232 }
233
234 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
235 fn validationrust-analyzer cargo derive_builder_core 0.20.2 build_method/tests/validation().() {
236 let validate_path: synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path# = parse_quoterust-analyzer cargo syn 2.0.119 parse_quote/parse_quote!!(IpsumBuilder::validate);
237
238 let mut build_method: BuildMethodrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod# = default_build_methodrust-analyzer cargo derive_builder_core 0.20.2 build_method/default_build_method!!();
239 build_method.validate_fnrust-analyzer cargo derive_builder_core 0.20.2 build_method/BuildMethod#validate_fn. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&validate_path);
240
241 #[rustfmt::skip]
242 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
243 quoterust-analyzer cargo quote 1.0.47 quote!!(#build_method).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
244 quoterust-analyzer cargo quote 1.0.47 quote!!(
245 pub fn build(&self) -> ::db::export::core::result::Result<Foo, FooBuilderError> {
246 IpsumBuilder::validate(&self)?;
247
248 Ok(Foo {
249 foo: self.foo,
250 })
251 }
252 )
253 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
254 );
255 }
256 }