https://github.com/dekirisu/buns
Create simple code templates - basically macro_rules lite
https://github.com/dekirisu/buns
proc-macro rust rust-lang
Last synced: 12 months ago
JSON representation
Create simple code templates - basically macro_rules lite
- Host: GitHub
- URL: https://github.com/dekirisu/buns
- Owner: dekirisu
- License: apache-2.0
- Created: 2024-11-11T11:33:57.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-12T11:51:26.000Z (about 1 year ago)
- Last Synced: 2025-03-26T16:48:50.784Z (about 1 year ago)
- Topics: proc-macro, rust, rust-lang
- Language: Rust
- Homepage:
- Size: 12.7 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
A simple way to write repeatable code anywhere, by defining buns and toppings. 🍞
## Sandwich / Compose
This can be seen as `format!()`, but for code:
1. **Buns**: Write the code, use `^0 ^1 .. ^N` as (Topping) placeholders
2. **Topping**: Write code inserts: `#0^1^..^N`, where numbers = any code
3. **Why tho?** The point is, you can repeat 2. and make infinite sandwiches. 🥪
```rust
buns::sandwich!{
const ^0: u32 = ^1; // Buns
#TEST^10 #OMEGA^59 // Toppings
}
// Will generate:
// const TEST: u32 = 10;
// const OMEGA: u32 = 59;
```
## Prepare / Preset
This can be seen as a simplified `macro_rules!{}`, where you prepare named **Buns** and add the **Toppings** later using the generated macro (The code (Buns) is automatically added to the macro documentation):
```rust
buns::prepare!{
burger // Name
let a = ^0 + ^0; // Buns
println!("{a}"); // "
}
fn main(){
burger!{#1 #2 #4+4 #4 #2*2} // Toppings
// prints: 2 4 16 8 8
}
```
## Flexibility
You can use any other magical token macro like [paste](https://github.com/dtolnay/paste) to add functionality:
```rust
buns::sandwich!{
paste::paste!{const [<^1 _ ^0:upper>]: ^0 = ^2;}
#u32^BREAD^100 #f32^BREAD^12.0
}
// Will generate:
// const BREAD_U32: u32 = 100;
// const BREAD_F32: f32 = 12.9;
```
---
### License
Licensed under either of Apache License, Version
2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.