1 | //! # `toml_edit` |
2 | //! |
3 | //! This crate allows you to parse and modify toml |
4 | //! documents, while preserving comments, spaces *and |
5 | //! relative order* or 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_auto_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 internal_string; |
88 | mod item; |
89 | mod key; |
90 | #[cfg (feature = "parse" )] |
91 | mod parser; |
92 | mod raw_string; |
93 | mod repr; |
94 | mod table; |
95 | mod value; |
96 | |
97 | #[cfg (feature = "serde" )] |
98 | pub mod de; |
99 | #[cfg (feature = "serde" )] |
100 | pub mod ser; |
101 | |
102 | pub mod visit; |
103 | pub mod visit_mut; |
104 | |
105 | pub use crate::array::{Array, ArrayIntoIter, ArrayIter, ArrayIterMut}; |
106 | pub use crate::array_of_tables::{ |
107 | ArrayOfTables, ArrayOfTablesIntoIter, ArrayOfTablesIter, ArrayOfTablesIterMut, |
108 | }; |
109 | /// Deprecated, replaced with [`DocumentMut`] |
110 | #[deprecated (since = "0.22.6" , note = "Replaced with `DocumentMut`" )] |
111 | pub type Document = DocumentMut; |
112 | pub use crate::document::DocumentMut; |
113 | pub use crate::document::ImDocument; |
114 | pub use crate::error::TomlError; |
115 | pub use crate::inline_table::{ |
116 | InlineEntry, InlineOccupiedEntry, InlineTable, InlineTableIntoIter, InlineTableIter, |
117 | InlineTableIterMut, InlineVacantEntry, |
118 | }; |
119 | pub use crate::internal_string::InternalString; |
120 | pub use crate::item::{array, table, value, Item}; |
121 | pub use crate::key::{Key, KeyMut}; |
122 | pub use crate::raw_string::RawString; |
123 | pub use crate::repr::{Decor, Formatted, Repr}; |
124 | pub use crate::table::{ |
125 | Entry, IntoIter, Iter, IterMut, OccupiedEntry, Table, TableLike, VacantEntry, |
126 | }; |
127 | pub use crate::value::Value; |
128 | pub use toml_datetime::*; |
129 | |
130 | // Prevent users from some traits. |
131 | pub(crate) mod private { |
132 | pub trait Sealed {} |
133 | impl Sealed for usize {} |
134 | impl Sealed for str {} |
135 | impl Sealed for String {} |
136 | impl Sealed for i64 {} |
137 | impl Sealed for f64 {} |
138 | impl Sealed for bool {} |
139 | impl Sealed for crate::Datetime {} |
140 | impl<T: ?Sized> Sealed for &T where T: Sealed {} |
141 | impl Sealed for crate::Table {} |
142 | impl Sealed for crate::InlineTable {} |
143 | } |
144 | |