Skip to content

Commit 465b648

Browse files
committed
impl Parse for ReceivedAttrs
1 parent 3db3e10 commit 465b648

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

glib-macros/src/properties.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ enum PropAttr {
8585
Get(Option<syn::Expr>),
8686
Set(Option<syn::Expr>),
8787

88-
// ident [= expr]
88+
// ident = expr
8989
OverrideClass(syn::Type),
9090
OverrideInterface(syn::Type),
9191

@@ -177,33 +177,19 @@ struct ReceivedAttrs {
177177
builder_fields: HashMap<syn::Ident, Option<syn::Expr>>,
178178
}
179179

180-
impl ReceivedAttrs {
181-
fn new(
182-
attrs_span: &proc_macro2::Span,
183-
attrs: impl IntoIterator<Item = PropAttr>,
184-
) -> syn::Result<Self> {
180+
impl Parse for ReceivedAttrs {
181+
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
182+
let attrs = syn::punctuated::Punctuated::<PropAttr, Token![,]>::parse_terminated(input)?;
185183
let this = attrs.into_iter().fold(Self::default(), |mut this, attr| {
186184
this.set_from_attr(attr);
187185
this
188186
});
189187

190-
if this.get.is_none() && this.set.is_none() {
191-
return Err(syn::Error::new(
192-
*attrs_span,
193-
"No `get` or `set` specified: at least one is required.".to_string(),
194-
));
195-
}
196-
197-
if this.override_class.is_some() && this.override_interface.is_some() {
198-
return Err(syn::Error::new(
199-
*attrs_span,
200-
"Both `override_class` and `override_interface` specified.".to_string(),
201-
));
202-
}
203-
204188
Ok(this)
205189
}
190+
}
206191

192+
impl ReceivedAttrs {
207193
fn set_from_attr(&mut self, attr: PropAttr) {
208194
match attr {
209195
PropAttr::Get(some_fn) => self.get = Some(some_fn.into()),
@@ -258,6 +244,21 @@ impl PropDesc {
258244
builder_fields,
259245
} = attrs;
260246

247+
if get.is_none() && set.is_none() {
248+
return Err(syn::Error::new(
249+
attrs_span,
250+
"No `get` or `set` specified: at least one is required.".to_string(),
251+
));
252+
}
253+
254+
if override_class.is_some() && override_interface.is_some() {
255+
return Err(syn::Error::new(
256+
attrs_span,
257+
"Both `override_class` and `override_interface` specified.".to_string(),
258+
));
259+
}
260+
261+
261262
// Fill needed, but missing, attributes with calculated default values
262263
let name = name.unwrap_or_else(|| {
263264
syn::LitStr::new(
@@ -469,16 +470,13 @@ fn parse_fields(fields: syn::Fields) -> syn::Result<Vec<PropDesc>> {
469470
attrs
470471
.into_iter()
471472
.filter(|a| a.path.is_ident("property"))
472-
.map(move |attrs| {
473-
let span = attrs.span();
474-
let attrs = attrs.parse_args_with(
475-
syn::punctuated::Punctuated::<PropAttr, Token![,]>::parse_terminated,
476-
)?;
473+
.map(move |prop_attrs| {
474+
let span = prop_attrs.span();
477475
PropDesc::new(
478476
span,
479477
ident.as_ref().unwrap().clone(),
480478
ty.clone(),
481-
ReceivedAttrs::new(&span, attrs)?,
479+
prop_attrs.parse_args()?
482480
)
483481
})
484482
})

0 commit comments

Comments
 (0)