Skip to content

Commit fd54021

Browse files
authored
Merge pull request #3 from clucompany/1.1.0
1.1.0
2 parents e1e516b + 86a21cf commit fd54021

File tree

18 files changed

+336
-475
lines changed

18 files changed

+336
-475
lines changed

Cargo.toml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
[package]
22
name = "include_tt"
3-
version = "1.0.7"
3+
version = "1.1.0"
44
edition = "2024"
55
authors = ["Denis Kotlyarov (Денис Котляров) <denis2005991@gmail.com>"]
66
repository = "https://github.com/clucompany/include_tt.git"
77
license = "MIT OR Apache-2.0"
88
readme = "README.md"
99

10-
description = "Macro for embedding (trees, strings, arrays) into macro trees directly from files."
11-
keywords = ["procedural-macros", "macro", "code-generation", "code-transformation", "clucompany"]
12-
categories = ["development-tools", "development-tools::build-utils"]
10+
description = "Macros for ultra-flexible injection of compiler trees, literals, or binary data into Rust syntax trees from external sources."
11+
keywords = [
12+
"procedural-macros",
13+
"macro",
14+
"codegen",
15+
"syntax-injection",
16+
"tt-injection",
17+
"clucompany"
18+
]
19+
20+
categories = [
21+
"development-tools",
22+
"development-tools::build-utils",
23+
"development-tools::procedural-macro",
24+
"code-generation",
25+
"parsing"
26+
]
27+
28+
[features]
29+
default = [ ]
30+
escape_symbol = []
1331

1432
[lib]
1533
proc-macro = true

README.md

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<b>[include_tt]</b>
44

5-
(Macro for embedding (trees, strings, arrays) into macro trees directly from files.)
5+
(Macros for ultra-flexible injection of compiler trees, literals, or binary data into Rust syntax trees from external sources.)
66
</br></br>
77

88
<div id="badges">
@@ -37,60 +37,33 @@ Add this to your Cargo.toml:
3737

3838
```toml
3939
[dependencies]
40-
include_tt = "1.0.7"
40+
include_tt = "1.1.0"
4141
```
4242

4343
and this to your source code:
4444

4545
```rust
46-
use include_tt::include_tt;
46+
use include_tt::inject;
4747
```
4848

4949
## Example
5050

5151
```rust
52-
use include_tt::include_tt;
52+
use include_tt::inject;
5353
use std::fmt::Write;
54-
55-
// Example demonstrating the usage of include_tt! macro for embedding content from files.
56-
{
57-
// Embedding trees from a file in an arbitrary place of other macros.
58-
let a = 10;
59-
let b = 20;
60-
let mut end_str = String::new();
61-
62-
// Using include_tt! to embed content into a macro.
63-
include_tt! {
64-
let _e = write!(
65-
&mut end_str,
66-
67-
"arg1: {}, arg2: {}",
68-
#include!("./for_examples/full.tt") // this file contains `a, b`.
69-
);
54+
fn main() {
55+
let mut buf = String::new();
56+
57+
inject! {
58+
write!(
59+
&mut buf,
60+
"Welcome, {}. Your score is {}.",
61+
#tt("examples/name.tt"), // `"Ferris"`
62+
#tt("examples/" "score" ".tt") // `100500`
63+
).unwrap();
7064
}
71-
72-
// Asserting the result matches the expected output.
73-
assert_eq!(end_str, "arg1: 10, arg2: 20");
74-
}
7565

76-
{
77-
// Loading a string from "full.tt" using include_tt! macro.
78-
let str = include_tt!(
79-
#include_str!("./for_examples/full.tt") // this file contains `a, b`.
80-
);
81-
82-
// Asserting the result matches the expected output.
83-
assert_eq!(str, "a, b");
84-
}
85-
86-
{
87-
// Loading a array from "full.tt" using include_tt! macro.
88-
let array: &'static [u8; 4] = include_tt!(
89-
#include_arr!("./for_examples/full.tt") // this file contains `a, b`.
90-
);
91-
92-
// Asserting the result matches the expected output.
93-
assert_eq!(array, b"a, b");
66+
assert_eq!(buf, "Welcome, Ferris. Your score is 100500.");
9467
}
9568
```
9669

examples/basic_codegen.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
macro_rules! new_module {
2+
[ @($const_t: ident) : [ $($path:tt)* ]; ] => {
3+
include_tt::inject! {
4+
#[allow(dead_code)]
5+
#[allow(non_upper_case_globals)]
6+
pub mod my_module {
7+
pub const a: usize = 0;
8+
pub const b: usize = 10;
9+
10+
// The `#POINT_TRACKER_FILES:` marker allows the macro to add additional
11+
// instructions that tell the compiler which files to track so that it can
12+
// recompile the macro if they change. This is completely optional, but without
13+
// it tracking will not work.
14+
#POINT_TRACKER_FILES:
15+
16+
pub const $const_t: (usize, usize) = (#tt($($path)*));
17+
}
18+
}
19+
};
20+
}
21+
22+
fn main() {
23+
// we created a module "my_module" and a constant "T" containing (a, b).
24+
//
25+
// if you need to change, for example, to (b,a) or substitute constant values,
26+
// we will only change the contents of the file "for_examples/full.tt"!
27+
new_module! {
28+
@(T): [examples / "full" . t 't']; // this file contains "a, b", see "for_examples/full.tt"
29+
}
30+
assert_eq!(my_module::T, (0, 10));
31+
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use include_tt::include_tt;
1+
use include_tt::inject;
22
use std::fmt::Write;
33

44
macro_rules! test2_rules {
@@ -44,35 +44,35 @@ macro_rules! test2_rules {
4444

4545
fn main() {
4646
// Loading trees from a file and substituting them into a custom macro.
47-
include_tt! {
48-
#POINT_TRACKER_FILES;
47+
inject! {
48+
#POINT_TRACKER_FILES:
4949
test2_rules! {
50-
[ #include!("./examples/full.tt") ] // this file contains `a, b`.
51-
[ #include! { "./examples/full.tt" } ] // this file contains `a, b`.
50+
[ #tt("./examples/full.tt") ] // this file contains `a, b`.
51+
[ #tt { "./examples/full.tt" } ] // this file contains `a, b`.
5252
}
5353
test2_rules! {
54-
#include!("./examples/full.tt") // this file contains `a, b`.
54+
#tt ("./examples/full.tt") // this file contains `a, b`.
5555
}
5656

5757
println!(
5858
concat!(
5959
"#",
60-
#include_str!("./examples/full.tt"), // this file contains `a, b`.
60+
#str("./examples/full.tt"), // this file contains `a, b`.
6161
"#"
6262
)
6363
);
6464
}
6565

6666
{
6767
// Loading a string from a file.
68-
let str = include_tt!(#include_str!("./examples/full.tt")); // this file contains `a, b`.
68+
let str = inject!(#str("./examples/full.tt")); // this file contains `a, b`.
6969
assert_eq!(str, "a, b");
7070
}
7171

7272
{
7373
// Loading an array from a file.
74-
let array: &'static [u8; 4] = include_tt!(
75-
#include_arr!("./examples/full.tt") // this file contains `a, b`.
74+
let array: &'static [u8; 4] = inject!(
75+
#array("./examples/full.tt") // this file contains `a, b`.
7676
);
7777
assert_eq!(array, b"a, b");
7878
}
@@ -83,12 +83,12 @@ fn main() {
8383
let b = 20;
8484

8585
let mut end_str = String::new();
86-
include_tt! {
86+
inject! {
8787
let _e = write!(
8888
&mut end_str,
8989

9090
"arg1: {}, arg2: {}",
91-
#include!("./examples/full.tt") // this file contains `a, b`.
91+
#tt("./examples/full.tt") // this file contains `a, b`.
9292
);
9393
}
9494
assert_eq!(end_str, "arg1: 10, arg2: 20");

examples/constmodule.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/mrules.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use include_tt::include_tt;
1+
use include_tt::inject;
22

33
macro_rules! test_rules {
44
[
@@ -35,14 +35,14 @@ fn main() {
3535
let a = 10;
3636
let b = 20;
3737

38-
include_tt! {
38+
inject! {
3939
// this macro only supports:
4040
// a + b = n
4141
// or
4242
// a - b = n
43-
#POINT_TRACKER_FILES;
43+
#POINT_TRACKER_FILES:
4444
test_rules! {
45-
#include!("./examples/mrules.tt") // this file contains "a + b = n", see "./for_examples/mrules.tt"
45+
#tt("./examples/mrules.tt") // this file contains "a + b = n", see "./for_examples/mrules.tt"
4646
}
4747
}
4848
assert_eq!(n, a + b);

examples/name.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"Ferris"

examples/score.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
100500

examples/template_macro.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use include_tt::inject;
2+
use std::fmt::Write;
3+
fn main() {
4+
let mut buf = String::new();
5+
6+
inject! {
7+
write!(
8+
&mut buf,
9+
"Welcome, {}. Your score is {}!",
10+
#tt("examples/name.tt"), // `"Ferris"`
11+
#tt("examples/" "score" ".tt") // `100500`
12+
).unwrap();
13+
}
14+
15+
assert_eq!(buf, "Welcome, Ferris. Your score is 100500!");
16+
}

0 commit comments

Comments
 (0)