89 releases

new 0.22.17 Jul 25, 2024
0.22.14 Jun 3, 2024
0.22.9 Mar 20, 2024
0.21.0 Nov 6, 2023
0.1.1 Dec 17, 2017

#60 in Encoding

Download history 2111322/week @ 2024-04-04 2061755/week @ 2024-04-11 2115495/week @ 2024-04-18 2046633/week @ 2024-04-25 2079010/week @ 2024-05-02 2074848/week @ 2024-05-09 2150095/week @ 2024-05-16 2078934/week @ 2024-05-23 2304243/week @ 2024-05-30 2215270/week @ 2024-06-06 2252012/week @ 2024-06-13 2193214/week @ 2024-06-20 2223852/week @ 2024-06-27 2195798/week @ 2024-07-04 2385805/week @ 2024-07-11 2035215/week @ 2024-07-18

9,238,884 downloads per month
Used in 8,819 crates (348 directly)

MIT/Apache

380KB
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
~22K SLoC