Skip to content

Commit

Permalink
Merge pull request #18 from daxpedda/syn-v2
Browse files Browse the repository at this point in the history
Update `syn` to v2
  • Loading branch information
zesterer committed Mar 28, 2023
2 parents 04e2d60 + 8ef0686 commit 55cc36c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async fn main() {

Additionally if you have re-exported the crate with a different name then `pollster`, you have to specify it:
```rust,ignore
#[pollster::main(crate = "renamed-pollster")]
#[pollster::main(crate = renamed_pollster)]
async fn main() {
let my_fut = async {};
Expand Down
2 changes: 1 addition & 1 deletion macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1"
quote = { version = "1", default-features = false }
syn = { version = "1", default-features = false, features = [
syn = { version = "2", default-features = false, features = [
"full",
"parsing",
"printing",
Expand Down
77 changes: 29 additions & 48 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::iter::FromIterator;
use std::str::FromStr;

use proc_macro2::TokenStream;
use quote::{quote_spanned, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::{AttributeArgs, Error, ItemFn, Lit, Meta, MetaNameValue, NestedMeta, Result};
use quote::ToTokens;
use syn::spanned::Spanned;
use syn::{Error, Expr, ExprLit, ExprPath, ItemFn, Lit, MetaNameValue, Result};

/// Uses [`pollster::block_on`] to enable `async fn main() {}`.
///
Expand Down Expand Up @@ -69,70 +69,51 @@ fn test_internal(attr: TokenStream, item: TokenStream) -> Result<ItemFn> {

fn common(attr: TokenStream, item: TokenStream) -> Result<ItemFn> {
let mut item: ItemFn = syn::parse2(item)?;
let span = item.block.brace_token.span;

if item.sig.asyncness.is_some() {
item.sig.asyncness = None;
} else {
return Err(Error::new_spanned(item, "expected function to be async"));
}

let attr: Attr = syn::parse2(attr)?;
let mut crate_name = None;

for meta in attr.0 {
if let NestedMeta::Meta(Meta::NameValue(MetaNameValue {
path,
lit: Lit::Str(name),
..
})) = meta
{
if path.is_ident("crate") {
let span = name.span();
let name = TokenStream::from_str(&name.value())?;

if crate_name
.replace(quote_spanned! { span => #name})
.is_some()
{
return Err(Error::new(span, "found duplicate \"crate\" attribute"));
let path = if attr.is_empty() {
quote::quote! { ::pollster }
} else {
let attr: MetaNameValue = syn::parse2(attr)?;

if attr.path.is_ident("crate") {
match attr.value {
Expr::Lit(ExprLit {
attrs,
lit: Lit::Str(str),
}) if attrs.is_empty() => TokenStream::from_str(&str.value())?,
Expr::Path(ExprPath {
attrs,
qself: None,
path,
}) if attrs.is_empty() => path.to_token_stream(),
_ => {
return Err(Error::new_spanned(
attr.value,
"expected valid path, e.g. `::package_name`",
))
}

continue;
}
} else {
return Err(Error::new_spanned(attr.path, "expected `crate`"));
}
};

return Err(Error::new_spanned(
item,
"expected valid attribute, e.g. `main(crate = \"package-name\")`",
));
}

let crate_name = crate_name.unwrap_or_else(|| quote::quote! { ::pollster });

let span = item.span();
let block = item.block;
item.block = syn::parse_quote_spanned! {
span =>
{
#crate_name::block_on(async {
#path::block_on(async {
#block
})
}
};

Ok(item)
}

struct Attr(AttributeArgs);

impl Parse for Attr {
fn parse(input: ParseStream) -> Result<Self> {
let mut attr = Vec::new();

while let Ok(meta) = input.parse() {
attr.push(meta)
}

Ok(Self(attr))
}
}
10 changes: 8 additions & 2 deletions macro/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ fn result() {
main_result().unwrap();
}

#[pollster::main(crate = reexported_pollster)]
async fn main_crate_path() {
ready(42).await;
}

#[pollster::main(crate = "reexported_pollster")]
async fn main_crate() {
async fn main_crate_str() {
ready(42).await;
}

#[test]
fn crate_() {
main_crate();
main_crate_path();
main_crate_str();
}
7 changes: 6 additions & 1 deletion macro/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ async fn result() -> Result<(), std::io::Error> {
}
}

#[pollster::test(crate = reexported_pollster)]
async fn crate_path() {
ready(42).await;
}

#[pollster::test(crate = "reexported_pollster")]
async fn crate_() {
async fn crate_str() {
ready(42).await;
}

0 comments on commit 55cc36c

Please sign in to comment.