xmacro/lib.rs
1#![doc = include_str!("../README.md")]
2use proc_macro::TokenStream;
3use xmacro_lib::{xmacro_expand, xmacro_expand_items};
4
5/// Generate code by expanding template expressions with substitutions.
6///
7/// The `xmacro!` macro allows you to define code templates with substitution points
8/// and expand them using sets of values. It supports:
9///
10/// - Named and unnamed definitions
11/// - Nested scopes with `${...}`
12/// - Define expansions with `$(...)`
13///
14/// # Example
15/// ```rust
16/// # use xmacro::xmacro;
17/// struct Container<T>(T);
18/// trait Trait {}
19/// xmacro! {
20/// // Define types to substitute
21/// $(T: (i32)(u32)(f64))
22///
23/// // Generate implementations
24/// impl Trait for Container<$T> {}
25/// }
26/// ```
27#[allow(clippy::missing_panics_doc)]
28#[proc_macro]
29pub fn xmacro(input: TokenStream) -> TokenStream {
30 xmacro_expand(input.into()).unwrap().into()
31}
32
33/// Does per item expansion of xmacro definitions.
34///
35/// Unlike [`xmacro!`] which processes everything in one scope, `xmacro_items!` treats each
36/// item independently, expanding them as if each were in its own separate scope. This is
37/// particularly useful when generating multiple independent items at module level.
38///
39/// # Example
40///
41/// ```rust
42/// use xmacro::xmacro_items;
43///
44/// trait IsInteger {}
45/// trait IsSigned {}
46/// trait IsUnsigned {}
47/// trait IsFloat {}
48///
49/// xmacro_items! {
50/// // Each item expands independently in its own scope
51///
52/// // Generates specific trait implementations
53/// impl IsInteger for $(i8 u64) {}
54/// impl IsFloat for $(f32 f64) {}
55/// impl IsSigned for $(i8 i16 i32 i64 f32 f64) {}
56/// impl IsUnsigned for $(u8 u16 u32 u64) {}
57/// }
58///
59/// // Expands to:
60/// // impl IsInteger for i8 {}
61/// // same for i16 i32 i64 u8 u16 u32 u64
62/// // impl IsFloat for f32 {}
63/// // impl IsFloat for f64 {}
64/// // impl IsSigned for i8 {}
65/// // ... and so on
66/// ```
67#[allow(clippy::missing_panics_doc)]
68#[proc_macro]
69pub fn xmacro_items(input: TokenStream) -> TokenStream {
70 xmacro_expand_items(input.into()).unwrap().into()
71}