#toml #table #string

no-std toml_write

A low-level interface for writing out TOML

2 releases

new 0.1.1 Apr 28, 2025
0.1.0 Apr 25, 2025

#182 in Encoding

Download history 111381/week @ 2025-04-21

115,232 downloads per month
Used in 419 crates (2 directly)

MIT/Apache

32KB
704 lines

A low-level interface for writing out TOML

Considerations when serializing arbitrary data:

  • Verify the implementation with toml-test-harness
  • Be sure to group keys under a table before writing another table
  • Watch for extra trailing newlines and leading newlines, both when starting with top-level keys or a table
  • When serializing an array-of-tables, be sure to verify that all elements of the array serialize as tables
  • Standard tables and inline tables may need separate implementations of corner cases, requiring verifying them both

When serializing Rust data structures

  • Option: Skip key-value pairs with a value of None, otherwise error when seeing None
    • When skipping key-value pairs, be careful that a deeply nested None doesn't get skipped
  • Scalars and arrays are unsupported as top-level data types
  • Tuples and tuple variants seriallize as arrays
  • Structs, struct variants, and maps serialize as tables
  • Newtype variants serialize as to the inner type
  • Unit variants serialize to a string
  • Unit and unit structs don't have a clear meaning in TOML

Example

use toml_write::TomlWrite as _;

let mut output = String::new();
output.newline()?;
output.open_table_header()?;
output.key("table")?;
output.close_table_header()?;
output.newline()?;

output.key("key")?;
output.space()?;
output.keyval_sep()?;
output.space()?;
output.value("value")?;
output.newline()?;

assert_eq!(output, r#"
[table]
key = "value"
"#);

toml_write

Latest Version Documentation

A low-level interface for writing out TOML

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in toml-rs by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps