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
//! # MMM 😋
//!
//! Export tokens to other crates. Now with 100% less proc macros!
//!
//! This crate provides the [`export`] macro which allows exporting tokens.
//! The concept is similar to that used by the inspiration for this crate [macro_magic](https://docs.rs/macro_magic/latest/macro_magic/).
//! Namely, a macro_rules is generated alongside the tokens to be exported. This macro_rules will invoke a passed macro with the tokens exported. Simple as that. 
//!
//! The difference to macro_magic is that this
//! crate does it all with one `macro_rules!` macro. No more need to poll in a set of proc macros
//! if you don't need the full power of macro_magic. Instead use [`export`] to generate a 
//! macro you or a dependant can use. Also, a dependant doesn't need to know about
//! `mini_macro_magic` to use the generated macro_rules. They are fully self contained.
//!
//! ## no_std
//! `mini_macro_magic` is fully `no_std` compatible.

#![no_std]

/// Export tokens for use by other macros.
///
/// Creates a macro that injects the tokens the macro was originally invoked with.
///
/// Invoked with the following form.
/// ```ignore
/// export! {
///     <@only_export>
///
///     /// Extra docs for the generated macro can be added here.
///     <visibility> name_for_the_macro$
///
///     any rust tokens here
/// }
/// ```
///
/// Note, the `$` after the macro name must be there.
///
/// * `<@only_export>` - Modifier to disable emitting the given tokens into the local scope. Don't
/// use this if you want the exported tokens to be real code as well as exporting them.
/// * `<visibility>` - Visibility modifier. Can be nothing, `pub`, or `pub crate`. Nothing
/// will result in a macro with a `pub(crate) use` to make it visible to the local crate.
/// `pub` will make the macro visible outside of the crate usinge `#[macro_export]`. `pub crate` is
/// like `pub` but for macros defined in the root of a crate.
///
/// The generated macro_rules will have a section of docs showing the tokens it expands to (See
/// [`example::inspect_demo`]). Additionally, the macro_rules will have a doc test generated for
/// it (see [`example::inspect_demo`](example::inspect_demo#examples)).
///
/// # Examples
/// 
/// ## No `#[macro_export]`
///
/// ```
/// mod inner {
///     use mini_macro_magic::export;
///
///     export! {
///         /// Inspect demo struct.
///         inspect_demo$
///
///         /// Demo struct for inspection.
///         pub struct Demo {
///             pub x: i32,
///             pub y: i32,
///         }
///     }
/// }
///
/// macro_rules! first_token {
///     ($first:tt $($t:tt)*) => {
///         stringify!($first)
///     }
/// }
///
/// type Demo = inner::Demo;
///
/// let first = inner::inspect_demo!(first_token);
///
/// assert_eq!(first, "#");
/// ```
///
/// ## With `#[macro_export]` at crate root
///
/// ```
/// use mini_macro_magic::export;
///
/// export! {
///     /// Inspect demo struct.
///     pub crate inspect_demo$
///
///     /// Demo struct for inspection.
///     pub struct Demo {
///         pub x: i32,
///         pub y: i32,
///     }
/// }
///
/// macro_rules! first_token {
///     ($first:tt $($t:tt)*) => {
///         stringify!($first)
///     }
/// }
///
/// type OtherDemo = Demo;
///
/// let first = inspect_demo!(first_token);
///
/// assert_eq!(first, "#");
/// ```
///
/// ## With `#[macro_export]` in module
///
/// ```
/// mod inner {
///     use mini_macro_magic::export;
///
///     export! {
///         /// Inspect demo struct.
///         pub inspect_demo$
///
///         /// Demo struct for inspection.
///         pub struct Demo {
///             pub x: i32,
///             pub y: i32,
///         }
///     }
/// }
///
/// macro_rules! first_token {
///     ($first:tt $($t:tt)*) => {
///         stringify!($first)
///     }
/// }
///
/// type Demo = inner::Demo;
///
/// let first = inner::inspect_demo!(first_token);
///
/// assert_eq!(first, "#");
/// ```
///
/// ## Just export
///
/// ```
/// use mini_macro_magic::export;
///
/// export! {
///     @only_export
///
///     /// Inspect demo struct.
///     pub crate inspect_tokens$
///
///     just some tokens to export
/// }
///
/// macro_rules! first_token {
///     ($first:tt $($t:tt)*) => {
///         stringify!($first)
///     }
/// }
///
/// let first = inspect_tokens!(first_token);
///
/// assert_eq!(first, "just");
/// ```
///
#[macro_export]
macro_rules! export {
    ($(#[$($a:tt)*])* pub crate $name:ident $dollar:tt $($t:tt)*) => {
        $crate::export!(
            @helper

            $(#[$($a)*])*
            #[macro_export]
            $name $dollar

            {}

            {$($t)*}
            {$($t)*}
        );
    };
    ($(#[$($a:tt)*])* pub $name:ident $dollar:tt $($t:tt)*) => {
        $crate::export!(
            @helper

            $(#[$($a)*])*
            #[doc(hidden)]
            #[macro_export]
            $name $dollar

            {
                #[doc(inline)]
                pub use $name;
            }

            {$($t)*}
            {$($t)*}
        );
    };
    ($(#[$($a:tt)*])* $name:ident $dollar:tt $($t:tt)*) => {
        $crate::export!(
            @helper

            $(#[$($a)*])*
            $name $dollar

            {
                pub(crate) use $name;
            }

            {$($t)*}
            {$($t)*}
        );
    };
    (@only_export $(#[$($a:tt)*])* pub crate $name:ident $dollar:tt $($t:tt)*) => {
        $crate::export!(
            @helper

            $(#[$($a)*])*
            #[macro_export]
            $name $dollar

            {}

            {$($t)*}
            {}
        );
    };
    (@only_export $(#[$($a:tt)*])* pub $name:ident $dollar:tt $($t:tt)*) => {
        $crate::export!(
            @helper

            $(#[$($a)*])*
            #[doc(hidden)]
            #[macro_export]
            $name $dollar

            {
                #[doc(inline)]
                pub use $name;
            }

            {$($t)*}
            {}
        );
    };
    (@only_export $(#[$($a:tt)*])* $name:ident $dollar:tt $($t:tt)*) => {
        $crate::export!(
            @helper

            $(#[$($a)*])*
            $name $dollar

            {
                pub(crate) use $name;
            }

            {$($t)*}
            {}
        );
    };
    (@helper $(#[$($a:tt)*])* $name:ident $dollar:tt {$($e:tt)*} {$first:tt $($t:tt)*} {$($i:tt)*}) => {
        $(#[$($a)*])*
        ///
        /// Calls the provided macro with the exported tokens.
        ///
        /// <details>
        /// <summary>Expand to show exported tokens</summary>
        ///
        /// ```
        #[doc = stringify!($first $($t)*)]
        /// ```
        ///
        /// </details>
        ///
        /// # Examples
        ///
        /// ```
        #[doc = concat!("use ", module_path!(), "::", stringify!($name), ";")]
        ///
        /// macro_rules! first_token {
        ///     ($first:tt $($t:tt)*) => {
        ///         stringify!($first)
        ///     }
        /// }
        ///
        #[doc = concat!("let first = ", stringify!($name), "!(first_token);")]
        ///
        #[doc = concat!("assert_eq!(first, \"", stringify!($first), "\");")]
        /// ```
        macro_rules! $name {
            ($dollar ($dollar m:tt)*) => {
                $dollar ($dollar m)*!($first $($t)*)
            }
        }

        $($e)*
        
        $($i)*
    };
}

/// Example of using [`export`]. (This module is not part of the crate's public API)
///
/// Code used for this module:
/// ```
/// pub mod example {
///     use mini_macro_magic::export;
///     
///     export! {
///         /// Inspect demo struct.
///         pub inspect_demo$
///
///         /// Demo struct for inspection.
///         pub struct Demo {
///             /// The X value.
///             pub x: i32,
///
///             /// The Y value.
///             pub y: i32,
///         }
///     }
/// }
/// ```
///
/// [`Demo`](example::Demo) is a struct definition exported by [`export`].
/// As you can see it exists as expected as a normal struct.
///
/// [`inspect_demo`] is the inspection macro that was generated by [`export`].
/// This is the macro you would call to inspect the tokens of [`Demo`](example::Demo).
#[cfg(any(doc, doc_example))]
pub mod example {
    export! {
        /// Inspect demo struct.
        pub inspect_demo$

        /// Demo struct for inspection.
        pub struct Demo {
            /// The X value.
            pub x: i32,

            /// The Y value.
            pub y: i32,
        }
    }
}