Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying arbitrary attributes on the builder struct #237

Merged
merged 15 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions derive_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,7 @@
//! #[derive(Builder)]
//! #[builder(derive(serde::Serialize))]
//! struct Lorem {
//! #[builder_field_attrs(
//! #[serde(rename="dolor")]
//! )]
//! #[builder_field_attrs(serde(rename="dolor"))]
ijackson marked this conversation as resolved.
Show resolved Hide resolved
//! ipsum: String,
//! }
//!
Expand Down
4 changes: 1 addition & 3 deletions derive_builder/tests/compile-fail/builder_field_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ pub struct Lorem {
ok: String,

// This foolish repr() attribute generates an unused attribute warning
ijackson marked this conversation as resolved.
Show resolved Hide resolved
#[builder_field_attrs(
#[no_such_attribute]
)]
#[builder_field_attrs(no_such_attribute)]
broken: String,
}

Expand Down
8 changes: 4 additions & 4 deletions derive_builder/tests/compile-fail/builder_field_attrs.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: cannot find attribute `no_such_attribute` in this scope
--> tests/compile-fail/builder_field_attrs.rs:10:11
|
10 | #[no_such_attribute]
| ^^^^^^^^^^^^^^^^^
--> tests/compile-fail/builder_field_attrs.rs:9:27
|
9 | #[builder_field_attrs(no_such_attribute)]
| ^^^^^^^^^^^^^^^^^
4 changes: 1 addition & 3 deletions derive_builder/tests/compile-fail/builder_setter_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pub struct Lorem {
ok: String,

// This foolish repr() attribute generates an unused attribute warning
ijackson marked this conversation as resolved.
Show resolved Hide resolved
#[builder_setter_attrs(
#[must_use]
)]
#[builder_setter_attrs(must_use)]
broken: usize,
}

Expand Down
4 changes: 2 additions & 2 deletions derive_builder/tests/compile-fail/builder_setter_attrs.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: unused return value of `LoremBuilder::broken` that must be used
--> tests/compile-fail/builder_setter_attrs.rs:18:5
--> tests/compile-fail/builder_setter_attrs.rs:16:5
|
18 | LoremBuilder::default().broken(42);
16 | LoremBuilder::default().broken(42);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: lint level defined here
Expand Down
17 changes: 13 additions & 4 deletions derive_builder_core/src/macro_options/darling_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::BuildMethod;

use darling::util::{Flag, PathList};
use darling::{self, FromMeta};
use proc_macro2::Span;
use syn::parse::ParseStream;
use proc_macro2::{Span, TokenStream};
use syn::parse::{ParseStream, Parser};
use syn::Meta;
use syn::{self, spanned::Spanned, Attribute, Generics, Ident, Path, Visibility};

Expand Down Expand Up @@ -303,9 +303,18 @@ fn unnest_from_one_attribute(attr: syn::Attribute) -> darling::Result<Vec<Attrib
struct ContainedAttributes(Vec<syn::Attribute>);
impl syn::parse::Parse for ContainedAttributes {
fn parse(input: ParseStream) -> syn::Result<Self> {
// Strip parentheses, and save the span of the parenthesis token
let content;
let _paren_token = parenthesized!(content in input);
let attrs = content.call(syn::Attribute::parse_outer)?;
let paren_token = parenthesized!(content in input);
let wrap_span = paren_token.span;

// Wrap up in #[ ] instead.
let pound = Token![#](wrap_span); // We can't write a literal # inside quote
let content: TokenStream = content.parse()?;
let content = quote_spanned!(wrap_span=> #pound [ #content ]);

let parser = syn::Attribute::parse_outer;
let attrs = parser.parse2(content)?;
Ok(Self(attrs))
}
}
Expand Down