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
//! # Useful program macros for development
//! 
//! - [format_template_code!](./macro.format_template_code.html) extend `format!` macro, accept in {…} internal input the rust template code.

extern crate proc_macro;
use proc_macro::*;



mod macro_format_template_code;

/// extend `format!` macro, accept in {…} internal input the rust template code.
/// # 扩展 `format!` 宏, 接受在 `{...}` 中输入 `rust` 模板代码.  
/// 宏展开后实际上是以命名参数的方式使用 `format!(template,named_args...)`.
/// ### 结构:
/// > `format_template_code!(template,args1_r=value,args2_r=value,...) -> String`
///
/// ### 参数:
/// > `template`:   以大括号 `{...}` 包围的模板代码, 在模板中以 `{var_name}` 的方式指定命名替换参数;  
/// > `arg,...` :   命名替换参数, 以 `arg_1 = "user",arg_2 = "18",...` 的方式指定替换值;    
///
/// ### 示例:
/// ```
/// mod format_template_code_example {
///
///     use autodo_helper::format_template_code;
///
///     #[test]
///     fn test() {
///         let s = format_template_code!(
///             /* in {...} internal input template code */
///             {
///                 struct {struct_name} {
///                     a: String,
///                     b: String,
///                 }
///                 macro_rules! {macro_name} {
///                     () => {
///                         println!("do something...");
///                     };
///                 }
///             },
///             struct_name = "Example",
///             macro_name = "example_macro",
///         );
///
///         assert_eq!(
///             s,
///             r#"struct Example  { a:String,b:String, } macro_rules! example_macro  { ()=> { println!("do something..."); } ; } "#
///         );
///     }
///     // format s value as follows:
///     struct Example {
///         a: String,
///         b: String,
///     }
///     macro_rules! example_macro {
///         () => {
///             println!("do something...");
///         };
///     }
/// }
/// ```
#[proc_macro]
pub fn format_template_code(stream: TokenStream) -> TokenStream {
    macro_format_template_code::format_template_code(stream)
}