Skip to content

Commit

Permalink
Upgrade to darling 0.20 and syn v2 (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev authored Nov 18, 2023
1 parent 008e2d9 commit 7d41ead
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 47 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions macros/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/serenity-rs/poise/"
proc-macro = true

[dependencies]
syn = { version = "1.0.64", features = ["full", "fold"] }
syn = { version = "2", features = ["fold"] }
quote = "1.0.9"
proc-macro2 = "1.0.24"
darling = "0.14.1"
darling = "0.20"
6 changes: 3 additions & 3 deletions macros/src/choice_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ pub fn choice_parameter(input: syn::DeriveInput) -> Result<TokenStream, darling:
.into());
}

let attrs = variant
let attrs: Vec<_> = variant
.attrs
.into_iter()
.map(|attr| attr.parse_meta().map(syn::NestedMeta::Meta))
.collect::<Result<Vec<_>, _>>()?;
.map(|attr| darling::ast::NestedMeta::Meta(attr.meta))
.collect();
let mut attrs = <VariantAttribute as darling::FromMeta>::from_list(&attrs)?;

let main_name = if attrs.name.is_empty() {
Expand Down
20 changes: 11 additions & 9 deletions macros/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ pub struct Invocation {
fn extract_help_from_doc_comments(attrs: &[syn::Attribute]) -> (Option<String>, Option<String>) {
let mut doc_lines = String::new();
for attr in attrs {
if attr.path == quote::format_ident!("doc").into() {
for token in attr.tokens.clone() {
if let Ok(literal) = syn::parse2::<syn::LitStr>(token.into()) {
doc_lines += literal.value().trim(); // Trim lines like rustdoc does
doc_lines += "\n";
if let syn::Meta::NameValue(doc_attr) = &attr.meta {
if doc_attr.path == quote::format_ident!("doc").into() {
if let syn::Expr::Lit(lit_expr) = &doc_attr.value {
if let syn::Lit::Str(literal) = &lit_expr.lit {
doc_lines += literal.value().trim(); // Trim lines like rustdoc does
doc_lines += "\n";
}
}
}
}
Expand Down Expand Up @@ -170,11 +172,11 @@ pub fn command(
}
};

let attrs = pattern
let attrs: Vec<_> = pattern
.attrs
.drain(..)
.map(|attr| attr.parse_meta().map(syn::NestedMeta::Meta))
.collect::<Result<Vec<_>, _>>()?;
.map(|attr| darling::ast::NestedMeta::Meta(attr.meta))
.collect();
let attrs = <ParamArgs as darling::FromMeta>::from_list(&attrs)?;

let name = if let Some(rename) = &attrs.rename {
Expand Down Expand Up @@ -332,7 +334,7 @@ fn generate_command(mut inv: Invocation) -> Result<proc_macro2::TokenStream, dar
let function = &inv.function;
Ok(quote::quote! {
#[allow(clippy::str_to_string)]
#function_visibility fn #function_ident#function_generics() -> ::poise::Command<
#function_visibility fn #function_ident #function_generics() -> ::poise::Command<
<#ctx_type_with_static as poise::_GetGenerics>::U,
<#ctx_type_with_static as poise::_GetGenerics>::E,
> {
Expand Down
6 changes: 5 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ exact desugaring
*/
#[proc_macro_attribute]
pub fn command(args: TokenStream, function: TokenStream) -> TokenStream {
let args = syn::parse_macro_input!(args as Vec<syn::NestedMeta>);
let args = match darling::ast::NestedMeta::parse_meta_list(args.into()) {
Ok(x) => x,
Err(e) => return e.into_compile_error().into(),
};

let args = match <command::CommandArgs as darling::FromMeta>::from_list(&args) {
Ok(x) => x,
Err(e) => return e.write_errors().into(),
Expand Down
16 changes: 8 additions & 8 deletions macros/src/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ pub fn modal(input: syn::DeriveInput) -> Result<TokenStream, darling::Error> {
}
};

let struct_attrs = input
let struct_attrs: Vec<_> = input
.attrs
.iter()
.map(|attr| attr.parse_meta().map(syn::NestedMeta::Meta))
.collect::<Result<Vec<_>, _>>()?;
.into_iter()
.map(|attr| darling::ast::NestedMeta::Meta(attr.meta))
.collect();
let struct_attrs = <StructAttributes as darling::FromMeta>::from_list(&struct_attrs)?;

let mut builders = Vec::new();
let mut parsers = Vec::new();
for field in fields {
// Extract data from syn::Field
let field_attrs = field
let field_attrs: Vec<_> = field
.attrs
.iter()
.map(|attr| attr.parse_meta().map(syn::NestedMeta::Meta))
.collect::<Result<Vec<_>, _>>()?;
.into_iter()
.map(|attr| darling::ast::NestedMeta::Meta(attr.meta))
.collect();
let field_attrs = <FieldAttributes as darling::FromMeta>::from_list(&field_attrs)?;
let field_ident = field.ident.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions macros/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl syn::fold::Fold for AllLifetimesToStatic {
#[derive(Debug)]
pub struct List<T>(pub Vec<T>);
impl<T: darling::FromMeta> darling::FromMeta for List<T> {
fn from_list(items: &[::syn::NestedMeta]) -> darling::Result<Self> {
fn from_list(items: &[::darling::ast::NestedMeta]) -> darling::Result<Self> {
items
.iter()
.map(|item| T::from_nested_meta(item))
Expand All @@ -57,7 +57,7 @@ impl<T> Default for List<T> {
#[derive(Debug)]
pub struct Tuple2<T>(pub T, pub T);
impl<T: darling::FromMeta> darling::FromMeta for Tuple2<T> {
fn from_list(items: &[::syn::NestedMeta]) -> darling::Result<Self> {
fn from_list(items: &[::darling::ast::NestedMeta]) -> darling::Result<Self> {
Ok(match items {
[a, b] => Self(T::from_nested_meta(a)?, T::from_nested_meta(b)?),
_ => {
Expand Down

0 comments on commit 7d41ead

Please sign in to comment.