Skip to main content

Crate rustapi_toon

Crate rustapi_toon 

Source
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/toon or text/toon
  • Response: application/toon

Modules§

decode
Decoder Implementation
encode
Encoder Implementation

Structs§

AcceptHeader
Parsed Accept header with quality values
DecodeOptions
Options for decoding TOON format to JSON values.
EncodeOptions
Options for encoding JSON values to TOON format.
LlmResponse
LLM-optimized response wrapper with token counting.
MediaTypeEntry
A single media type entry from Accept header
Negotiate
Content-negotiated response wrapper
Toon
TOON body extractor and response type

Enums§

OutputFormat
Supported output formats
ToonError
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