Skip to content

Commit

Permalink
Merge pull request #698 from nicholasbishop/bishop-upgrade-xtask-syn
Browse files Browse the repository at this point in the history
xtask: Upgrade to syn 2.0
  • Loading branch information
phip1611 authored Mar 19, 2023
2 parents 39cba0b + d9d8730 commit 4728db0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 103 deletions.
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ proc-macro2 = "1.0.46"
quote = "1.0.21"
regex = "1.5.4"
serde_json = "1.0.73"
syn = { version = "1.0.101", features = ["full"] }
syn = { version = "2.0.0", features = ["full"] }
tempfile = "3.2.0"
81 changes: 32 additions & 49 deletions xtask/src/device_path/field.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::device_path::util::is_doc_attr;
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{
Attribute, Expr, ExprLit, Field, Ident, Lit, Meta, MetaList, MetaNameValue, NestedMeta, Path,
Type, TypeArray,
};
use syn::{Attribute, Expr, ExprLit, Field, Ident, Lit, Path, Type, TypeArray};

/// A fixed-size non-array type.
///
Expand Down Expand Up @@ -344,58 +341,44 @@ impl FieldNodeAttr {
/// readme. Returns `None` if the attribute does not exactly match
/// the expected format.
fn from_attr(attr: &Attribute) -> Option<Self> {
let meta = attr.parse_meta().ok()?;

if let Meta::List(MetaList { path, nested, .. }) = meta {
if path.get_ident()? != "node" {
return None;
}
if !attr.path().is_ident("node") {
return None;
}

let mut out = Self::default();

for nested in nested.iter() {
match nested {
NestedMeta::Meta(Meta::Path(path)) => {
let ident = path.get_ident()?;
if ident == "no_get_func" {
out.get_func = GetFunc::None;
} else if ident == "custom_get_impl" {
out.get_func = GetFunc::Custom;
} else if ident == "custom_build_impl" {
out.custom_build_impl = true;
} else if ident == "custom_build_size_impl" {
out.custom_build_size_impl = true;
} else {
return None;
}
let mut out = Self::default();

attr.parse_nested_meta(|meta| {
let path = &meta.path;
if path.is_ident("no_get_func") {
out.get_func = GetFunc::None;
} else if path.is_ident("custom_get_impl") {
out.get_func = GetFunc::Custom;
} else if path.is_ident("custom_build_impl") {
out.custom_build_impl = true;
} else if path.is_ident("custom_build_size_impl") {
out.custom_build_size_impl = true;
} else if path.is_ident("build_type") {
let value = meta.value()?;
let lit: Lit = value.parse()?;

match lit {
Lit::Str(s) => {
out.build_type = BuildType::Custom(syn::parse_str(&s.value())?);
}
NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit, .. })) => {
if path.get_ident()? != "build_type" {
return None;
}

match lit {
Lit::Str(s) => {
out.build_type =
BuildType::Custom(syn::parse_str(&s.value()).ok()?);
}
Lit::Bool(b) if !b.value() => {
out.build_type = BuildType::None;
}
_ => {
return None;
}
}
Lit::Bool(b) if !b.value() => {
out.build_type = BuildType::None;
}
_ => {
return None;
return Err(meta.error("invalid build_type"));
}
}
} else {
return Err(meta.error("invalid field node attribute"));
}
Ok(())
})
.ok()?;

Some(out)
} else {
None
}
Some(out)
}
}
11 changes: 2 additions & 9 deletions xtask/src/device_path/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::node::{is_node_attr, Node};
use heck::ToUpperCamelCase;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{Attribute, Ident, Item, ItemMod, ItemStruct, Meta};
use syn::{Attribute, Ident, Item, ItemMod, ItemStruct};

#[derive(Clone)]
pub struct DeviceType(Ident);
Expand Down Expand Up @@ -170,14 +170,7 @@ impl NodeGroup {
}

fn is_build_attr(attr: &Attribute) -> bool {
if let Ok(Meta::Path(path)) = attr.parse_meta() {
if let Some(ident) = path.get_ident() {
if ident == "build" {
return true;
}
}
}
false
attr.path().is_ident("build")
}

fn has_build_attr(item: &Item) -> bool {
Expand Down
62 changes: 26 additions & 36 deletions xtask/src/device_path/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use heck::ToShoutySnakeCase;
use proc_macro2::Span;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Attribute, Fields, Ident, ItemStruct, Lit, Meta, MetaList, MetaNameValue, NestedMeta};
use syn::{Attribute, Fields, Ident, ItemStruct, LitInt, LitStr};

/// Device path node specification.
pub struct Node {
Expand Down Expand Up @@ -467,44 +467,34 @@ struct NodeAttr {
/// Parse a `node` attribute. Returns `None` for any other attribute, or
/// if the contents don't match the expected format.
fn parse_node_attr(attr: &Attribute) -> Option<NodeAttr> {
let meta = attr.parse_meta().ok()?;

if let Meta::List(MetaList { path, nested, .. }) = meta {
if path.get_ident()? != "node" {
return None;
}

let mut static_size = None;
let mut sub_type = None;

for nested in nested.iter() {
if let NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit, .. })) = nested {
let ident = path.get_ident()?;
if !attr.path().is_ident("node") {
return None;
}

match lit {
Lit::Int(lit) if ident == "static_size" => {
let lit = lit.base10_parse().ok()?;
static_size = Some(lit);
}
Lit::Str(lit) if ident == "sub_type" => {
sub_type = Some(lit.value());
}
_ => {
return None;
}
}
} else {
return None;
}
let mut static_size = None;
let mut sub_type = None;
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("static_size") {
let value = meta.value()?;
let lit: LitInt = value.parse()?;
let lit = lit.base10_parse()?;
static_size = Some(lit);
Ok(())
} else if meta.path.is_ident("sub_type") {
let value = meta.value()?;
let lit: LitStr = value.parse()?;
sub_type = Some(lit.value());
Ok(())
} else {
Err(meta.error("invalid struct node attribute"))
}
})
.ok()?;

Some(NodeAttr {
static_size: static_size?,
sub_type,
})
} else {
None
}
Some(NodeAttr {
static_size: static_size?,
sub_type,
})
}

/// Returns `true` if the attribute is a valid `node` attribute, false
Expand Down
10 changes: 2 additions & 8 deletions xtask/src/device_path/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ use anyhow::{bail, Context, Result};
use std::io::Write;
use std::process::{Command, Stdio};
use std::thread;
use syn::{Attribute, Meta};
use syn::Attribute;

/// Returns true if the attribute is a `#[doc = "..."]` attribute,
/// otherwise returns false.
pub fn is_doc_attr(attr: &Attribute) -> bool {
if let Ok(Meta::NameValue(nv)) = attr.parse_meta() {
if let Some(ident) = nv.path.get_ident() {
return ident == "doc";
}
}

false
attr.path().is_ident("doc")
}

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

0 comments on commit 4728db0

Please sign in to comment.