Skip to content

Commit ea408f7

Browse files
committed
Avoid parsing function bodies for a better rust-analyzer experience.
1 parent 15cafb3 commit ea408f7

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlxmq"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
authors = ["Diggory Blake <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
@@ -23,7 +23,7 @@ uuid = { version = "0.8.2", features = ["v4"] }
2323
log = "0.4.14"
2424
serde_json = "1.0.64"
2525
serde = "1.0.124"
26-
sqlxmq_macros = { version = "0.3.2", path = "sqlxmq_macros" }
26+
sqlxmq_macros = { version = "0.3.3", path = "sqlxmq_macros" }
2727
anymap2 = "0.13.0"
2828

2929
[features]

sqlxmq_macros/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlxmq_macros"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
authors = ["Diggory Blake <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
@@ -14,5 +14,6 @@ documentation = "https://docs.rs/sqlxmq"
1414
proc-macro = true
1515

1616
[dependencies]
17-
syn = { version = "1.0.64", features = ["full"] }
18-
quote = "1.0.9"
17+
syn = { version = "1.0.80", features = ["full"] }
18+
quote = "1.0.10"
19+
proc-macro2 = "1.0.30"

sqlxmq_macros/src/lib.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use std::mem;
77

88
use proc_macro::TokenStream;
9-
use quote::quote;
9+
use proc_macro2::TokenStream as TokenStream2;
10+
use quote::{quote, ToTokens, TokenStreamExt};
1011
use syn::{
11-
parse_macro_input, parse_quote, AttributeArgs, Error, ItemFn, Lit, Meta, NestedMeta, Path,
12-
Result, Visibility,
12+
parse::{Parse, ParseStream},
13+
parse_macro_input, parse_quote, AttrStyle, Attribute, AttributeArgs, Error, Lit, Meta,
14+
NestedMeta, Path, Result, Signature, Visibility,
1315
};
1416

1517
#[derive(Default)]
@@ -90,6 +92,44 @@ fn interpret_job_arg(options: &mut JobOptions, arg: NestedMeta) -> Result<()> {
9092
Ok(())
9193
}
9294

95+
#[derive(Clone)]
96+
struct MaybeItemFn {
97+
attrs: Vec<Attribute>,
98+
vis: Visibility,
99+
sig: Signature,
100+
block: TokenStream2,
101+
}
102+
103+
/// This parses a `TokenStream` into a `MaybeItemFn`
104+
/// (just like `ItemFn`, but skips parsing the body).
105+
impl Parse for MaybeItemFn {
106+
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
107+
let attrs = input.call(syn::Attribute::parse_outer)?;
108+
let vis: Visibility = input.parse()?;
109+
let sig: Signature = input.parse()?;
110+
let block: TokenStream2 = input.parse()?;
111+
Ok(Self {
112+
attrs,
113+
vis,
114+
sig,
115+
block,
116+
})
117+
}
118+
}
119+
120+
impl ToTokens for MaybeItemFn {
121+
fn to_tokens(&self, tokens: &mut TokenStream2) {
122+
tokens.append_all(
123+
self.attrs
124+
.iter()
125+
.filter(|attr| matches!(attr.style, AttrStyle::Outer)),
126+
);
127+
self.vis.to_tokens(tokens);
128+
self.sig.to_tokens(tokens);
129+
self.block.to_tokens(tokens);
130+
}
131+
}
132+
93133
/// Marks a function as being a background job.
94134
///
95135
/// The first argument to the function must have type `CurrentJob`.
@@ -181,7 +221,7 @@ fn interpret_job_arg(options: &mut JobOptions, arg: NestedMeta) -> Result<()> {
181221
#[proc_macro_attribute]
182222
pub fn job(attr: TokenStream, item: TokenStream) -> TokenStream {
183223
let args = parse_macro_input!(attr as AttributeArgs);
184-
let mut inner_fn = parse_macro_input!(item as ItemFn);
224+
let mut inner_fn = parse_macro_input!(item as MaybeItemFn);
185225

186226
let mut options = JobOptions::default();
187227
let mut errors = Vec::new();

0 commit comments

Comments
 (0)