summaryrefslogtreecommitdiff
path: root/README.md
blob: 14439b44f41240c36bbc9e3f3eb1e71febc7abbd (plain)
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
# just_template

> a tool for code gen via templates

## Template File Writing Rules

`just_template` gens repetitive code using three core concepts: `impl_mark`, `impl_area`, and `param`.

1. impl_mark
   Mark an "impl point" with a line starting with 10 `>` chars: `>>>>>>>>>> NAME`.
   Used for positioning. The system extracts the matching `impl_area` content and expands it here.

2. impl_area
   Declare a reusable code template:
   `@@@ NAME >>>`
   [template content]
   `@@@ <<<`
   Inside, use param placeholders like `<<<PARAM>>>`.
   When adding an impl via cmd (e.g., `insert_impl!`), the system copies the area, replaces params, and appends to the impl_mark.

3. param
   - Params outside an impl_area are replaced globally.
   - Params inside are replaced per-impl when generating.

Example:

Template:
```rust
// Auto generated
use std::collections::HashMap;

pub async fn my_func(
    name: &str,
    data: &[u8],
    params: &HashMap<String, String>,
) -> Option<Result<Vec<u32>, std::io::Error>> {
    match name {
>>>>>>>>>> arms
@@@ >>> arms
        "<<<crate_name>>>" => Some(<<<crate_name>>>::exec(data, params).await),
@@@ <<<
        _ => None,
    }
}
```

Run cmds:
```rust
tmpl!(tmpl += {
    arms {
        (crate_name = "my"),
        (crate_name = "you")
    }
});
```

The `arms` impl_area becomes:
```rust
        "my" => Some(my::exec(data, params).await),
        "you" => Some(you::exec(data, params).await),
```

Final expanded code:
```rust
// Auto generated
use std::collections::HashMap;

pub async fn my_func(
    name: &str,
    data: &[u8],
    params: &HashMap<String, String>,
) -> Option<Result<Vec<u32>, std::io::Error>> {
    match name {
        "my" => Some(my::exec(data, params).await),
        "you" => Some(you::exec(data, params).await),
        _ => None,
    }
}
```

## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
just_template = "0.1"
```

## License

This project is dual-licensed under MIT and Apache 2.0.
See the LICENSE file for details.