Skip to content

Commit 4728db0

Browse files
authored
Merge pull request #698 from nicholasbishop/bishop-upgrade-xtask-syn
xtask: Upgrade to syn 2.0
2 parents 39cba0b + d9d8730 commit 4728db0

File tree

5 files changed

+63
-103
lines changed

5 files changed

+63
-103
lines changed

xtask/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ proc-macro2 = "1.0.46"
1717
quote = "1.0.21"
1818
regex = "1.5.4"
1919
serde_json = "1.0.73"
20-
syn = { version = "1.0.101", features = ["full"] }
20+
syn = { version = "2.0.0", features = ["full"] }
2121
tempfile = "3.2.0"

xtask/src/device_path/field.rs

+32-49
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use crate::device_path::util::is_doc_attr;
22
use proc_macro2::{Span, TokenStream};
33
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};
85

96
/// A fixed-size non-array type.
107
///
@@ -344,58 +341,44 @@ impl FieldNodeAttr {
344341
/// readme. Returns `None` if the attribute does not exactly match
345342
/// the expected format.
346343
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+
}
353347

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())?);
371367
}
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;
389370
}
390371
_ => {
391-
return None;
372+
return Err(meta.error("invalid build_type"));
392373
}
393374
}
375+
} else {
376+
return Err(meta.error("invalid field node attribute"));
394377
}
378+
Ok(())
379+
})
380+
.ok()?;
395381

396-
Some(out)
397-
} else {
398-
None
399-
}
382+
Some(out)
400383
}
401384
}

xtask/src/device_path/group.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::node::{is_node_attr, Node};
22
use heck::ToUpperCamelCase;
33
use proc_macro2::{Span, TokenStream};
44
use quote::quote;
5-
use syn::{Attribute, Ident, Item, ItemMod, ItemStruct, Meta};
5+
use syn::{Attribute, Ident, Item, ItemMod, ItemStruct};
66

77
#[derive(Clone)]
88
pub struct DeviceType(Ident);
@@ -170,14 +170,7 @@ impl NodeGroup {
170170
}
171171

172172
fn is_build_attr(attr: &Attribute) -> bool {
173-
if let Ok(Meta::Path(path)) = attr.parse_meta() {
174-
if let Some(ident) = path.get_ident() {
175-
if ident == "build" {
176-
return true;
177-
}
178-
}
179-
}
180-
false
173+
attr.path().is_ident("build")
181174
}
182175

183176
fn has_build_attr(item: &Item) -> bool {

xtask/src/device_path/node.rs

+26-36
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use heck::ToShoutySnakeCase;
55
use proc_macro2::Span;
66
use proc_macro2::TokenStream;
77
use quote::quote;
8-
use syn::{Attribute, Fields, Ident, ItemStruct, Lit, Meta, MetaList, MetaNameValue, NestedMeta};
8+
use syn::{Attribute, Fields, Ident, ItemStruct, LitInt, LitStr};
99

1010
/// Device path node specification.
1111
pub struct Node {
@@ -467,44 +467,34 @@ struct NodeAttr {
467467
/// Parse a `node` attribute. Returns `None` for any other attribute, or
468468
/// if the contents don't match the expected format.
469469
fn parse_node_attr(attr: &Attribute) -> Option<NodeAttr> {
470-
let meta = attr.parse_meta().ok()?;
471-
472-
if let Meta::List(MetaList { path, nested, .. }) = meta {
473-
if path.get_ident()? != "node" {
474-
return None;
475-
}
476-
477-
let mut static_size = None;
478-
let mut sub_type = None;
479-
480-
for nested in nested.iter() {
481-
if let NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit, .. })) = nested {
482-
let ident = path.get_ident()?;
470+
if !attr.path().is_ident("node") {
471+
return None;
472+
}
483473

484-
match lit {
485-
Lit::Int(lit) if ident == "static_size" => {
486-
let lit = lit.base10_parse().ok()?;
487-
static_size = Some(lit);
488-
}
489-
Lit::Str(lit) if ident == "sub_type" => {
490-
sub_type = Some(lit.value());
491-
}
492-
_ => {
493-
return None;
494-
}
495-
}
496-
} else {
497-
return None;
498-
}
474+
let mut static_size = None;
475+
let mut sub_type = None;
476+
attr.parse_nested_meta(|meta| {
477+
if meta.path.is_ident("static_size") {
478+
let value = meta.value()?;
479+
let lit: LitInt = value.parse()?;
480+
let lit = lit.base10_parse()?;
481+
static_size = Some(lit);
482+
Ok(())
483+
} else if meta.path.is_ident("sub_type") {
484+
let value = meta.value()?;
485+
let lit: LitStr = value.parse()?;
486+
sub_type = Some(lit.value());
487+
Ok(())
488+
} else {
489+
Err(meta.error("invalid struct node attribute"))
499490
}
491+
})
492+
.ok()?;
500493

501-
Some(NodeAttr {
502-
static_size: static_size?,
503-
sub_type,
504-
})
505-
} else {
506-
None
507-
}
494+
Some(NodeAttr {
495+
static_size: static_size?,
496+
sub_type,
497+
})
508498
}
509499

510500
/// Returns `true` if the attribute is a valid `node` attribute, false

xtask/src/device_path/util.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ use anyhow::{bail, Context, Result};
22
use std::io::Write;
33
use std::process::{Command, Stdio};
44
use std::thread;
5-
use syn::{Attribute, Meta};
5+
use syn::Attribute;
66

77
/// Returns true if the attribute is a `#[doc = "..."]` attribute,
88
/// otherwise returns false.
99
pub fn is_doc_attr(attr: &Attribute) -> bool {
10-
if let Ok(Meta::NameValue(nv)) = attr.parse_meta() {
11-
if let Some(ident) = nv.path.get_ident() {
12-
return ident == "doc";
13-
}
14-
}
15-
16-
false
10+
attr.path().is_ident("doc")
1711
}
1812

1913
/// Run `rustfmt` on the `input` string and return the formatted code.

0 commit comments

Comments
 (0)