README.md
1# toml_edit
2
3[![Build Status](https://github.com/ordian/toml_edit/workflows/Continuous%20integration/badge.svg)](https://github.com/ordian/toml_edit/actions)
4[![codecov](https://codecov.io/gh/ordian/toml_edit/branch/master/graph/badge.svg)](https://codecov.io/gh/ordian/toml_edit)
5[![crates.io](https://img.shields.io/crates/v/toml_edit.svg)](https://crates.io/crates/toml_edit)
6[![docs](https://docs.rs/toml_edit/badge.svg)](https://docs.rs/toml_edit)
7[![Join the chat at https://gitter.im/toml_edit/Lobby](https://badges.gitter.im/a.svg)](https://gitter.im/toml_edit/Lobby)
8
9
10This crate allows you to parse and modify toml
11documents, while preserving comments, spaces* and
12relative order* or items.
13
14`toml_edit` is primarily tailored for [cargo-edit](https://github.com/killercup/cargo-edit/) needs.
15
16## Example
17
18```rust
19extern crate toml_edit;
20
21use toml_edit::{Document, value};
22
23fn main() {
24 let toml = r#"
25"hello" = 'toml!' # comment
26['a'.b]
27 "#;
28 let mut doc = toml.parse::<Document>().expect("invalid doc");
29 assert_eq!(doc.to_string(), toml);
30 // let's add a new key/value pair inside a.b: c = {d = "hello"}
31 doc["a"]["b"]["c"]["d"] = value("hello");
32 // autoformat inline table a.b.c: { d = "hello" }
33 doc["a"]["b"]["c"].as_inline_table_mut().map(|t| t.fmt());
34 let expected = r#"
35"hello" = 'toml!' # comment
36['a'.b]
37c = { d = "hello" }
38 "#;
39 assert_eq!(doc.to_string(), expected);
40}
41```
42
43## Limitations
44
45Things it does not preserve:
46* Different quotes and spaces around the same table key, e.g.
47```toml
48[ 'a'. b]
49[ "a" .c]
50[a.d]
51```
52will be represented as (spaces are removed, the first encountered quote type is used)
53```toml
54['a'.b]
55['a'.c]
56['a'.d]
57```
58* Children tables before parent table (tables are reordered by default, see [test]).
59* Scattered array of tables (tables are reordered by default, see [test]).
60
61The reason behind the first limitation is that `Table` does not store its header,
62allowing us to safely swap two tables (we store a mapping in each table: child key -> child table).
63
64This last two limitations allow us to represent a toml document as a tree-like data structure,
65which enables easier implementation of editing operations
66and an easy to use and type-safe API. If you care about the above two cases, you
67can use `Document::to_string_in_original_order()` to reconstruct tables in their
68original order.
69
70## License
71
72Licensed under either of
73
74- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://apache.org/licenses/LICENSE-2.0)
75- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
76
77### Contribution
78
79Unless 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.
80
81[test]: https://github.com/ordian/toml_edit/blob/f09bd5d075fdb7d2ef8d9bb3270a34506c276753/tests/test_valid.rs#L84
82