1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! # serde_toon
//!
//! A Serde-compatible serialization library for the TOON (Token-Oriented Object Notation) format.
//!
//! ## What is TOON?
//!
//! TOON is a compact, human-readable data format specifically designed for efficient communication
//! with Large Language Models (LLMs). It achieves 30-60% fewer tokens than equivalent JSON while
//! maintaining readability and structure.
//!
//! ## Key Features
//!
//! - **Token-Efficient**: Minimalist syntax reduces token count by eliminating unnecessary braces,
//! brackets, and quotes
//! - **Tabular Arrays**: Homogeneous object arrays serialize as compact tables with headers
//! - **Serde Compatible**: Works seamlessly with existing Rust types via `#[derive(Serialize, Deserialize)]`
//! - **Type Safe**: Statically typed with comprehensive error reporting
//! - **No Unsafe Code**: Written entirely in safe Rust with zero unsafe blocks
//!
//! ## Quick Start
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! serde = { version = "1.0", features = ["derive"] }
//! serde_toon = "0.2"
//! ```
//!
//! ### Basic Serialization and Deserialization
//!
//! ```rust
//! use serde::{Deserialize, Serialize};
//! use serde_toon::{to_string, from_str};
//!
//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
//! struct User {
//! id: u32,
//! name: String,
//! active: bool,
//! }
//!
//! let user = User {
//! id: 123,
//! name: "Alice".to_string(),
//! active: true,
//! };
//!
//! // Serialize to TOON format
//! let toon_string = to_string(&user).unwrap();
//! // Output: "id: 123\nname: Alice\nactive: true"
//!
//! // Deserialize back
//! let user_back: User = from_str(&toon_string).unwrap();
//! assert_eq!(user, user_back);
//! ```
//!
//! ### Working with Arrays (Tabular Format)
//!
//! Arrays of homogeneous objects automatically serialize as space-efficient tables:
//!
//! ```rust
//! use serde::{Deserialize, Serialize};
//! use serde_toon::to_string;
//!
//! #[derive(Serialize, Deserialize)]
//! struct Product {
//! id: u32,
//! name: String,
//! price: f64,
//! }
//!
//! let products = vec![
//! Product { id: 1, name: "Widget".to_string(), price: 9.99 },
//! Product { id: 2, name: "Gadget".to_string(), price: 14.99 },
//! ];
//!
//! let toon = to_string(&products).unwrap();
//! // Output: "[2]{id,name,price}:\n 1,Widget,9.99\n 2,Gadget,14.99"
//! ```
//!
//! ### Dynamic Values with toon! Macro
//!
//! ```rust
//! use serde_toon::{toon, Value};
//!
//! let data = toon!({
//! "name": "Alice",
//! "age": 30,
//! "tags": ["rust", "serde", "llm"]
//! });
//!
//! if let Value::Object(obj) = data {
//! assert_eq!(obj.get("name").and_then(|v| v.as_str()), Some("Alice"));
//! }
//! ```
//!
//! ## Performance Characteristics
//!
//! - **Serialization**: O(n) where n is the number of fields/elements
//! - **Deserialization**: O(n) with single-pass parsing
//! - **Memory**: Pre-allocated buffers minimize reallocations
//! - **Token Count**: 30-60% reduction vs JSON for typical structured data
//!
//! ## Safety Guarantees
//!
//! - No `unsafe` code blocks
//! - All array indexing is bounds-checked
//! - Proper error propagation with `Result` types
//! - No panics in public API (except for logic errors that indicate bugs)
//!
//! ## Format Specification
//!
//! For the complete TOON format specification, see the [`spec`] module documentation.
//!
//! External reference: <https://github.com/johannschopplich/toon>
//!
//! ## Examples
//!
//! See the `examples/` directory for focused, production-ready examples:
//!
//! - **`simple.rs`** - Your first TOON experience (basic serialization)
//! - **`macro.rs`** - Building values with the toon! macro
//! - **`tabular_arrays.rs`** - TOON's tabular feature for repeated structures
//! - **`dynamic_values.rs`** - Working with Value dynamically
//! - **`custom_options.rs`** - Customizing delimiters and formatting
//! - **`token_efficiency.rs`** - TOON vs JSON comparison
//!
//! Run any example with: `cargo run --example <name>`
pub use Deserializer;
pub use ;
pub use ToonMap;
pub use ;
pub use ;
pub use ;
use ;
use io;
/// Serialize any `T: Serialize` to a TOON string.
///
/// # Examples
///
/// ```rust
/// use serde_toon::to_string;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Point { x: i32, y: i32 }
///
/// let point = Point { x: 1, y: 2 };
/// let toon = to_string(&point).unwrap();
/// ```
///
/// # Errors
///
/// Returns an error if the value cannot be serialized (e.g., unsupported types).
/// Serialize any `T: Serialize` to a pretty-printed TOON string.
///
/// Pretty-printing adds newlines and indentation for readability.
///
/// # Examples
///
/// ```rust
/// use serde_toon::to_string_pretty;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Point { x: i32, y: i32 }
///
/// let point = Point { x: 1, y: 2 };
/// let toon = to_string_pretty(&point).unwrap();
/// ```
///
/// # Errors
///
/// Returns an error if the value cannot be serialized.
/// Serialize any `T: Serialize` to a TOON string with custom options.
///
/// Allows customization of delimiters, indentation, and length markers.
///
/// # Examples
///
/// ```rust
/// use serde_toon::{to_string_with_options, ToonOptions, Delimiter};
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Point { x: i32, y: i32 }
///
/// let point = Point { x: 1, y: 2 };
/// let options = ToonOptions::new()
/// .with_delimiter(Delimiter::Tab)
/// .with_length_marker('#');
/// let toon = to_string_with_options(&point, options).unwrap();
/// ```
///
/// # Errors
///
/// Returns an error if the value cannot be serialized.
/// Convert any `T: Serialize` to a `Value`.
///
/// Useful for working with TOON data dynamically when the structure isn't known at compile time.
///
/// # Examples
///
/// ```rust
/// use serde_toon::{to_value, Value};
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct Point { x: i32, y: i32 }
///
/// let point = Point { x: 1, y: 2 };
/// let value: Value = to_value(&point).unwrap();
/// assert!(value.is_object());
/// ```
///
/// # Errors
///
/// Returns an error if the value cannot be serialized.
/// Serialize any `T: Serialize` to a writer in TOON format.
///
/// # Examples
///
/// ```rust
/// use serde_toon::to_writer;
/// use serde::Serialize;
/// use std::io::Cursor;
///
/// #[derive(Serialize)]
/// struct Point { x: i32, y: i32 }
///
/// let point = Point { x: 1, y: 2 };
/// let mut buffer = Vec::new();
/// to_writer(&mut buffer, &point).unwrap();
/// ```
///
/// # Errors
///
/// Returns an error if serialization fails or writing to the writer fails.
/// Serialize any `T: Serialize` to a writer in TOON format with custom options.
///
/// # Errors
///
/// Returns an error if serialization fails or writing to the writer fails.
/// Deserialize an instance of type `T` from a string of TOON text.
///
/// # Examples
///
/// ```rust
/// use serde_toon::from_str;
/// use serde::Deserialize;
///
/// #[derive(Deserialize, PartialEq, Debug)]
/// struct Point { x: i32, y: i32 }
///
/// let toon = "x: 1\ny: 2";
/// let point: Point = from_str(toon).unwrap();
/// assert_eq!(point, Point { x: 1, y: 2 });
/// ```
///
/// # Errors
///
/// Returns an error if the input is not valid TOON format or cannot be deserialized to type `T`.
/// Error messages include line and column information.
/// Deserialize an instance of type `T` from an I/O stream of TOON.
///
/// # Examples
///
/// ```rust
/// use serde_toon::from_reader;
/// use serde::Deserialize;
/// use std::io::Cursor;
///
/// #[derive(Deserialize, PartialEq, Debug)]
/// struct Point { x: i32, y: i32 }
///
/// let toon_bytes = b"x: 1\ny: 2";
/// let cursor = Cursor::new(toon_bytes);
/// let point: Point = from_reader(cursor).unwrap();
/// assert_eq!(point, Point { x: 1, y: 2 });
/// ```
///
/// # Errors
///
/// Returns an error if reading from the reader fails, the input is not valid TOON,
/// or the data cannot be deserialized to type `T`.
/// Deserialize an instance of type `T` from bytes of TOON text.
///
/// # Examples
///
/// ```rust
/// use serde_toon::from_slice;
/// use serde::Deserialize;
///
/// #[derive(Deserialize, PartialEq, Debug)]
/// struct Point { x: i32, y: i32 }
///
/// let toon_bytes = b"x: 1\ny: 2";
/// let point: Point = from_slice(toon_bytes).unwrap();
/// assert_eq!(point, Point { x: 1, y: 2 });
/// ```
///
/// # Errors
///
/// Returns an error if the bytes are not valid UTF-8, not valid TOON format,
/// or cannot be deserialized to type `T`.