|
1 | 1 | use crate::device_path::util::is_doc_attr; |
2 | 2 | use proc_macro2::{Span, TokenStream}; |
3 | 3 | use quote::{quote, ToTokens, TokenStreamExt}; |
4 | | -use syn::{ |
5 | | - Attribute, Expr, ExprLit, Field, Ident, Lit, Meta, MetaList, MetaNameValue, NestedMeta, Path, |
6 | | - Type, TypeArray, |
7 | | -}; |
| 4 | +use syn::{Attribute, Expr, ExprLit, Field, Ident, Lit, Path, Type, TypeArray}; |
8 | 5 |
|
9 | 6 | /// A fixed-size non-array type. |
10 | 7 | /// |
@@ -344,58 +341,44 @@ impl FieldNodeAttr { |
344 | 341 | /// readme. Returns `None` if the attribute does not exactly match |
345 | 342 | /// the expected format. |
346 | 343 | fn from_attr(attr: &Attribute) -> Option<Self> { |
347 | | - let meta = attr.parse_meta().ok()?; |
348 | | - |
349 | | - if let Meta::List(MetaList { path, nested, .. }) = meta { |
350 | | - if path.get_ident()? != "node" { |
351 | | - return None; |
352 | | - } |
| 344 | + if !attr.path().is_ident("node") { |
| 345 | + return None; |
| 346 | + } |
353 | 347 |
|
354 | | - let mut out = Self::default(); |
355 | | - |
356 | | - for nested in nested.iter() { |
357 | | - match nested { |
358 | | - NestedMeta::Meta(Meta::Path(path)) => { |
359 | | - let ident = path.get_ident()?; |
360 | | - if ident == "no_get_func" { |
361 | | - out.get_func = GetFunc::None; |
362 | | - } else if ident == "custom_get_impl" { |
363 | | - out.get_func = GetFunc::Custom; |
364 | | - } else if ident == "custom_build_impl" { |
365 | | - out.custom_build_impl = true; |
366 | | - } else if ident == "custom_build_size_impl" { |
367 | | - out.custom_build_size_impl = true; |
368 | | - } else { |
369 | | - return None; |
370 | | - } |
| 348 | + let mut out = Self::default(); |
| 349 | + |
| 350 | + attr.parse_nested_meta(|meta| { |
| 351 | + let path = &meta.path; |
| 352 | + if path.is_ident("no_get_func") { |
| 353 | + out.get_func = GetFunc::None; |
| 354 | + } else if path.is_ident("custom_get_impl") { |
| 355 | + out.get_func = GetFunc::Custom; |
| 356 | + } else if path.is_ident("custom_build_impl") { |
| 357 | + out.custom_build_impl = true; |
| 358 | + } else if path.is_ident("custom_build_size_impl") { |
| 359 | + out.custom_build_size_impl = true; |
| 360 | + } else if path.is_ident("build_type") { |
| 361 | + let value = meta.value()?; |
| 362 | + let lit: Lit = value.parse()?; |
| 363 | + |
| 364 | + match lit { |
| 365 | + Lit::Str(s) => { |
| 366 | + out.build_type = BuildType::Custom(syn::parse_str(&s.value())?); |
371 | 367 | } |
372 | | - NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit, .. })) => { |
373 | | - if path.get_ident()? != "build_type" { |
374 | | - return None; |
375 | | - } |
376 | | - |
377 | | - match lit { |
378 | | - Lit::Str(s) => { |
379 | | - out.build_type = |
380 | | - BuildType::Custom(syn::parse_str(&s.value()).ok()?); |
381 | | - } |
382 | | - Lit::Bool(b) if !b.value() => { |
383 | | - out.build_type = BuildType::None; |
384 | | - } |
385 | | - _ => { |
386 | | - return None; |
387 | | - } |
388 | | - } |
| 368 | + Lit::Bool(b) if !b.value() => { |
| 369 | + out.build_type = BuildType::None; |
389 | 370 | } |
390 | 371 | _ => { |
391 | | - return None; |
| 372 | + return Err(meta.error("invalid build_type")); |
392 | 373 | } |
393 | 374 | } |
| 375 | + } else { |
| 376 | + return Err(meta.error("invalid field node attribute")); |
394 | 377 | } |
| 378 | + Ok(()) |
| 379 | + }) |
| 380 | + .ok()?; |
395 | 381 |
|
396 | | - Some(out) |
397 | | - } else { |
398 | | - None |
399 | | - } |
| 382 | + Some(out) |
400 | 383 | } |
401 | 384 | } |
0 commit comments