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
|
use syn::parse::{Parse, ParseStream};
use syn::{Ident, LitBool, Token};
#[derive(Debug, PartialEq, Eq)]
pub struct MacroFlag
{
pub flag: Ident,
pub is_on: LitBool,
}
impl Parse for MacroFlag
{
fn parse(input: ParseStream) -> syn::Result<Self>
{
let input_forked = input.fork();
let flag: Ident = input_forked.parse()?;
input.parse::<Ident>()?;
input.parse::<Token![=]>()?;
let is_on: LitBool = input.parse()?;
Ok(Self { flag, is_on })
}
}
#[cfg(test)]
mod tests
{
use std::error::Error;
use proc_macro2::Span;
use quote::{format_ident, quote};
use syn::parse2;
use super::*;
#[test]
fn can_parse_macro_flag() -> Result<(), Box<dyn Error>>
{
assert_eq!(
parse2::<MacroFlag>(quote! {
more = true
})?,
MacroFlag {
flag: format_ident!("more"),
is_on: LitBool::new(true, Span::call_site())
}
);
assert_eq!(
parse2::<MacroFlag>(quote! {
do_something = false
})?,
MacroFlag {
flag: format_ident!("do_something"),
is_on: LitBool::new(false, Span::call_site())
}
);
assert!(parse2::<MacroFlag>(quote! {
10 = false
})
.is_err());
Ok(())
}
}
|