derive_builder_core/builder.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
use std::borrow::Cow;
use proc_macro2::TokenStream;
use quote::{format_ident, ToTokens, TokenStreamExt};
use syn::punctuated::Punctuated;
use syn::{Path, TraitBound, TraitBoundModifier, TypeParamBound};
use crate::{doc_comment_from, BuildMethod, BuilderField, BuilderPattern, Setter};
const ALLOC_NOT_ENABLED_ERROR: &str = r#"`alloc` is disabled within 'derive_builder', consider one of the following:
* enable feature `alloc` on 'dervie_builder' if a `global_allocator` is present
* use a custom error `#[builder(build_fn(error = "path::to::Error"))]
* disable the validation error `#[builder(build_fn(error(validation_error = false)))]"#;
/// Builder, implementing `quote::ToTokens`.
///
/// # Examples
///
/// Will expand to something like the following (depending on settings):
///
/// ```rust,ignore
/// # extern crate proc_macro2;
/// # #[macro_use]
/// # extern crate quote;
/// # extern crate syn;
/// # #[macro_use]
/// # extern crate derive_builder_core;
/// # use quote::TokenStreamExt;
/// # use derive_builder_core::{Builder, DeprecationNotes};
/// # fn main() {
/// # let builder = default_builder!();
/// #
/// # assert_eq!(
/// # quote!(#builder).to_string(),
/// # {
/// # let mut result = quote!();
/// # #[cfg(not(feature = "clippy"))]
/// # result.append_all(quote!(#[allow(clippy::all)]));
/// #
/// # result.append_all(quote!(
/// #[derive(Clone)]
/// pub struct FooBuilder {
/// foo: u32,
/// }
///
/// #[doc="Error type for FooBuilder"]
/// #[derive(Debug)]
/// #[non_exhaustive]
/// pub enum FooBuilderError {
/// /// Uninitialized field
/// UninitializedField(&'static str),
/// /// Custom validation error
/// ValidationError(::derive_builder::export::core::string::String),
/// }
///
/// impl ::derive_builder::export::core::convert::From<... various ...> for FooBuilderError {}
///
/// #[cfg(not(no_std))]
/// impl std::error::Error for FooBuilderError {}
/// # ));
/// # #[cfg(not(feature = "clippy"))]
/// # result.append_all(quote!(#[allow(clippy::all)]));
/// #
/// # result.append_all(quote!(
///
/// #[allow(dead_code)]
/// impl FooBuilder {
/// fn bar () -> {
/// unimplemented!()
/// }
/// }
///
/// impl ::derive_builder::export::core::default::Default for FooBuilder {
/// fn default() -> Self {
/// Self {
/// foo: ::derive_builder::export::core::default::Default::default(),
/// }
/// }
/// }
///
/// # ));
/// # result
/// # }.to_string()
/// # );
/// # }
/// ```
#[derive(Debug)]
pub struct Builder<'a> {
/// Path to the root of the derive_builder crate.
pub crate_root: &'a Path,
/// Enables code generation for this builder struct.
pub enabled: bool,
/// Name of this builder struct.
pub ident: syn::Ident,
/// Pattern of this builder struct.
pub pattern: BuilderPattern,
/// Traits to automatically derive on the builder type.
pub derives: &'a [Path],
/// Attributes to include on the builder `struct` declaration.
pub struct_attrs: &'a [syn::Attribute],
/// Attributes to include on the builder's inherent `impl` block.
pub impl_attrs: &'a [syn::Attribute],
/// When true, generate `impl Default for #ident` which calls the `create_empty` inherent method.
///
/// Note that the name of `create_empty` can be overridden; see the `create_empty` field for more.
pub impl_default: bool,
/// The identifier of the inherent method that creates a builder with all fields set to
/// `None` or `PhantomData`.
///
/// This method will be invoked by `impl Default` for the builder, but it is also accessible
/// to `impl` blocks on the builder that expose custom constructors.
pub create_empty: syn::Ident,
/// Type parameters and lifetimes attached to this builder's struct
/// definition.
pub generics: Option<&'a syn::Generics>,
/// Visibility of the builder struct, e.g. `syn::Visibility::Public`.
pub visibility: Cow<'a, syn::Visibility>,
/// Fields of the builder struct, e.g. `foo: u32,`
///
/// Expects each entry to be terminated by a comma.
pub fields: Vec<TokenStream>,
/// Builder field initializers, e.g. `foo: Default::default(),`
///
/// Expects each entry to be terminated by a comma.
pub field_initializers: Vec<TokenStream>,
/// Functions of the builder struct, e.g. `fn bar() -> { unimplemented!() }`
pub functions: Vec<TokenStream>,
/// Whether or not a generated error type is required.
///
/// This would be `false` in the case where an already-existing error is to be used.
pub generate_error: bool,
/// Whether to include `ValidationError` in the generated enum. Necessary to avoid dependency
/// on `alloc::string`.
///
/// This would be `false` when `build_fn.error.as_validation_error() == Some((false, _))`. This
/// has no effect when `generate_error` is `false`.
pub generate_validation_error: bool,
/// Indicator of `cfg!(not(any(feature = "alloc", feature = "std")))`, as a field for tests
pub no_alloc: bool,
/// Whether this builder must derive `Clone`.
///
/// This is true even for a builder using the `owned` pattern if there is a field whose setter
/// uses a different pattern.
pub must_derive_clone: bool,
/// Doc-comment of the builder struct.
pub doc_comment: Option<syn::Attribute>,
/// Whether or not a libstd is used.
pub std: bool,
}
impl<'a> ToTokens for Builder<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if self.enabled {
let crate_root = self.crate_root;
let builder_vis = &self.visibility;
let builder_ident = &self.ident;
// Splitting because Generics doesn't output WhereClause, see dtolnay/syn#782
let (struct_generics, struct_where_clause) = (
self.generics,
self.generics.and_then(|g| g.where_clause.as_ref()),
);
let bounded_generics = self.compute_impl_bounds();
let (impl_generics, impl_ty_generics, impl_where_clause) =
bounded_generics.split_for_impl();
let builder_fields = &self.fields;
let builder_field_initializers = &self.field_initializers;
let create_empty = &self.create_empty;
let functions = &self.functions;
// Create the comma-separated set of derived traits for the builder
let derive_attr = {
let clone_trait: Path = parse_quote!(Clone);
let mut traits: Punctuated<&Path, Token![,]> = Default::default();
if self.must_derive_clone {
traits.push(&clone_trait);
}
traits.extend(self.derives);
if traits.is_empty() {
quote!()
} else {
quote!(#[derive(#traits)])
}
};
let struct_attrs = self.struct_attrs;
let impl_attrs = self.impl_attrs;
let builder_doc_comment = &self.doc_comment;
#[cfg(not(feature = "clippy"))]
tokens.append_all(quote!(#[allow(clippy::all)]));
// struct_attrs MUST come after derive_attr, otherwise attributes for a derived
// trait will appear before its derivation. As of rustc 1.59.0 this is a compiler
// warning; see https://github.com/rust-lang/rust/issues/79202
tokens.append_all(quote!(
#derive_attr
#(#struct_attrs)*
#builder_doc_comment
#builder_vis struct #builder_ident #struct_generics #struct_where_clause {
#(#builder_fields)*
}
));
#[cfg(not(feature = "clippy"))]
tokens.append_all(quote!(#[allow(clippy::all)]));
tokens.append_all(quote!(
#(#impl_attrs)*
#[allow(dead_code)]
impl #impl_generics #builder_ident #impl_ty_generics #impl_where_clause {
#(#functions)*
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn #create_empty() -> Self {
Self {
#(#builder_field_initializers)*
}
}
}
));
if self.impl_default {
tokens.append_all(quote!(
impl #impl_generics #crate_root::export::core::default::Default for #builder_ident #impl_ty_generics #impl_where_clause {
fn default() -> Self {
Self::#create_empty()
}
}
));
}
if self.no_alloc && self.generate_error && self.generate_validation_error {
let err = syn::Error::new_spanned(&self.ident, ALLOC_NOT_ENABLED_ERROR);
tokens.append_all(err.to_compile_error());
} else if self.generate_error {
let builder_error_ident = format_ident!("{}Error", builder_ident);
let builder_error_doc = format!("Error type for {}", builder_ident);
let validation_error = if self.generate_validation_error {
quote!(
/// Custom validation error
ValidationError(#crate_root::export::core::string::String),
)
} else {
TokenStream::new()
};
let validation_from = if self.generate_validation_error {
quote!(
impl #crate_root::export::core::convert::From<#crate_root::export::core::string::String> for #builder_error_ident {
fn from(s: #crate_root::export::core::string::String) -> Self {
Self::ValidationError(s)
}
}
)
} else {
TokenStream::new()
};
let validation_display = if self.generate_validation_error {
quote!(
Self::ValidationError(ref error) => write!(f, "{}", error),
)
} else {
TokenStream::new()
};
tokens.append_all(quote!(
#[doc=#builder_error_doc]
#[derive(Debug)]
#[non_exhaustive]
#builder_vis enum #builder_error_ident {
/// Uninitialized field
UninitializedField(&'static str),
#validation_error
}
impl #crate_root::export::core::convert::From<#crate_root::UninitializedFieldError> for #builder_error_ident {
fn from(s: #crate_root::UninitializedFieldError) -> Self {
Self::UninitializedField(s.field_name())
}
}
#validation_from
impl #crate_root::export::core::fmt::Display for #builder_error_ident {
fn fmt(&self, f: &mut #crate_root::export::core::fmt::Formatter) -> #crate_root::export::core::fmt::Result {
match self {
Self::UninitializedField(ref field) => write!(f, "`{}` must be initialized", field),
#validation_display
}
}
}
));
if self.std {
tokens.append_all(quote!(
impl std::error::Error for #builder_error_ident {}
));
}
}
}
}
}
impl<'a> Builder<'a> {
/// Set a doc-comment for this item.
pub fn doc_comment(&mut self, s: String) -> &mut Self {
self.doc_comment = Some(doc_comment_from(s));
self
}
/// Add a field to the builder
pub fn push_field(&mut self, f: BuilderField) -> &mut Self {
self.fields.push(quote!(#f));
self.field_initializers.push(f.default_initializer_tokens());
self
}
/// Add a setter function to the builder
pub fn push_setter_fn(&mut self, f: Setter) -> &mut Self {
self.functions.push(quote!(#f));
self
}
/// Add final build function to the builder
pub fn push_build_fn(&mut self, f: BuildMethod) -> &mut Self {
self.functions.push(quote!(#f));
self
}
/// Add `Clone` trait bound to generic types for non-owned builders.
/// This enables target types to declare generics without requiring a
/// `Clone` impl. This is the same as how the built-in derives for
/// `Clone`, `Default`, `PartialEq`, and other traits work.
fn compute_impl_bounds(&self) -> syn::Generics {
if let Some(type_gen) = self.generics {
let mut generics = type_gen.clone();
if !self.pattern.requires_clone() || type_gen.type_params().next().is_none() {
return generics;
}
let crate_root = self.crate_root;
let clone_bound = TypeParamBound::Trait(TraitBound {
paren_token: None,
modifier: TraitBoundModifier::None,
lifetimes: None,
path: syn::parse_quote!(#crate_root::export::core::clone::Clone),
});
for typ in generics.type_params_mut() {
typ.bounds.push(clone_bound.clone());
}
generics
} else {
Default::default()
}
}
}
/// Helper macro for unit tests. This is _only_ public in order to be accessible
/// from doc-tests too.
#[doc(hidden)]
#[macro_export]
macro_rules! default_builder {
() => {
Builder {
// Deliberately don't use the default value here - make sure
// that all test cases are passing crate_root through properly.
crate_root: &parse_quote!(::db),
enabled: true,
ident: syn::Ident::new("FooBuilder", ::proc_macro2::Span::call_site()),
pattern: Default::default(),
derives: &vec![],
struct_attrs: &vec![],
impl_attrs: &vec![],
impl_default: true,
create_empty: syn::Ident::new("create_empty", ::proc_macro2::Span::call_site()),
generics: None,
visibility: ::std::borrow::Cow::Owned(parse_quote!(pub)),
fields: vec![quote!(foo: u32,)],
field_initializers: vec![quote!(foo: ::db::export::core::default::Default::default(), )],
functions: vec![quote!(fn bar() -> { unimplemented!() })],
generate_error: true,
generate_validation_error: true,
no_alloc: false,
must_derive_clone: true,
doc_comment: None,
std: true,
}
};
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
use syn::Ident;
fn add_simple_foo_builder(result: &mut TokenStream) {
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[derive(Clone)]
pub struct FooBuilder {
foo: u32,
}
));
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[allow(dead_code)]
impl FooBuilder {
fn bar () -> {
unimplemented!()
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
foo: ::db::export::core::default::Default::default(),
}
}
}
impl ::db::export::core::default::Default for FooBuilder {
fn default() -> Self {
Self::create_empty()
}
}
));
}
fn add_generated_error(result: &mut TokenStream) {
result.append_all(quote!(
#[doc="Error type for FooBuilder"]
#[derive(Debug)]
#[non_exhaustive]
pub enum FooBuilderError {
/// Uninitialized field
UninitializedField(&'static str),
/// Custom validation error
ValidationError(::db::export::core::string::String),
}
impl ::db::export::core::convert::From<::db::UninitializedFieldError> for FooBuilderError {
fn from(s: ::db::UninitializedFieldError) -> Self {
Self::UninitializedField(s.field_name())
}
}
impl ::db::export::core::convert::From<::db::export::core::string::String> for FooBuilderError {
fn from(s: ::db::export::core::string::String) -> Self {
Self::ValidationError(s)
}
}
impl ::db::export::core::fmt::Display for FooBuilderError {
fn fmt(&self, f: &mut ::db::export::core::fmt::Formatter) -> ::db::export::core::fmt::Result {
match self {
Self::UninitializedField(ref field) => write!(f, "`{}` must be initialized", field),
Self::ValidationError(ref error) => write!(f, "{}", error),
}
}
}
impl std::error::Error for FooBuilderError {}
));
}
#[test]
fn simple() {
let builder = default_builder!();
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
add_simple_foo_builder(&mut result);
add_generated_error(&mut result);
result
}
.to_string()
);
}
#[test]
fn rename_create_empty() {
let mut builder = default_builder!();
builder.create_empty = Ident::new("empty", proc_macro2::Span::call_site());
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[derive(Clone)]
pub struct FooBuilder {
foo: u32,
}
));
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[allow(dead_code)]
impl FooBuilder {
fn bar () -> {
unimplemented!()
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn empty() -> Self {
Self {
foo: ::db::export::core::default::Default::default(),
}
}
}
impl ::db::export::core::default::Default for FooBuilder {
fn default() -> Self {
Self::empty()
}
}
));
add_generated_error(&mut result);
result
}
.to_string()
);
}
// This test depends on the exact formatting of the `stringify`'d code,
// so we don't automatically format the test
#[rustfmt::skip]
#[test]
fn generic() {
let ast: syn::DeriveInput = parse_quote! {
struct Lorem<'a, T: Debug> where T: PartialEq { }
};
let generics = ast.generics;
let mut builder = default_builder!();
builder.generics = Some(&generics);
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[derive(Clone)]
pub struct FooBuilder<'a, T: Debug> where T: PartialEq {
foo: u32,
}
));
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[allow(dead_code)]
impl<'a, T: Debug + ::db::export::core::clone::Clone> FooBuilder<'a, T> where T: PartialEq {
fn bar() -> {
unimplemented!()
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
foo: ::db::export::core::default::Default::default(),
}
}
}
impl<'a, T: Debug + ::db::export::core::clone::Clone> ::db::export::core::default::Default for FooBuilder<'a, T> where T: PartialEq {
fn default() -> Self {
Self::create_empty()
}
}
));
add_generated_error(&mut result);
result
}.to_string()
);
}
// This test depends on the exact formatting of the `stringify`'d code,
// so we don't automatically format the test
#[rustfmt::skip]
#[test]
fn generic_reference() {
let ast: syn::DeriveInput = parse_quote! {
struct Lorem<'a, T: 'a + Default> where T: PartialEq{ }
};
let generics = ast.generics;
let mut builder = default_builder!();
builder.generics = Some(&generics);
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[derive(Clone)]
pub struct FooBuilder<'a, T: 'a + Default> where T: PartialEq {
foo: u32,
}
));
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[allow(dead_code)]
impl<'a, T: 'a + Default + ::db::export::core::clone::Clone> FooBuilder<'a, T>
where
T: PartialEq
{
fn bar() -> {
unimplemented!()
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
foo: ::db::export::core::default::Default::default(),
}
}
}
impl<'a, T: 'a + Default + ::db::export::core::clone::Clone> ::db::export::core::default::Default for FooBuilder<'a, T> where T: PartialEq {
fn default() -> Self {
Self::create_empty()
}
}
));
add_generated_error(&mut result);
result
}.to_string()
);
}
// This test depends on the exact formatting of the `stringify`'d code,
// so we don't automatically format the test
#[rustfmt::skip]
#[test]
fn generic_with_default_type() {
let ast: syn::DeriveInput = parse_quote! {
struct Lorem<T = ()> { }
};
let generics = ast.generics;
let mut builder = default_builder!();
builder.generics = Some(&generics);
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[derive(Clone)]
pub struct FooBuilder<T = ()> {
foo: u32,
}
));
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[allow(dead_code)]
impl<T: ::db::export::core::clone::Clone> FooBuilder<T>
{
fn bar() -> {
unimplemented!()
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
foo: ::db::export::core::default::Default::default(),
}
}
}
impl<T: ::db::export::core::clone::Clone> ::db::export::core::default::Default for FooBuilder<T> {
fn default() -> Self {
Self::create_empty()
}
}
));
add_generated_error(&mut result);
result
}.to_string()
);
}
// This test depends on the exact formatting of the `stringify`'d code,
// so we don't automatically format the test
#[rustfmt::skip]
#[test]
fn owned_generic() {
let ast: syn::DeriveInput = parse_quote! {
struct Lorem<'a, T: Debug> where T: PartialEq { }
};
let generics = ast.generics;
let mut builder = default_builder!();
builder.generics = Some(&generics);
builder.pattern = BuilderPattern::Owned;
builder.must_derive_clone = false;
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
pub struct FooBuilder<'a, T: Debug> where T: PartialEq {
foo: u32,
}
));
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[allow(dead_code)]
impl<'a, T: Debug> FooBuilder<'a, T> where T: PartialEq {
fn bar() -> {
unimplemented!()
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
foo: ::db::export::core::default::Default::default(),
}
}
}
impl<'a, T: Debug> ::db::export::core::default::Default for FooBuilder<'a, T>
where T: PartialEq {
fn default() -> Self {
Self::create_empty()
}
}
));
add_generated_error(&mut result);
result
}.to_string()
);
}
#[test]
fn disabled() {
let mut builder = default_builder!();
builder.enabled = false;
assert_eq!(quote!(#builder).to_string(), quote!().to_string());
}
#[test]
fn add_derives() {
let derives = vec![parse_quote!(Serialize)];
let mut builder = default_builder!();
builder.derives = &derives;
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[derive(Clone, Serialize)]
pub struct FooBuilder {
foo: u32,
}
));
#[cfg(not(feature = "clippy"))]
result.append_all(quote!(#[allow(clippy::all)]));
result.append_all(quote!(
#[allow(dead_code)]
impl FooBuilder {
fn bar () -> {
unimplemented!()
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
foo: ::db::export::core::default::Default::default(),
}
}
}
impl ::db::export::core::default::Default for FooBuilder {
fn default() -> Self {
Self::create_empty()
}
}
));
add_generated_error(&mut result);
result
}
.to_string()
);
}
#[test]
fn no_validation_error() {
let mut builder = default_builder!();
builder.generate_validation_error = false;
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
add_simple_foo_builder(&mut result);
result.append_all(quote!(
#[doc="Error type for FooBuilder"]
#[derive(Debug)]
#[non_exhaustive]
pub enum FooBuilderError {
/// Uninitialized field
UninitializedField(&'static str),
}
impl ::db::export::core::convert::From<::db::UninitializedFieldError> for FooBuilderError {
fn from(s: ::db::UninitializedFieldError) -> Self {
Self::UninitializedField(s.field_name())
}
}
impl ::db::export::core::fmt::Display for FooBuilderError {
fn fmt(&self, f: &mut ::db::export::core::fmt::Formatter) -> ::db::export::core::fmt::Result {
match self {
Self::UninitializedField(ref field) => write!(f, "`{}` must be initialized", field),
}
}
}
impl std::error::Error for FooBuilderError {}
));
result
}
.to_string()
);
}
#[test]
fn no_alloc_bug_using_string() {
let mut builder = default_builder!();
builder.no_alloc = true;
assert_eq!(
quote!(#builder).to_string(),
{
let mut result = quote!();
add_simple_foo_builder(&mut result);
result.append_all(quote!(::core::compile_error! { #ALLOC_NOT_ENABLED_ERROR }));
result
}
.to_string()
);
}
}