toon_rust/lib.rs
1//! Token-Oriented Object Notation (TOON) - Rust implementation
2//!
3//! TOON is a compact, human-readable format designed to reduce token usage
4//! in Large Language Model (LLM) prompts by 30–60% compared to JSON.
5//!
6//! # Examples
7//!
8//! ## Standalone API
9//!
10//! ```rust
11//! use toon_rust::{encode, decode};
12//! use serde_json::json;
13//!
14//! let data = json!({
15//! "items": [
16//! {"sku": "A1", "qty": 2, "price": 9.99},
17//! {"sku": "B2", "qty": 1, "price": 14.5}
18//! ]
19//! });
20//!
21//! let toon = encode(&data, None).unwrap();
22//! let decoded = decode(&toon, None).unwrap();
23//! ```
24//!
25//! ## Streaming API
26//!
27//! For large datasets, use the streaming API to avoid loading everything into memory:
28//!
29//! ```rust,no_run
30//! use std::fs::File;
31//! use std::io::BufWriter;
32//! use serde_json::json;
33//! use toon_rust::{encode_stream, decode_stream};
34//!
35//! // Encode to file
36//! let data = json!({"name": "Alice", "age": 30});
37//! let file = File::create("output.toon").unwrap();
38//! let mut writer = BufWriter::new(file);
39//! encode_stream(&data, &mut writer, None).unwrap();
40//!
41//! // Decode from file
42//! let file = File::open("output.toon").unwrap();
43//! let decoded = decode_stream(file, None).unwrap();
44//! ```
45//!
46//! ## Serde API (requires `serde` feature)
47//!
48//! ```rust,no_run
49//! use serde::{Serialize, Deserialize};
50//! use toon_rust::{to_string, from_str};
51//!
52//! #[derive(Serialize, Deserialize)]
53//! struct Product {
54//! sku: String,
55//! qty: u32,
56//! price: f64,
57//! }
58//!
59//! let products = vec![
60//! Product { sku: "A1".to_string(), qty: 2, price: 9.99 },
61//! Product { sku: "B2".to_string(), qty: 1, price: 14.5 },
62//! ];
63//!
64//! let toon = to_string(&products).unwrap();
65//! let decoded: Vec<Product> = from_str(&toon).unwrap();
66//! ```
67
68pub mod decode;
69pub mod encode;
70pub mod error;
71pub mod options;
72mod simd;
73
74pub use decode::{decode, decode_stream};
75pub use encode::{encode, encode_stream};
76pub use error::Error;
77pub use options::{DecodeOptions, EncodeOptions};
78
79#[cfg(feature = "serde")]
80pub mod serde_api;
81
82#[cfg(feature = "serde")]
83pub use serde_api::{from_reader, from_str, to_string, to_writer};