#toml

toml_edit

Yet another format-preserving TOML parser

10 releases

new 0.3.1 Sep 14, 2021
0.3.0 Sep 13, 2021
0.2.1 Jun 7, 2021
0.2.0 Jun 18, 2020
0.1.1 Dec 17, 2017

#8 in Parser implementations

Download history 7484/week @ 2021-05-31 11402/week @ 2021-06-07 12366/week @ 2021-06-14 11197/week @ 2021-06-21 11323/week @ 2021-06-28 12128/week @ 2021-07-05 11394/week @ 2021-07-12 12916/week @ 2021-07-19 12777/week @ 2021-07-26 14752/week @ 2021-08-02 13669/week @ 2021-08-09 15144/week @ 2021-08-16 14845/week @ 2021-08-23 14927/week @ 2021-08-30 14898/week @ 2021-09-06 17194/week @ 2021-09-13

57,273 downloads per month
Used in 102 crates (82 directly)

MIT/Apache

275KB
6.5K 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 or items.

toml_edit is primarily tailored for cargo-edit needs.

Example

extern crate toml_edit;

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:

  • Scattered array of tables (tables are reordered by default, see test).
  • 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

~2MB
~39K SLoC