Skip to content

Commit d48d2ed

Browse files
authored
refactor: migrate to syn 2.x (#8)
This PR updates the codebase to use the latest version of the `syn` crate. See ratatui/ratatui#961
1 parent bdddcd6 commit d48d2ed

File tree

3 files changed

+50
-61
lines changed

3 files changed

+50
-61
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition = "2018"
1313
quote = "1"
1414

1515
[dependencies.syn]
16-
version = "1"
16+
version = "2"
1717
features = ["derive", "full"]
1818

1919
[lib]

src/lib.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,20 @@ mod unstable;
9898
/// }
9999
/// ```
100100
#[proc_macro_attribute]
101-
pub fn unstable(args: TokenStream, item: TokenStream) -> TokenStream {
102-
let args = parse_macro_input!(args as syn::AttributeArgs);
103-
let attr = unstable::UnstableAttribute::from(args);
101+
pub fn unstable(args: TokenStream, input: TokenStream) -> TokenStream {
102+
let mut attributes = unstable::UnstableAttribute::default();
103+
let attributes_parser = syn::meta::parser(|meta| attributes.parse(meta));
104+
parse_macro_input!(args with attributes_parser);
104105

105-
match parse_macro_input!(item as Item) {
106-
Item::Type(item_type) => attr.expand(item_type),
107-
Item::Enum(item_enum) => attr.expand(item_enum),
108-
Item::Struct(item_struct) => attr.expand(item_struct),
109-
Item::Fn(item_fn) => attr.expand(item_fn),
110-
Item::Mod(item_mod) => attr.expand(item_mod),
111-
Item::Trait(item_trait) => attr.expand(item_trait),
112-
Item::Const(item_const) => attr.expand(item_const),
113-
Item::Static(item_static) => attr.expand(item_static),
106+
match parse_macro_input!(input as Item) {
107+
Item::Type(item_type) => attributes.expand(item_type),
108+
Item::Enum(item_enum) => attributes.expand(item_enum),
109+
Item::Struct(item_struct) => attributes.expand(item_struct),
110+
Item::Fn(item_fn) => attributes.expand(item_fn),
111+
Item::Mod(item_mod) => attributes.expand(item_mod),
112+
Item::Trait(item_trait) => attributes.expand(item_trait),
113+
Item::Const(item_const) => attributes.expand(item_const),
114+
Item::Static(item_static) => attributes.expand(item_static),
114115
_ => panic!("unsupported item type"),
115116
}
116117
}

src/unstable.rs

+36-48
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
use proc_macro::TokenStream;
22
use quote::{quote, ToTokens};
3+
use syn::meta::ParseNestedMeta;
4+
use syn::parse::Result;
35
use syn::{parse_quote, Visibility};
46

5-
#[derive(Debug)]
7+
#[derive(Debug, Default)]
68
pub(crate) struct UnstableAttribute {
79
feature: Option<String>,
8-
issue: Option<String>
10+
issue: Option<String>,
911
}
1012

1113
impl UnstableAttribute {
14+
pub(crate) fn parse(&mut self, meta: ParseNestedMeta) -> Result<()> {
15+
if meta.path.is_ident("feature") {
16+
match meta.value()?.parse()? {
17+
syn::Lit::Str(s) => self.feature = Some(s.value()),
18+
_ => panic!(),
19+
}
20+
} else if meta.path.is_ident("issue") {
21+
match meta.value()?.parse()? {
22+
syn::Lit::Str(s) => self.issue = Some(s.value()),
23+
_ => panic!(),
24+
}
25+
}
26+
Ok(())
27+
}
28+
1229
fn crate_feature_name(&self) -> String {
1330
if let Some(name) = self.feature.as_deref() {
1431
format!("unstable-{}", name)
@@ -22,9 +39,9 @@ impl UnstableAttribute {
2239
if item.is_public() {
2340
let feature_name = self.crate_feature_name();
2441

25-
if let Some(issue) = &self.issue {
26-
let doc_addendum = format!(
27-
"\n\
42+
if let Some(issue) = &self.issue {
43+
let doc_addendum = format!(
44+
"\n\
2845
# Availability\n\
2946
\n\
3047
**This API is marked as unstable** and is only available when \
@@ -33,27 +50,26 @@ impl UnstableAttribute {
3350
3451
The tracking issue is: `{}`
3552
",
36-
feature_name,
37-
issue
38-
);
39-
item.push_attr(parse_quote! {
40-
#[doc = #doc_addendum]
41-
});
42-
} else {
43-
let doc_addendum = format!(
44-
"\n\
53+
feature_name, issue
54+
);
55+
item.push_attr(parse_quote! {
56+
#[doc = #doc_addendum]
57+
});
58+
} else {
59+
let doc_addendum = format!(
60+
"\n\
4561
# Availability\n\
4662
\n\
4763
**This API is marked as unstable** and is only available when \
4864
the `{}` crate feature is enabled. This comes with no stability \
4965
guarantees, and could be changed or removed at any time.\
5066
",
51-
feature_name
52-
);
53-
item.push_attr(parse_quote! {
54-
#[doc = #doc_addendum]
55-
});
56-
}
67+
feature_name
68+
);
69+
item.push_attr(parse_quote! {
70+
#[doc = #doc_addendum]
71+
});
72+
}
5773

5874
let mut hidden_item = item.clone();
5975
*hidden_item.visibility_mut() = parse_quote! {
@@ -74,34 +90,6 @@ The tracking issue is: `{}`
7490
}
7591
}
7692

77-
impl From<syn::AttributeArgs> for UnstableAttribute {
78-
fn from(args: syn::AttributeArgs) -> Self {
79-
let mut feature = None;
80-
let mut issue = None;
81-
82-
for arg in args {
83-
match arg {
84-
syn::NestedMeta::Meta(syn::Meta::NameValue(name_value)) => {
85-
if name_value.path.is_ident("feature") {
86-
match name_value.lit {
87-
syn::Lit::Str(s) => feature = Some(s.value()),
88-
_ => panic!(),
89-
}
90-
} else if name_value.path.is_ident("issue") {
91-
match name_value.lit {
92-
syn::Lit::Str(s) => issue = Some(s.value()),
93-
_ => panic!()
94-
}
95-
}
96-
}
97-
_ => {}
98-
}
99-
}
100-
101-
Self { feature, issue }
102-
}
103-
}
104-
10593
pub(crate) trait ItemLike {
10694
fn attrs(&self) -> &[syn::Attribute];
10795

0 commit comments

Comments
 (0)