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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use syn::parse::{Parse, ParseBuffer, ParseStream};
use syn::{parenthesized, Token};
/// Syntax used for providing a value
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum ValueSyntax {
/// `= contents`
Eq,
/// `(contents)`
Paren,
}
impl ValueSyntax {
/// Returns `true` if the syntax is [`Eq`](ValueSyntax::Eq).
///
/// # Example
///
/// ```
/// # use macroific_attr_parse::ValueSyntax;
/// #
/// assert!(ValueSyntax::Eq.is_eq());
/// assert!(!ValueSyntax::Paren.is_eq());
/// ```
#[inline]
#[must_use]
pub const fn is_eq(self) -> bool {
matches!(self, Self::Eq)
}
/// Returns `true` if the syntax is [`Paren`](ValueSyntax::Paren).
///
/// # Example
///
/// ```
/// # use macroific_attr_parse::ValueSyntax;
/// #
/// assert!(ValueSyntax::Paren.is_paren());
/// assert!(!ValueSyntax::Eq.is_paren());
/// ```
#[inline]
#[must_use]
pub const fn is_paren(self) -> bool {
matches!(self, Self::Paren)
}
/// Peek the stream **without moving the cursor** and attempt to construct self based on the
/// next token.
///
/// # Example
///
/// ```
/// # use macroific_attr_parse::ValueSyntax;
/// # use syn::parse::{ParseStream, Parse};
/// # use proc_macro2::TokenStream;
/// # use quote::quote;
/// #
/// struct Foo {
/// peek: Option<ValueSyntax>,
/// rest: TokenStream,
/// }
///
/// impl Parse for Foo {
/// fn parse(input: ParseStream) -> syn::Result<Self> {
/// let peek = ValueSyntax::from_stream(input);
/// let rest = input.parse()?;
///
/// Ok(Self { peek, rest })
/// }
/// }
///
/// let v: Foo = syn::parse2(quote!(= 123)).unwrap();
/// assert_eq!(v.peek, Some(ValueSyntax::Eq));
/// assert_eq!(v.rest.to_string(), "= 123");
///
/// let v: Foo = syn::parse2(quote!((456))).unwrap();
/// assert_eq!(v.peek, Some(ValueSyntax::Paren));
/// assert_eq!(v.rest.to_string(), "(456)");
///
/// let v: Foo = syn::parse2(quote!(none)).unwrap();
/// assert_eq!(v.peek, None);
/// assert_eq!(v.rest.to_string(), "none");
/// ```
pub fn from_stream(parse: ParseStream) -> Option<Self> {
if parse.peek(Token![=]) {
Some(Self::Eq)
} else if parse.peek(syn::token::Paren) {
Some(Self::Paren)
} else {
None
}
}
/// Parse whatever tokens need to be parsed based on the resolved syntax.
/// Returns a `ParseBuffer` you should continue parsing if the syntax is
/// [`Paren`](ValueSyntax::Paren).
///
/// # Example
///
/// ```
/// # use macroific_attr_parse::ValueSyntax;
/// # use syn::parse::{ParseStream, Parse};
/// # use quote::quote;
/// #
/// /// `=` implementation
/// struct Wrapper1(syn::LitStr);
/// impl Parse for Wrapper1 {
/// fn parse(input: ParseStream) -> syn::Result<Self> {
/// let inner = ValueSyntax::Eq.parse_token(input)?;
/// assert!(inner.is_none());
/// Ok(Self(input.parse()?))
/// }
/// }
///
/// /// `(value)` implementation
/// struct Wrapper2(syn::LitStr);
/// impl Parse for Wrapper2 {
/// fn parse(input: ParseStream) -> syn::Result<Self> {
/// let inner = ValueSyntax::Paren.parse_token(input)?.expect("expected inner buffer");
/// Ok(Self(inner.parse()?))
/// }
/// }
///
/// let v: Wrapper1 = syn::parse2(quote!(= "foo")).unwrap();
/// assert_eq!(v.0.value(), "foo");
///
/// let v: Wrapper2 = syn::parse2(quote!(("bar"))).unwrap();
/// assert_eq!(v.0.value(), "bar");
/// ```
pub fn parse_token(self, input: ParseStream) -> syn::Result<Option<ParseBuffer>> {
match self {
Self::Eq => {
input.parse::<Token![=]>()?;
Ok(None)
}
Self::Paren => {
let content;
parenthesized!(content in input);
Ok(Some(content))
}
}
}
/// Parse whatever tokens need to be parsed based on the resolved syntax and
/// then parse the referenced value as `P`.
///
/// # Example
///
/// ```
/// # use macroific_attr_parse::ValueSyntax;
/// # use syn::parse::{ParseStream, Parse};
/// # use quote::quote;
/// #
/// /// `=` implementation
/// struct Wrapper1(syn::LitStr);
/// impl Parse for Wrapper1 {
/// fn parse(input: ParseStream) -> syn::Result<Self> {
/// Ok(Self(ValueSyntax::Eq.parse(input)?))
/// }
/// }
///
/// /// `(value)` implementation
/// struct Wrapper2(syn::LitStr);
/// impl Parse for Wrapper2 {
/// fn parse(input: ParseStream) -> syn::Result<Self> {
/// Ok(Self(ValueSyntax::Paren.parse(input)?))
/// }
/// }
///
/// let v: Wrapper1 = syn::parse2(quote!(= "foo")).unwrap();
/// assert_eq!(v.0.value(), "foo");
///
/// let v: Wrapper2 = syn::parse2(quote!(("bar"))).unwrap();
/// assert_eq!(v.0.value(), "bar");
/// ```
pub fn parse<P: Parse>(self, input: ParseStream) -> syn::Result<P> {
if let Some(inner) = self.parse_token(input)? {
inner.parse()
} else {
input.parse()
}
}
}