Crate toon_macro

Crate toon_macro 

Source
Expand description

§toon-macro

Ergonomic macros for constructing and parsing TOON (Token-Oriented Object Notation) values.

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 values
  • toon_str! macro: Parse TOON-format strings at runtime
  • ToonTable trait: Encode/decode tabular data efficiently
  • #[derive(ToonTable)]: Automatic table serialization (requires derive feature)

§Quick Start

§Using toon! (Rust-DSL)

The toon! macro provides a JSON-like syntax for constructing TOON values:

use toon_macro::{toon, Value};

// Simple object
let user = toon!({
    name: "Alice",
    age: 30,
    active: true
});

// Nested structures
let data = toon!({
    config: {
        host: "localhost",
        port: 8080
    },
    users: [
        { id: 1, name: "Alice" },
        { id: 2, name: "Bob" }
    ]
});

// Using variables
let name = "Charlie";
let score = 95i64;
let result = toon!({
    name: name,
    score: score
});

§Using toon_str! (TOON syntax)

The toon_str! macro parses TOON-format text at runtime:

use toon_macro::toon_str;

let config = toon_str!(r#"
host: "localhost"
port: 5432
active: true
"#);

§Using from_toon_str (with error handling)

For fallible parsing, use from_toon_str directly:

use toon_macro::from_toon_str;

let input = r#"name: "Alice""#;
match from_toon_str(input) {
    Ok(value) => println!("Parsed: {:?}", value),
    Err(e) => eprintln!("Parse error: {}", e),
}

§Using ToonTable (requires derive feature)

use toon_macro::ToonTable;

#[derive(ToonTable)]
struct User {
    id: u64,
    name: String,
    #[toon(rename = "user_role")]
    role: String,
}

let users = vec![
    User { id: 1, name: "Alice".into(), role: "admin".into() },
    User { id: 2, name: "Bob".into(), role: "user".into() },
];

// Encode to compact table format
let table = User::to_toon_table(&users);

// Decode back to structs
let decoded = User::from_toon_table(&table).unwrap();

§Feature Flags

  • serde (default): Enable serde integration for serializing arbitrary types
  • derive: Enable #[derive(ToonTable)] macro
  • pretty: Enable pretty-printing functions

§Minimum Supported Rust Version

This crate requires Rust 1.70 or later.

Re-exports§

pub use error::Error;
pub use error::Result;
pub use ser::from_toon_str;
pub use ser::to_toon_string;
pub use table::ToonTable;
pub use ser::deserialize;
pub use ser::serialize;
pub use value::from_value;
pub use value::to_value;

Modules§

error
Unified error types for toon-macro.
internal
Internal helper macros and utilities.
macros
TOON construction macros.
ser
TOON serialization and deserialization utilities.
table
TOON table encoding and decoding.
value
TOON Value type and conversions.

Macros§

toon
Construct a TOON Value using a JSON-like DSL.
toon_str
Parse a TOON-format string literal at runtime.

Enums§

Number
Represents a TOON number.
Value
Represents any valid TOON value.

Type Aliases§

Map
An order-preserving map type used for TOON objects.