Expand description
§TOON Format Support for RustAPI
This crate provides TOON (Token-Oriented Object Notation) support for the RustAPI framework. TOON is a compact, human-readable format designed for passing structured data to Large Language Models (LLMs) with significantly reduced token usage (typically 20-40% savings).
§Quick Example
JSON (16 tokens, 40 bytes):
{
"users": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
}TOON (13 tokens, 28 bytes) - 18.75% token savings:
users[2]{id,name}:
1,Alice
2,Bob§Usage
Add to your Cargo.toml:
[dependencies]
rustapi-rs = { version = "0.1", features = ["toon"] }§Toon Extractor
Parse TOON request bodies:
ⓘ
use rustapi_rs::prelude::*;
use rustapi_rs::toon::Toon;
#[derive(Deserialize)]
struct CreateUser {
name: String,
email: String,
}
async fn create_user(Toon(user): Toon<CreateUser>) -> impl IntoResponse {
Json(user)
}§Toon Response
Return TOON formatted responses:
ⓘ
use rustapi_rs::prelude::*;
use rustapi_rs::toon::Toon;
#[derive(Serialize)]
struct User {
id: u64,
name: String,
}
async fn get_user() -> Toon<User> {
Toon(User {
id: 1,
name: "Alice".to_string(),
})
}§Content Types
- Request:
application/toonortext/toon - Response:
application/toon
Modules§
Structs§
- Accept
Header - Parsed Accept header with quality values
- Decode
Options - Options for decoding TOON format to JSON values.
- Encode
Options - Options for encoding JSON values to TOON format.
- LlmResponse
- LLM-optimized response wrapper with token counting.
- Media
Type Entry - A single media type entry from Accept header
- Negotiate
- Content-negotiated response wrapper
- Toon
- TOON body extractor and response type
Enums§
- Output
Format - Supported output formats
- Toon
Error - Error type for TOON operations
Constants§
- JSON_
CONTENT_ TYPE - JSON Content-Type
- TOON_
CONTENT_ TYPE - TOON Content-Type header value
- TOON_
CONTENT_ TYPE_ TEXT - Alternative TOON Content-Type (text-based)
- TOON_
FORMAT_ DESCRIPTION - TOON format description for OpenAPI
- X_
FORMAT_ USED - Header name for format used
- X_
TOKEN_ COUNT_ JSON - Header name for JSON token count
- X_
TOKEN_ COUNT_ TOON - Header name for TOON token count
- X_
TOKEN_ SAVINGS - Header name for token savings percentage
Functions§
- api_
description_ with_ toon - OpenAPI info description with TOON support notice
- decode
- Decode a TOON string into any deserializable type.
- decode_
default - Decode with default options (strict mode, type coercion enabled).
- encode
- Encode any serializable value to TOON format.
- encode_
default - Encode with default options (2-space indent, comma delimiter).
- format_
comparison_ example - Generate example responses showing JSON vs TOON
- token_
headers_ schema - Schema for token comparison headers
- toon_
extension - Generate OpenAPI vendor extension for TOON support
- toon_
schema - Generate OpenAPI schema for TOON format