toon/lib.rs
1//! Token-Oriented Object Notation (TOON)
2//!
3//! A compact, human-readable format designed for passing structured data to Large Language Models
4//! with significantly reduced token usage.
5//!
6//! # Example
7//!
8//! ```
9//! use toon::{encode, EncodeOptions, Delimiter};
10//! use serde_json::json;
11//!
12//! let data = json!({
13//! "items": [
14//! {"sku": "A1", "qty": 2, "price": 9.99},
15//! {"sku": "B2", "qty": 1, "price": 14.5}
16//! ]
17//! });
18//!
19//! let toon_output = encode(&data, None);
20//! println!("{}", toon_output);
21//! // Output:
22//! // items[2]{price,qty,sku}:
23//! // 9.99,2,A1
24//! // 14.5,1,B2
25//! ```
26
27mod encoders;
28mod normalize;
29mod primitives;
30mod types;
31mod writer;
32
33pub use types::{Delimiter, EncodeOptions};
34pub use serde_json;
35
36use normalize::normalize_value;
37use encoders::encode_value;
38
39/// Encode a serde_json::Value to TOON format
40///
41/// # Arguments
42///
43/// * `value` - A reference to a serde_json::Value to encode
44/// * `options` - Optional encoding options. If None, defaults are used.
45///
46/// # Returns
47///
48/// A String containing the TOON-formatted output
49///
50/// # Example
51///
52/// ```
53/// use toon::{encode, EncodeOptions, Delimiter};
54/// use serde_json::json;
55///
56/// let data = json!({"name": "Ada", "active": true});
57/// let result = encode(&data, None);
58/// assert_eq!(result, "active: true\nname: Ada");
59/// ```
60pub fn encode(value: &serde_json::Value, options: Option<EncodeOptions>) -> String {
61 let opts = options.unwrap_or_default();
62 let normalized = normalize_value(value);
63 encode_value(&normalized, &opts)
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69 use serde_json::json;
70
71 #[test]
72 fn test_simple_object() {
73 let data = json!({"id": 123, "name": "Ada"});
74 let result = encode(&data, None);
75 assert!(result.contains("id: 123"));
76 assert!(result.contains("name: Ada"));
77 }
78
79 #[test]
80 fn test_primitive_array() {
81 let data = json!({"tags": ["reading", "gaming"]});
82 let result = encode(&data, None);
83 assert_eq!(result, "tags[2]: reading,gaming");
84 }
85
86 #[test]
87 fn test_empty_object() {
88 let data = json!({});
89 let result = encode(&data, None);
90 assert_eq!(result, "");
91 }
92}