#toml

toml_edit

Yet another format-preserving TOML parser

78 releases

0.22.6 Feb 16, 2024
0.21.1 Jan 31, 2024
0.21.0 Nov 6, 2023
0.19.14 Jul 14, 2023
0.1.1 Dec 17, 2017

#7 in Encoding

Download history 1181302/week @ 2023-11-05 1274726/week @ 2023-11-12 1141182/week @ 2023-11-19 1319607/week @ 2023-11-26 1425601/week @ 2023-12-03 1353791/week @ 2023-12-10 1253332/week @ 2023-12-17 760045/week @ 2023-12-24 1167231/week @ 2023-12-31 1453760/week @ 2024-01-07 1511488/week @ 2024-01-14 1618292/week @ 2024-01-21 1818373/week @ 2024-01-28 1759145/week @ 2024-02-04 1764197/week @ 2024-02-11 1680592/week @ 2024-02-18

7,125,499 downloads per month
Used in 7,414 crates (296 directly)

MIT/Apache

370KB
9K SLoC

toml_edit

Build Status codecov crates.io docs Join the chat at https://gitter.im/toml_edit/Lobby

This crate allows you to parse and modify toml documents, while preserving comments, spaces and relative order of items.

toml_edit is primarily tailored for cargo-edit needs.

Example

use toml_edit::{Document, value};

fn main() {
    let toml = r#"
"hello" = 'toml!' # comment
['a'.b]
    "#;
    let mut doc = toml.parse::<Document>().expect("invalid doc");
    assert_eq!(doc.to_string(), toml);
    // let's add a new key/value pair inside a.b: c = {d = "hello"}
    doc["a"]["b"]["c"]["d"] = value("hello");
    // autoformat inline table a.b.c: { d = "hello" }
    doc["a"]["b"]["c"].as_inline_table_mut().map(|t| t.fmt());
    let expected = r#"
"hello" = 'toml!' # comment
['a'.b]
c = { d = "hello" }
    "#;
    assert_eq!(doc.to_string(), expected);
}

Limitations

Things it does not preserve:

  • Order of dotted keys, see issue.

License

Licensed under either of

Contribution

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

Dependencies

~0.7–1.3MB
~23K SLoC