toon-macro
Ergonomic macros for constructing and parsing TOON (Token-Oriented Object Notation) values in Rust.
TOON is a compact data format designed to convey the same information as JSON with 30-60% fewer tokens, making it ideal for LLM prompts and responses.
Features
toon!macro: JSON-like Rust DSL for constructing TOON valuestoon_str!macro: Parse TOON-format strings at runtimeToonTabletrait: Encode/decode tabular data efficiently#[derive(ToonTable)]: Automatic table serialization (withderivefeature)- Full serde integration: Serialize any serde type to TOON
Installation
Add to your Cargo.toml:
[]
= "0.1"
With derive macro support:
[]
= { = "0.1", = ["derive"] }
Quick Start
Using toon! (Rust-DSL)
The toon! macro provides a JSON-like syntax for constructing TOON values:
use ;
// Simple object
let user = toon!;
// Nested structures
let data = toon!;
// Using variables
let name = "Charlie";
let score = 95i64;
let result = toon!;
Using toon_str! (TOON syntax)
Parse TOON-format text at runtime:
use toon_str;
let config = toon_str!;
Error Handling with from_toon_str
For fallible parsing, use from_toon_str directly:
use from_toon_str;
let input = r#"name: "Alice""#;
match from_toon_str
Using ToonTable for Tabular Data
With the derive feature, you can efficiently encode/decode collections of structs:
use ;
let users = vec!;
// Encode to compact table format
let table = to_toon_table;
// Decode back to structs
let decoded = from_toon_table.unwrap;
ToonTable Attributes
When using #[derive(ToonTable)], you can customize field behavior:
#[toon(rename = "column_name")]- Use a custom column name#[toon(skip)]- Exclude this field from the table#[toon(default)]- UseDefault::default()when the column is missing#[toon(order = N)]- Specify explicit column ordering (0-based)
Serde Integration
Serialize and deserialize any serde-compatible type:
use ;
use ;
let point = Point ;
let toon_string = serialize.unwrap;
let decoded: Point = deserialize.unwrap;
Feature Flags
| Feature | Default | Description |
|---|---|---|
serde |
Yes | Enable serde integration |
derive |
No | Enable #[derive(ToonTable)] macro |
pretty |
No | Enable pretty-printing functions |
Why TOON?
TOON (Token-Oriented Object Notation) is designed to be:
- Token-efficient: 30-60% fewer tokens than JSON
- Human-readable: Easy to read and write
- JSON-compatible: Same data model as JSON
- LLM-friendly: Optimized for AI model input/output
Minimum Supported Rust Version
This crate requires Rust 1.88 or later (due to serde_toon2 using if let chains which were stabilized in Rust 1.88).
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
Built on top of serde_toon2 for TOON parsing and serialization.