| 1 |
# toml_edit |
| 2 |
|
| 3 |
[](https://docs.rs/toml_edit) |
| 4 |
 |
| 5 |
[](https://crates.io/crates/toml_edit) |
| 6 |
|
| 7 |
|
| 8 |
This crate allows you to parse and modify toml |
| 9 |
documents, while preserving comments, spaces *and |
| 10 |
relative order* of items. |
| 11 |
|
| 12 |
`toml_edit` is primarily tailored for [cargo-edit](https://github.com/killercup/cargo-edit/) needs. |
| 13 |
|
| 14 |
## Example |
| 15 |
|
| 16 |
```rust |
| 17 |
use toml_edit::{DocumentMut, value}; |
| 18 |
|
| 19 |
fn main() { |
| 20 |
let toml = r#" |
| 21 |
"hello" = 'toml!' # comment |
| 22 |
['a'.b] |
| 23 |
"#; |
| 24 |
let mut doc = toml.parse::<DocumentMut>().expect("invalid doc"); |
| 25 |
assert_eq!(doc.to_string(), toml); |
| 26 |
// let's add a new key/value pair inside a.b: c = {d = "hello"} |
| 27 |
doc["a"]["b"]["c"]["d"] = value("hello"); |
| 28 |
// autoformat inline table a.b.c: { d = "hello" } |
| 29 |
doc["a"]["b"]["c"].as_inline_table_mut().map(|t| t.fmt()); |
| 30 |
let expected = r#" |
| 31 |
"hello" = 'toml!' # comment |
| 32 |
['a'.b] |
| 33 |
c = { d = "hello" } |
| 34 |
"#; |
| 35 |
assert_eq!(doc.to_string(), expected); |
| 36 |
} |
| 37 |
``` |
| 38 |
|
| 39 |
## Limitations |
| 40 |
|
| 41 |
Things it does not preserve: |
| 42 |
|
| 43 |
* Order of dotted keys, see [issue](https://github.com/toml-rs/toml/issues/163). |
| 44 |
|
| 45 |
## License |
| 46 |
|
| 47 |
Licensed under either of |
| 48 |
|
| 49 |
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <https://www.apache.org/licenses/LICENSE-2.0>) |
| 50 |
* MIT license ([LICENSE-MIT](LICENSE-MIT) or <https://opensource.org/license/mit>) |
| 51 |
|
| 52 |
at your option. |
| 53 |
|
| 54 |
### Contribution |
| 55 |
|
| 56 |
Unless you explicitly state otherwise, any contribution intentionally |
| 57 |
submitted for inclusion in the work by you, as defined in the Apache-2.0 |
| 58 |
license, shall be dual-licensed as above, without any additional terms or |
| 59 |
conditions. |