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
//! High-performance, fast compiling, TOML serialization and deserialization library for
//! rust with full compliance with the TOML 1.1 spec.
//!
//! # Parsing and Traversal
//!
//! Use [`parse`] with a TOML string and an [`Arena`] to get a [`Document`].
//! ```
//! let arena = toml_spanner::Arena::new();
//! let doc = toml_spanner::parse("key = 'value'", &arena).unwrap();
//! ```
//! Traverse the tree via index operators, which return a [`MaybeItem`]:
//! ```
//! # let arena = toml_spanner::Arena::new();
//! # let doc = toml_spanner::parse("", &arena).unwrap();
//! let name: Option<&str> = doc["name"].as_str();
//! let numbers: Option<i64> = doc["numbers"][50].as_i64();
//! ```
//! Use [`MaybeItem::item()`] to get an [`Item`] containing a [`Value`] and [`Span`].
//! ```rust
//! # use toml_spanner::{Value, Span};
//! # let arena = toml_spanner::Arena::new();
//! # let doc = toml_spanner::parse("item = 0", &arena).unwrap();
//! let Some(item) = doc["item"].item() else {
//! panic!("Missing key `item`");
//! };
//! match item.value() {
//! Value::String(string) => {},
//! Value::Integer(integer) => {}
//! Value::Float(float) => {},
//! Value::Boolean(boolean) => {},
//! Value::Array(array) => {},
//! Value::Table(table) => {},
//! Value::DateTime(date_time) => {},
//! }
//! // Get byte offset of where item was defined in the source.
//! let Span{start, end} = item.span();
//! ```
//!
//! ## Deserialization
//!
//! [`Document::table_helper()`] creates a [`TableHelper`] for type-safe field extraction
//! via [`FromToml`]. Errors accumulate in the [`Document`]'s context rather than
//! failing on the first error.
//!
//! ```
//! # let arena = toml_spanner::Arena::new();
//! # let mut doc = toml_spanner::parse("name = 'hello'", &arena).unwrap();
//! let mut helper = doc.table_helper();
//! let name: Option<String> = helper.optional("name");
//! ```
//!
//! [`Item::parse`] extracts values from string items via [`std::str::FromStr`].
//!
//! ```
//! # fn main() -> Result<(), toml_spanner::Error> {
//! # let arena = toml_spanner::Arena::new();
//! # let doc = toml_spanner::parse("ip-address = '127.0.0.1'", &arena).unwrap();
//! let item = doc["ip-address"].item().unwrap();
//! let ip: std::net::Ipv4Addr = item.parse()?;
//! # Ok(())
//! # }
//! ```
//!
//! <details>
//! <summary>Toggle More Extensive Example</summary>
//!
//! ```
//! use toml_spanner::{Arena, FromToml, Item, Context, Failed, TableHelper};
//!
//! #[derive(Debug)]
//! struct Things {
//! name: String,
//! value: u32,
//! color: Option<String>,
//! }
//!
//! impl<'de> FromToml<'de> for Things {
//! fn from_toml(ctx: &mut Context<'de>, value: &Item<'de>) -> Result<Self, Failed> {
//! let mut th = value.table_helper(ctx)?;
//! let name = th.required("name")?;
//! let value = th.required("value")?;
//! let color = th.optional("color");
//! th.require_empty()?;
//! Ok(Things { name, value, color })
//! }
//! }
//!
//! let content = r#"
//! dev-mode = true
//!
//! [[things]]
//! name = "hammer"
//! value = 43
//!
//! [[things]]
//! name = "drill"
//! value = 300
//! color = "green"
//! "#;
//!
//! let arena = Arena::new();
//! let mut doc = toml_spanner::parse(content, &arena).unwrap();
//!
//! // Null-coalescing index operators: missing keys return a None-like
//! // MaybeItem instead of panicking.
//! assert_eq!(doc["things"][0]["color"].as_str(), None);
//! assert_eq!(doc["things"][1]["color"].as_str(), Some("green"));
//!
//! // Deserialize typed values out of the document table.
//! let mut helper = doc.table_helper();
//! let things: Vec<Things> = helper.required("things").ok().unwrap();
//! let dev_mode: bool = helper.optional("dev-mode").unwrap_or(false);
//! // Error if unconsumed fields remain.
//! helper.require_empty().ok();
//!
//! assert_eq!(things.len(), 2);
//! assert_eq!(things[0].name, "hammer");
//! assert!(dev_mode);
//! ```
//!
//! </details>
//!
//! ## Derive Macro
//!
//! The [`Toml`] derive macro generates [`FromToml`] and [`ToToml`]
//! implementations. A bare `#[derive(Toml)]` generates [`FromToml`] only.
//! Annotate with `#[toml(Toml)]` for both directions.
//!
//! use toml_spanner::{Arena, Toml};
//!
//! #[derive(Debug, Toml)]
//! #[toml(Toml)]
//! struct Config {
//! name: String,
//! port: u16,
//! #[toml(default)]
//! debug: bool,
//! }
//!
//! let arena = Arena::new();
//! let mut doc = toml_spanner::parse("name = 'app'\nport = 8080", &arena).unwrap();
//! let config = doc.to::<Config>().unwrap();
//! assert_eq!(config.name, "app");
//!
//! let output = toml_spanner::to_string(&config).unwrap();
//! assert!(output.contains("name = \"app\""));
//! ```
//!
//! See the [`Toml`] macro documentation for all supported attributes
//! (`rename`, `default`, `flatten`, `skip`, tagged enums, etc.).
//!
//! ## Serialization
//!
//! Types implementing [`ToToml`] can be written back to TOML text with
//! [`to_string`] or the [`Formatting`] builder for more control.
//!
//! use toml_spanner::{Arena, Formatting};
//! use std::collections::BTreeMap;
//!
//! let mut map = BTreeMap::new();
//! map.insert("key", "value");
//!
//! // Using default formatting.
//! let output = toml_spanner::to_string(&map).unwrap();
//!
//! // Preserve formatting from a parsed document
//! let arena = Arena::new();
//! let doc = toml_spanner::parse("key = \"old\"\n", &arena).unwrap();
//! let output = Formatting::preserved_from(&doc).format(&map).unwrap();
//! ```
//!
//! See [`Formatting`] for indentation, format preservation, and other options.
//!
/// Error sentinel indicating a failure.
///
/// Error details are recorded in the shared [`Context`].
;
pub use Arena;
pub use FromTomlError;
pub use ;
pub use Indent;
use ;
use ;
pub use ;
pub use Array;
pub use ;
pub use Table;
pub use ;
pub use parse_recoverable;
pub use ;
pub use ToTomlError;
pub use ;
pub use ;
pub use ;
pub use Toml;
/// Parses and deserializes a TOML document in one step.
///
/// For borrowing or non-fatal errors, use [`parse`] and [`Document`] methods.
///
/// # Errors
///
/// Returns a [`FromTomlError`] containing all parse or conversion errors
/// encountered.
/// Serializes a [`ToToml`] value into a TOML document string with default formatting.
///
/// The value must serialize to a table at the top level. For format
/// preservation or custom indentation, use [`Formatting`].
///
/// # Errors
///
/// Returns [`ToTomlError`] if serialization fails or the top-level value
/// is not a table.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use toml_spanner::to_string;
///
/// let mut map = BTreeMap::new();
/// map.insert("key", "value");
/// let output = to_string(&map).unwrap();
/// assert!(output.contains("key = \"value\""));
/// ```
/// Controls how TOML output is formatted when serializing.
///
/// [`Formatting::preserved_from`] preserves formatting from a previously
/// parsed document, use `Formatting::default()` for standard formatting.
///
/// # Examples
///
/// ```
/// use toml_spanner::{Arena, Formatting};
/// use std::collections::BTreeMap;
///
/// let arena = Arena::new();
/// let source = "key = \"value\"\n";
/// let doc = toml_spanner::parse(source, &arena).unwrap();
///
/// let mut map = BTreeMap::new();
/// map.insert("key", "updated");
///
/// let output = Formatting::preserved_from(&doc).format(&map).unwrap();
/// assert!(output.contains("key = \"updated\""));
/// ```