-
Notifications
You must be signed in to change notification settings - Fork 52
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
Optionally transform Extend
fields to add items one by one
#46
base: master
Are you sure you want to change the base?
Changes from 1 commit
486480b
c824836
03e0e58
b19a01b
4376566
8e4b301
7635462
a3d3e31
c9bba54
586d3dc
23ca53a
094a827
4a2afb0
a3006a1
9bb9dbf
4c67cf4
c8abc4d
8cc816a
3a8ff5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use proc_macro2::{Span, TokenStream}; | ||
use quote::{quote, quote_spanned, ToTokens}; | ||
use std::fmt::Write; | ||
use syn::{parse::Error, spanned::Spanned}; | ||
|
||
use crate::field_info::{Configured, ExtendField, FieldBuilderAttr, FieldInfo, SetterSettings}; | ||
|
@@ -287,7 +288,7 @@ impl<'a> StructInfo<'a> { | |
keyword_span, | ||
from_first, | ||
from_iter, | ||
item_name, | ||
item_name: _, | ||
}) = &field.builder_attr.setter.extend | ||
{ | ||
// Changing the builder field type entirely here (instead of just the argument) means there | ||
|
@@ -299,12 +300,7 @@ impl<'a> StructInfo<'a> { | |
field_type | ||
}; | ||
|
||
let item_name = item_name.clone().unwrap_or_else(|| { | ||
syn::Ident::new( | ||
(field.name.to_string() + "_item").trim_start().trim_start_matches("r#"), | ||
field.name.span(), | ||
) | ||
}); | ||
let item_name = syn::Ident::new(&field.item_name(), field.name.span()); | ||
|
||
let descructuring = descructuring.collect::<Vec<_>>(); | ||
let reconstructing = reconstructing.collect::<Vec<_>>(); | ||
|
@@ -609,7 +605,10 @@ impl<'a> StructInfo<'a> { | |
), | ||
proc_macro2::Span::call_site(), | ||
); | ||
let early_build_error_message = format!("Missing required field {}", field_name); | ||
let mut early_build_error_message = format!("Missing required field {}", field_name); | ||
if field.builder_attr.setter.extend.is_some() { | ||
write!(&mut early_build_error_message, " (single item setter: {})", field.item_name()).unwrap(); | ||
} | ||
Comment on lines
+619
to
+622
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There probably should be a special message in case neither of these are available as initialisers. (As an aside, I've just been able to confirm this feature will work for my use-case 🥳 |
||
|
||
Ok(quote! { | ||
#[doc(hidden)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to use
quote_spanned
quite often even when it's not strictly necessary, since it tends to make for better error locations.My earlier hack with
#[forbid(unused_variables)]
is gone, of course, but this should still flag some type/trait errors on the field itself or the matching attribute or attribute parameter.