@@ -12,79 +12,195 @@ use syn::parse_macro_input;
1212
1313/// Generates conditional permutations of a function based on a supplied set of parameters.
1414///
15- /// For example:
16- ///
17- /// ```
15+ /// ```rust
1816/// # use permutate_macro::permutate;
1917/// #[permutate(
20- /// parameters = {
21- /// a: on | off,
22- /// b: on | off,
23- /// c: on | off
24- /// },
25- /// permutations = [
26- /// (on, on, on),
27- /// (off, on, off),
28- /// (off, off, off)
29- /// ]
18+ /// parameters = {
19+ /// a: on | off,
20+ /// b: on | off,
21+ /// c: on | off
22+ /// },
23+ /// constants = {},
24+ /// permutations = [
25+ /// {
26+ /// parameters = [
27+ /// on,
28+ /// on,
29+ /// on
30+ /// ],
31+ /// constants = {}
32+ /// },
33+ /// {
34+ /// parameters = [
35+ /// off,
36+ /// on,
37+ /// off
38+ /// ],
39+ /// constants = {}
40+ /// },
41+ /// {
42+ /// parameters = [
43+ /// off,
44+ /// off,
45+ /// off
46+ /// ],
47+ /// constants = {}
48+ /// }
49+ /// ]
3050/// )]
3151/// fn foo() {
32- /// #[permutate(a = on)]
33- /// println!("A");
34- ///
35- /// #[permutate(b = on)]
36- /// println!("B");
37- ///
38- /// #[permutate(c = on)]
39- /// println!("C");
52+ /// #[permutate(a = on)]
53+ /// println!("A");
54+ ///
55+ /// #[permutate(b = on)]
56+ /// println!("B");
57+ ///
58+ /// #[permutate(c = on)]
59+ /// println!("C");
4060/// }
4161/// ```
42- ///
43- /// Would expand to...
44- ///
45- /// ```
62+ ///
63+ /// Would expand to:
64+ ///
65+ /// ```rust
4666/// fn foo__on__on__on() {
4767/// println!("A");
4868/// println!("B");
4969/// println!("C");
5070/// }
51- ///
71+ ///
5272/// fn foo__off__on__off() {
5373/// println!("B");
5474/// }
75+ ///
76+ /// fn foo__off__off__off() {}
77+ /// ```
78+ ///
79+ /// Key-value constants can also be specified, and injected in place of generic parameters:
80+ ///
81+ /// ```rust
82+ /// # use permutate_macro::permutate;
5583///
56- /// fn foo__off__off__off() {
84+ /// fn const_print<const VAL: u32>() {
85+ /// println!("{VAL:}");
86+ /// }
87+ ///
88+ /// #[permutate(
89+ /// parameters = {
90+ /// a: on | off,
91+ /// b: on | off
92+ /// },
93+ /// constants = {
94+ /// CONST_INT: u32,
95+ /// },
96+ /// permutations = [
97+ /// {
98+ /// parameters = [
99+ /// on,
100+ /// off
101+ /// ],
102+ /// constants = {
103+ /// CONST_INT = 4
104+ /// }
105+ /// },
106+ /// {
107+ /// parameters = [
108+ /// off,
109+ /// on
110+ /// ],
111+ /// constants = {
112+ /// CONST_INT = 8
113+ /// }
114+ /// }
115+ /// ]
116+ /// )]
117+ /// fn bar() {
118+ /// #[permutate(a = on)]
119+ /// {
120+ /// println!("A:");
121+ /// const_print::<permutate!(CONST_INT)>();
122+ /// }
123+ ///
124+ /// #[permutate(b = on)]
125+ /// {
126+ /// println!("B:");
127+ /// const_print::<permutate!(CONST_INT)>();
128+ /// }
57129/// }
58130/// ```
59131///
60- /// In addition, permutations can be specified by file path,
61- /// or an environment variable containing a file path:
132+ /// Would expand to:
133+ ///
134+ /// ```rust
135+ /// # fn const_print<const VAL: u32>() {
136+ /// # println!("{VAL:}");
137+ /// # }
138+ ///
139+ /// fn bar__on__off__CONST_INT_4() {
140+ /// println!("A:");
141+ /// const_print::<4>();
142+ /// }
143+ ///
144+ /// fn bar__off__on__CONST_INT_8() {
145+ /// println!("B:");
146+ /// const_print::<8>();
147+ /// }
62148/// ```
149+ ///
150+ /// In addition, permutations can be specified by file path, or an environment variable containing a file path:
151+ ///
152+ /// ```rust
63153/// # use permutate_macro::permutate;
64154/// #[permutate(
65155/// parameters = {
66- /// foo: on | off,
67- /// bar: on | off
68- /// },
69- /// permutations = [
70- /// file("permutations.json", "path::to::this::module"),
71- /// env("SOME_ENV_VAR", "path::to::this::module")
72- /// ]
156+ /// foo: on | off,
157+ /// bar: on | off
158+ /// },
159+ /// constants = {},
160+ /// permutations = [
161+ /// file("permutations.json", "path::to::this::module"),
162+ /// env("SOME_ENV_VAR", "path::to::this::module")
163+ /// ]
73164/// )]
74165/// fn func() {}
75166/// ```
76- ///
167+ ///
77168/// The expected format of this file is as follows:
78- ///
169+ ///
79170/// ```json
80171/// {
81- /// "path::to::this::module::func": [
82- /// ["on", "on"],
83- /// ["off", "on"],
84- /// ["off", "off"],
85- /// ]
172+ /// "path::to::this::module::func": [
173+ /// {
174+ /// "parameters": [
175+ /// "on",
176+ /// "on",
177+ /// ],
178+ /// "constants": {
179+ /// "CONST_INT": 4,
180+ /// }
181+ /// },
182+ /// {
183+ /// "parameters": [
184+ /// "off",
185+ /// "on",
186+ /// ],
187+ /// "constants": {
188+ /// "CONST_INT": 6,
189+ /// }
190+ /// },
191+ /// {
192+ /// "parameters": [
193+ /// "off",
194+ /// "off",
195+ /// ],
196+ /// "constants": {
197+ /// "CONST_INT": 8,
198+ /// }
199+ /// }
200+ /// ],
86201/// }
87202/// ```
203+
88204#[ proc_macro_attribute]
89205pub fn permutate ( attr : TokenStream , item : TokenStream ) -> TokenStream {
90206 permutate:: macro_impl ( parse_macro_input ! ( attr) , parse_macro_input ! ( item) )
0 commit comments