-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
411 lines (405 loc) · 15.5 KB
/
lib.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
use crate::ast::{
LitIntegerOrExprs, NegativeLitIntegerOrExprs, PositiveLitIntegerOrExprs,
UnsignedLitIntegerOrExprs,
};
use crate::macros::debug_eprintln;
use proc_macro::{self, TokenStream};
use tnconst_impl::{
nconst_impl_lit_integer, nconst_impl_math_exprs, pconst_impl_lit_integer,
pconst_impl_math_exprs, tnconst_impl_lit_integer, tnconst_impl_math_exprs,
uconst_impl_lit_integer, uconst_impl_math_exprs,
};
mod ast;
mod ast_macro;
mod exprs_impl;
mod macros;
mod tnconst_impl;
mod uconst_impl;
/// [`uconst!`] is a procedural macro that converts a literal integer or an expression into a [`typenum`]'s type-level unsigned integer (i.e. the type that implements the [`typenum::Unsigned`] trait).
///
/// There are three ways you can invoke this macro.
///
/// ## 1. Invoke it with a literal integer
///
/// ```rust
/// use typenum::{U123, assert_type_eq};
/// use typenum_consts::uconst;
///
/// type A = uconst![123];
/// assert_type_eq!(A, U123);
/// ```
///
/// Compilation fails if the literal integer is prefixed with either a `-` or a `+`.
///
/// ```compile_fail
/// # use typenum::{U123, assert_type_eq};
/// # use typenum_consts::uconst;
/// type B = uconst![+123]; // Fail to compile
/// ```
///
/// ```compile_fail
/// # use typenum::{U123, assert_type_eq};
/// # use typenum_consts::uconst;
/// type C = uconst![-123]; // Fail to compile
/// ```
///
/// ## 2. Invoke using an expression or many simple mathematical expressions
///
/// ```rust
/// use typenum::{U15, assert_type_eq};
/// use typenum_consts::uconst;
/// type D = uconst![{
/// a = 10;
/// b = 5;
/// a + b; // Last statement is always the final returned value to be casted into `typenum` type-level integer, U15
/// }];
/// assert_type_eq!(D, U15);
/// ```
///
/// It is a compilation error if the mathematical expressions evaluate to a negative literal integer.
///
/// ```compile_fail
/// use typenum::{U15, assert_type_eq};
/// use typenum_consts::uconst;
/// type D = uconst![{
/// a = 10;
/// b = 5;
/// b - a; // 5 - 10 = -5, cannot be made into a `U15`
/// }];
/// assert_type_eq!(D, U15);
/// ```
///
/// ## 3. Invoke by reading from an environment variable
///
/// Note: `env!(...)` is a macro-like invocation. The first parameter is mandatory and is the key of the environment variable that `uconst` will read. The second parameter is optional and is the file path of the `.env.*` file to read the environment variable from, e.g. `env!("ENV_VAR", "./.env.prod")`, `"ENV_VAR"` is the key to read the value from and `"./.env.prod"` is the file path relative to [`CARGO_MANIFEST_DIR`].
///
/// ```rust
/// use typenum::{U69, assert_type_eq};
/// use typenum_consts::uconst;
/// // ``` .env
/// // ENV_VAR=69
/// // ```
/// type E = uconst![env!("ENV_VAR");];
/// assert_type_eq!(E, U69);
/// ```
///
/// It is a compilation error if the environment variable evaluate to a negative literal integer.
///
/// ```compile_fail
/// use typenum::{U69, assert_type_eq};
/// use typenum_consts::uconst;
/// // ``` .env
/// // NENV_VAR=-69
/// // ```
/// type F = uconst![env!("NENV_VAR");];
/// assert_type_eq!(F, U69);
/// ```
///
/// [`CARGO_MANIFEST_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
/// [`typenum::Unsigned`]: https://docs.rs/typenum/latest/typenum/marker_traits/trait.Unsigned.html
/// [`typenum`]: https://github.com/paholg/typenum/tree/main/src
#[proc_macro]
pub fn uconst(items: TokenStream) -> TokenStream {
match syn::parse::<UnsignedLitIntegerOrExprs>(items) {
Ok(litint_exprs) => match litint_exprs.0 {
LitIntegerOrExprs::Exprs(math_exprs) => uconst_impl_math_exprs(math_exprs)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
LitIntegerOrExprs::LitInteger(lit_integer) => uconst_impl_lit_integer(lit_integer)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
},
Err(err) => err.into_compile_error().into(),
}
}
/// [`tnconst!`] is a procedural macro that converts a literal integer or an expression into a [`typenum`]'s type-level unsigned/positive/negative (depending on what is the prefix-sign) integer (i.e. the type that implements the [`typenum::Unsigned`]/[`typenum::Integer`] trait).
///
/// Because [`tnconst!`] can be evaluated into [`typenum::UInt`], [`typenum::PInt`] and [`typenum::NInt`], to disambiguate them, one is required to invoke the macro with either `+`, `-` to get [`tnconst!`] to evaluate the macro input as [`typenum::PInt`] or [`typenum::NInt`], respectively. [`typenum::UInt`] is the default and does not require any sign to be prefixed.
///
/// Examples:
///
/// If you invoke e.g. `tnconst![123]`, i.e. there is no sign prefixing the literal integer `123`, then this will be evaluated as a [`typenum::UInt`], specifically, [`typenum::U123`].
///
/// If you invoke e.g. `tnconst![-123]`, i.e. there is a negative sign prefixing the literal integer `123`, then this will be evaluated as a [`typenum::NInt`], specifically, [`typenum::N123`].
///
/// There are three ways you can invoke this macro.
///
/// ## 1. Invoke it with a literal integer
///
/// ```rust
/// use typenum::{N123, assert_type_eq};
/// use typenum_consts::tnconst;
///
/// type A = tnconst![-123];
/// assert_type_eq!(A, N123);
/// ```
///
/// ## 2. Invoke using an expression or many simple mathematical expressions
///
/// ```rust
/// use typenum::{P15, assert_type_eq};
/// use typenum_consts::tnconst;
/// type D = tnconst![+ {
/// a = 10;
/// b = 5;
/// a + b; // Last statement is always the final returned value to be casted into `typenum` type-level integer, P15
/// }];
/// assert_type_eq!(D, P15);
/// ```
///
/// It is a compilation error if the mathematical expressions evaluate to a negative (positive) literal integer and you had specify a `+` (`-`) prefix to ask [`tnconst!`] to evaluate the input as a [`typenum::PInt`] ([`typenum::NInt`]).
///
/// ```compile_fail
/// use typenum::{P15, assert_type_eq};
/// use typenum_consts::tnconst;
/// type D = tnconst![+ {
/// a = 10;
/// b = 5;
/// b - a; // 5 - 10 = -5, cannot be made into a `P15`
/// }];
/// assert_type_eq!(D, P15);
/// ```
///
/// ## 3. Invoke by reading from an environment variable
///
/// Note: `env!(...)` is a macro-like invocation. The first parameter is mandatory and is the key of the environment variable that `tnconst` will read. The second parameter is optional and is the file path of the `.env.*` file to read the environment variable from, e.g. `env!("ENV_VAR", "./.env.prod")`, `"ENV_VAR"` is the key to read the value from and `"./.env.prod"` is the file path relative to [`CARGO_MANIFEST_DIR`].
///
/// ```rust
/// use typenum::{U69, assert_type_eq};
/// use typenum_consts::tnconst;
/// // ``` .env
/// // ENV_VAR=69
/// // ```
/// type E = tnconst![env!("ENV_VAR");];
/// assert_type_eq!(E, U69);
/// ```
///
/// It is a compilation error if the environment variable evaluate to a negative (positive) literal integer and you had specify a `+` (`-`) prefix to ask [`tnconst!`] to evaluate the input as a [`typenum::PInt`] ([`typenum::NInt`]).
///
/// ```compile_fail
/// use typenum::{P69, assert_type_eq};
/// use typenum_consts::tnconst;
/// // ``` .env
/// // NENV_VAR=-69
/// // ```
/// type F = tnconst![+ env!("NENV_VAR");];
/// assert_type_eq!(F, P69);
/// ```
///
/// [`CARGO_MANIFEST_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
/// [`typenum::Unsigned`]: https://docs.rs/typenum/latest/typenum/marker_traits/trait.Unsigned.html
/// [`typenum::Integer`]: https://docs.rs/typenum/latest/typenum/marker_traits/trait.Integer.html
/// [`typenum::PInt`]: https://docs.rs/typenum/latest/typenum/int/struct.PInt.html
/// [`typenum::UInt`]: https://docs.rs/typenum/latest/typenum/uint/struct.UInt.html
/// [`typenum::NInt`]: https://docs.rs/typenum/latest/typenum/int/struct.NInt.html
/// [`typenum`]: https://github.com/paholg/typenum/tree/main/src
#[proc_macro]
pub fn tnconst(items: TokenStream) -> TokenStream {
match syn::parse::<LitIntegerOrExprs>(items) {
Ok(litint_exprs) => match litint_exprs {
LitIntegerOrExprs::Exprs(math_exprs) => tnconst_impl_math_exprs(math_exprs)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
LitIntegerOrExprs::LitInteger(lit_integer) => tnconst_impl_lit_integer(lit_integer)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
},
Err(err) => err.into_compile_error().into(),
}
}
/// [`pconst!`] is a procedural macro that converts a literal integer or an expression into a [`typenum`]'s type-level positive integer (i.e. the type that implements the [`typenum::Integer`] trait).
///
/// There are three ways you can invoke this macro.
///
/// ## 1. Invoke it with a literal integer
///
/// ```rust
/// use typenum::{P123, assert_type_eq};
/// use typenum_consts::pconst;
///
/// type A = pconst![123];
/// assert_type_eq!(A, P123);
/// ```
///
/// Compilation fails if the literal integer is prefixed with either a `-` or a `+`.
///
/// ```compile_fail
/// # use typenum::{P123, assert_type_eq};
/// # use typenum_consts::pconst;
/// type B = pconst![+123]; // Fail to compile
/// ```
///
/// ```compile_fail
/// # use typenum::{P123, assert_type_eq};
/// # use typenum_consts::pconst;
/// type C = pconst![-123]; // Fail to compile
/// ```
///
/// ## 2. Invoke using an expression or many simple mathematical expressions
///
/// ```rust
/// use typenum::{P15, assert_type_eq};
/// use typenum_consts::pconst;
/// type D = pconst![{
/// a = 10;
/// b = 5;
/// a + b; // Last statement is always the final returned value to be casted into `typenum` type-level integer, P15
/// }];
/// assert_type_eq!(D, P15);
/// ```
///
/// It is a compilation error if the mathematical expressions evaluate to a negative literal integer.
///
/// ```compile_fail
/// use typenum::{P15, assert_type_eq};
/// use typenum_consts::pconst;
/// type D = pconst![{
/// a = 10;
/// b = 5;
/// b - a; // 5 - 10 = -5, cannot be made into a `PInt<U15>`
/// }];
/// assert_type_eq!(D, P15);
/// ```
///
/// ## 3. Invoke by reading from an environment variable
///
/// Note: `env!(...)` is a macro-like invocation. The first parameter is mandatory and is the key of the environment variable that `pconst` will read. The second parameter is optional and is the file path of the `.env.*` file to read the environment variable from, e.g. `env!("ENV_VAR", "./.env.prod")`, `"ENV_VAR"` is the key to read the value from and `"./.env.prod"` is the file path relative to [`CARGO_MANIFEST_DIR`].
///
/// ```rust
/// use typenum::{P69, assert_type_eq};
/// use typenum_consts::pconst;
/// // ``` .env
/// // ENV_VAR=69
/// // ```
/// type E = pconst![env!("ENV_VAR");];
/// assert_type_eq!(E, P69);
/// ```
///
/// It is a compilation error if the environment variable evaluate to a negative literal integer.
///
/// ```compile_fail
/// use typenum::{P69, assert_type_eq};
/// use typenum_consts::pconst;
/// // ``` .env
/// // NENV_VAR=-69
/// // ```
/// type F = pconst![env!("NENV_VAR");];
/// assert_type_eq!(F, P69);
/// ```
///
/// [`CARGO_MANIFEST_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
/// [`typenum::Integer`]: https://docs.rs/typenum/latest/typenum/marker_traits/trait.Integer.html
/// [`typenum`]: https://github.com/paholg/typenum/tree/main/src
#[proc_macro]
pub fn pconst(items: TokenStream) -> TokenStream {
match syn::parse::<PositiveLitIntegerOrExprs>(items) {
Ok(litint_exprs) => match litint_exprs.0 {
LitIntegerOrExprs::Exprs(math_exprs) => pconst_impl_math_exprs(math_exprs)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
LitIntegerOrExprs::LitInteger(lit_integer) => pconst_impl_lit_integer(lit_integer)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
},
Err(err) => err.into_compile_error().into(),
}
}
/// [`nconst!`] is a procedural macro that converts a literal integer or an expression into a [`typenum`]'s type-level negative integer (i.e. the type that implements the [`typenum::Integer`] trait).
///
/// There are three ways you can invoke this macro.
///
/// ## 1. Invoke it with a literal integer
///
/// ```rust
/// use typenum::{N123, assert_type_eq};
/// use typenum_consts::nconst;
///
/// type A = nconst![123];
/// assert_type_eq!(A, N123);
/// ```
///
/// Compilation fails if the literal integer is prefixed with either a `-` or a `+`.
///
/// ```compile_fail
/// # use typenum::{N123, assert_type_eq};
/// # use typenum_consts::nconst;
/// type B = nconst![+123]; // Fail to compile
/// ```
///
/// ```compile_fail
/// # use typenum::{N123, assert_type_eq};
/// # use typenum_consts::nconst;
/// type C = nconst![-123]; // Fail to compile
/// ```
///
/// ## 2. Invoke using an expression or many simple mathematical expressions
///
/// ```rust
/// use typenum::{N15, assert_type_eq};
/// use typenum_consts::nconst;
/// type D = nconst![{
/// a = 10;
/// b = 5;
/// 0 - a - b; // Last statement is always the final returned value to be casted into `typenum` type-level integer, N15
/// }];
/// assert_type_eq!(D, N15);
/// ```
///
/// It is a compilation error if the mathematical expressions evaluate to a positive literal integer.
///
/// ```compile_fail
/// use typenum::{N15, assert_type_eq};
/// use typenum_consts::nconst;
/// type D = nconst![{
/// a = 10;
/// b = 5;
/// b + a; // 5 + 10 = 15, cannot be made into a `N15`
/// }];
/// assert_type_eq!(D, N15);
/// ```
///
/// ## 3. Invoke by reading from an environment variable
///
/// Note: `env!(...)` is a macro-like invocation. The first parameter is mandatory and is the key of the environment variable that `nconst` will read. The second parameter is optional and is the file path of the `.env.*` file to read the environment variable from, e.g. `env!("ENV_VAR", "./.env.prod")`, `"ENV_VAR"` is the key to read the value from and `"./.env.prod"` is the file path relative to [`CARGO_MANIFEST_DIR`].
///
/// ```rust
/// use typenum::{N69, assert_type_eq};
/// use typenum_consts::nconst;
/// // ``` .env
/// // NENV_VAR=-69
/// // ```
/// type E = nconst![env!("NENV_VAR");];
/// assert_type_eq!(E, N69);
/// ```
///
/// It is a compilation error if the environment variable evaluate to a positive literal integer.
///
/// ```compile_fail
/// use typenum::{N69, assert_type_eq};
/// use typenum_consts::nconst;
/// // ``` .env
/// // ENV_VAR=69
/// // ```
/// type F = nconst![env!("ENV_VAR");];
/// assert_type_eq!(F, N69);
/// ```
///
/// [`CARGO_MANIFEST_DIR`]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
/// [`typenum::Integer`]: https://docs.rs/typenum/latest/typenum/marker_traits/trait.Integer.html
/// [`typenum`]: https://github.com/paholg/typenum/tree/main/src
#[proc_macro]
pub fn nconst(items: TokenStream) -> TokenStream {
match syn::parse::<NegativeLitIntegerOrExprs>(items) {
Ok(litint_exprs) => match litint_exprs.0 {
LitIntegerOrExprs::Exprs(math_exprs) => nconst_impl_math_exprs(math_exprs)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
LitIntegerOrExprs::LitInteger(lit_integer) => nconst_impl_lit_integer(lit_integer)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
},
Err(err) => err.into_compile_error().into(),
}
}