| 1 |
//! # `toml_edit` |
| 2 |
//! |
| 3 |
//! This crate allows you to parse and modify toml |
| 4 |
//! documents, while preserving comments, spaces *and |
| 5 |
//! relative order* of items. |
| 6 |
//! |
| 7 |
//! If you also need the ease of a more traditional API, see the [`toml`] crate. |
| 8 |
//! |
| 9 |
//! # Example |
| 10 |
//! |
| 11 |
//! ```rust |
| 12 |
//! # #[cfg(feature = "parse")] { |
| 13 |
//! # #[cfg(feature = "display")] { |
| 14 |
//! use toml_edit::{DocumentMut, value}; |
| 15 |
//! |
| 16 |
//! let toml = r#" |
| 17 |
//! "hello" = 'toml!' # comment |
| 18 |
//! ['a'.b] |
| 19 |
//! "#; |
| 20 |
//! let mut doc = toml.parse::<DocumentMut>().expect("invalid doc"); |
| 21 |
//! assert_eq!(doc.to_string(), toml); |
| 22 |
//! // let's add a new key/value pair inside a.b: c = {d = "hello"} |
| 23 |
//! doc["a"]["b"]["c"]["d"] = value("hello"); |
| 24 |
//! // autoformat inline table a.b.c: { d = "hello" } |
| 25 |
//! doc["a"]["b"]["c"].as_inline_table_mut().map(|t| t.fmt()); |
| 26 |
//! let expected = r#" |
| 27 |
//! "hello" = 'toml!' # comment |
| 28 |
//! ['a'.b] |
| 29 |
//! c = { d = "hello" } |
| 30 |
//! "#; |
| 31 |
//! assert_eq!(doc.to_string(), expected); |
| 32 |
//! # } |
| 33 |
//! # } |
| 34 |
//! ``` |
| 35 |
//! |
| 36 |
//! ## Controlling formatting |
| 37 |
//! |
| 38 |
//! By default, values are created with default formatting |
| 39 |
//! ```rust |
| 40 |
//! # #[cfg(feature = "display")] { |
| 41 |
//! # #[cfg(feature = "parse")] { |
| 42 |
//! let mut doc = toml_edit::DocumentMut::new(); |
| 43 |
//! doc["foo"] = toml_edit::value("bar"); |
| 44 |
//! let expected = r#"foo = "bar" |
| 45 |
//! "#; |
| 46 |
//! assert_eq!(doc.to_string(), expected); |
| 47 |
//! # } |
| 48 |
//! # } |
| 49 |
//! ``` |
| 50 |
//! |
| 51 |
//! You can choose a custom TOML representation by parsing the value. |
| 52 |
//! ```rust |
| 53 |
//! # #[cfg(feature = "display")] { |
| 54 |
//! # #[cfg(feature = "parse")] { |
| 55 |
//! let mut doc = toml_edit::DocumentMut::new(); |
| 56 |
//! doc["foo"] = "'bar'".parse::<toml_edit::Item>().unwrap(); |
| 57 |
//! let expected = r#"foo = 'bar' |
| 58 |
//! "#; |
| 59 |
//! assert_eq!(doc.to_string(), expected); |
| 60 |
//! # } |
| 61 |
//! # } |
| 62 |
//! ``` |
| 63 |
//! |
| 64 |
//! ## Limitations |
| 65 |
//! |
| 66 |
//! Things it does not preserve: |
| 67 |
//! |
| 68 |
//! * Order of dotted keys, see [issue](https://github.com/toml-rs/toml/issues/163). |
| 69 |
//! |
| 70 |
//! [`toml`]: https://docs.rs/toml/latest/toml/ |
| 71 |
|
| 72 |
// https://github.com/Marwes/combine/issues/172 |
| 73 |
#![recursion_limit = "256"] |
| 74 |
#![cfg_attr(docsrs, feature(doc_cfg))] |
| 75 |
#![warn(missing_docs)] |
| 76 |
#![warn(clippy::print_stderr)] |
| 77 |
#![warn(clippy::print_stdout)] |
| 78 |
|
| 79 |
mod array; |
| 80 |
mod array_of_tables; |
| 81 |
mod document; |
| 82 |
#[cfg(feature = "display")] |
| 83 |
mod encode; |
| 84 |
mod error; |
| 85 |
mod index; |
| 86 |
mod inline_table; |
| 87 |
mod item; |
| 88 |
mod key; |
| 89 |
#[cfg(feature = "parse")] |
| 90 |
mod parser; |
| 91 |
mod raw_string; |
| 92 |
mod repr; |
| 93 |
mod table; |
| 94 |
mod value; |
| 95 |
|
| 96 |
#[cfg(feature = "serde")] |
| 97 |
pub mod de; |
| 98 |
#[cfg(feature = "serde")] |
| 99 |
pub mod ser; |
| 100 |
|
| 101 |
pub mod visit; |
| 102 |
pub mod visit_mut; |
| 103 |
|
| 104 |
pub use crate::array::{Array, ArrayIntoIter, ArrayIter, ArrayIterMut}; |
| 105 |
pub use crate::array_of_tables::{ |
| 106 |
ArrayOfTables, ArrayOfTablesIntoIter, ArrayOfTablesIter, ArrayOfTablesIterMut, |
| 107 |
}; |
| 108 |
pub use crate::document::DocumentMut; |
| 109 |
/// Type representing a parsed TOML document |
| 110 |
#[deprecated(since = "0.23.0", note = "Replaced with `Document`")] |
| 111 |
pub type ImDocument<S> = Document<S>; |
| 112 |
pub use crate::document::Document; |
| 113 |
pub use crate::error::TomlError; |
| 114 |
pub use crate::inline_table::{ |
| 115 |
InlineEntry, InlineOccupiedEntry, InlineTable, InlineTableIntoIter, InlineTableIter, |
| 116 |
InlineTableIterMut, InlineVacantEntry, |
| 117 |
}; |
| 118 |
pub use crate::item::{Item, array, table, value}; |
| 119 |
pub use crate::key::{Key, KeyMut}; |
| 120 |
pub use crate::raw_string::RawString; |
| 121 |
pub use crate::repr::{Decor, Formatted, Repr}; |
| 122 |
pub use crate::table::{ |
| 123 |
Entry, IntoIter, Iter, IterMut, OccupiedEntry, Table, TableLike, VacantEntry, |
| 124 |
}; |
| 125 |
pub use crate::value::Value; |
| 126 |
pub use toml_datetime::*; |
| 127 |
|
| 128 |
// Prevent users from some traits. |
| 129 |
pub(crate) mod private { |
| 130 |
pub trait Sealed {} |
| 131 |
impl Sealed for usize {} |
| 132 |
impl Sealed for str {} |
| 133 |
impl Sealed for String {} |
| 134 |
impl Sealed for i64 {} |
| 135 |
impl Sealed for f64 {} |
| 136 |
impl Sealed for bool {} |
| 137 |
impl Sealed for crate::Datetime {} |
| 138 |
impl<T: ?Sized> Sealed for &T where T: Sealed {} |
| 139 |
impl Sealed for crate::Table {} |
| 140 |
impl Sealed for crate::InlineTable {} |
| 141 |
} |
| 142 |
|
| 143 |
#[doc = include_str!("../README.md")] |
| 144 |
#[cfg(doctest)] |
| 145 |
#[cfg(feature = "display")] |
| 146 |
#[cfg(feature = "parse")] |
| 147 |
pub struct ReadmeDoctests; |