83 releases

new 0.22.11 Apr 19, 2024
0.22.9 Mar 20, 2024
0.21.0 Nov 6, 2023
0.19.14 Jul 14, 2023
0.1.1 Dec 17, 2017

#62 in Encoding

Download history 761492/week @ 2023-12-23 1145405/week @ 2023-12-30 1450867/week @ 2024-01-06 1500246/week @ 2024-01-13 1623848/week @ 2024-01-20 1798179/week @ 2024-01-27 1761901/week @ 2024-02-03 1757219/week @ 2024-02-10 1790886/week @ 2024-02-17 1897443/week @ 2024-02-24 1902326/week @ 2024-03-02 1930599/week @ 2024-03-09 2030018/week @ 2024-03-16 1988490/week @ 2024-03-23 2077707/week @ 2024-03-30 1736731/week @ 2024-04-06

8,152,207 downloads per month
Used in 7,256 crates (324 directly)

MIT/Apache

375KB
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