1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::token::Async;
use syn::{Error, Result, Token, Visibility};
pub(super) struct Attrs {
pub(super) visibility: Option<Visibility>,
pub(super) asyncness: Option<Async>,
}
pub(super) struct SyncOnlyAttrs {
pub(super) visibility: Option<Visibility>,
}
impl Parse for SyncOnlyAttrs {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let Attrs {
visibility,
asyncness,
} = input.parse()?;
if let Some(async_token) = asyncness {
Err(syn::Error::new(
async_token.span,
"this macro doesn't support async traits",
))
} else {
Ok(Self { visibility })
}
}
}
impl Parse for Attrs {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let mut attrs = Attrs {
visibility: None,
asyncness: None,
};
while !input.is_empty() {
if input.peek(Async) {
let new_asyncness: Async = input.parse()?;
if attrs.asyncness.is_some() {
return Err(syn::Error::new(
new_asyncness.span,
"cannot specify asyncness twice",
));
} else {
attrs.asyncness = Some(new_asyncness);
}
} else {
let new_vis: Visibility = input.parse()?;
if attrs.visibility.is_some() {
return Err(syn::Error::new(
new_vis.span(),
"cannot specify visibility twice",
));
} else {
attrs.visibility = Some(new_vis);
}
}
if input.peek(Token![,]) {
let comma: Token![,] = input.parse()?;
if input.is_empty() {
return Err(Error::new(comma.span, "expected visibility or `async`"));
}
}
}
Ok(attrs)
}
}