/
kwargs.rs
95 lines (91 loc) · 1.9 KB
/
kwargs.rs
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
use proc_macro_kwargs::{parse::NestedList, MacroKeywordArgs};
#[derive(MacroKeywordArgs, Debug, PartialEq)]
pub struct ExampleArgs {
first: usize,
#[kwarg(rename = "foo")]
second: String,
#[kwarg(optional)]
opt: bool,
}
#[test]
fn basic_example() {
assert_eq!(
syn::parse_str::<ExampleArgs>(
r##"first => 1,
foo => "str"
"##
)
.unwrap(),
ExampleArgs {
first: 1,
second: "str".into(),
opt: false
}
);
assert_eq!(
syn::parse_str::<ExampleArgs>(
r##"first => 17,
foo => "str2",
opt => true,
"##
)
.unwrap(),
ExampleArgs {
first: 17,
second: "str2".into(),
opt: true
}
);
}
#[derive(MacroKeywordArgs, Debug, PartialEq, Default)]
struct NestedArgs {
one: bool,
#[kwarg(optional, with_wrapper = "NestedList<u32>")]
list: Vec<u32>,
#[kwarg(optional)]
nested: Option<ExampleArgs>,
}
#[test]
fn nesting() {
assert_eq!(
syn::parse_str::<NestedArgs>(
r##"one => false,
"##
)
.unwrap(),
Default::default()
);
assert_eq!(
syn::parse_str::<NestedArgs>(
r##"one => true,
list => [1, 4, 7]
"##
)
.unwrap(),
NestedArgs {
one: true,
list: vec![1, 4, 7],
nested: None
}
);
assert_eq!(
syn::parse_str::<NestedArgs>(
r##"one => true,
nested => {
first => 12,
foo => "nested-str",
}
"##
)
.unwrap(),
NestedArgs {
one: true,
list: Default::default(),
nested: Some(ExampleArgs {
first: 12,
second: "nested-str".into(),
opt: false
})
}
);
}