1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//! INI files (and git's config files) for deser.
//!
//! ```rust
//! use deser::{Deserialize, Serialize};
//!
//! #[derive(Debug, Deserialize, Serialize)]
//! struct Config {
//! name: String,
//! server: Server,
//! }
//!
//! #[derive(Debug, Deserialize, Serialize)]
//! struct Server {
//! host: String,
//! port: u16,
//! #[deser(default)]
//! tags: Vec<String>,
//! }
//!
//! let config: Config = deser_ini::from_str("
//! name = shop
//!
//! [server]
//! host = localhost ; the host to bind to
//! port = 8080
//! tags = a
//! tags = b
//! ").unwrap();
//! assert_eq!(config.name, "shop");
//! assert_eq!(config.server.port, 8080);
//! assert_eq!(config.server.tags, ["a", "b"]);
//!
//! assert_eq!(
//! deser_ini::to_string(&config).unwrap(),
//! "name = shop\n\n[server]\nhost = localhost\nport = 8080\ntags = a\ntags = b\n"
//! );
//! ```
//!
//! # Dialects
//!
//! INI files have no specification, every implementation reads them a
//! little differently. The default ([`DeserializerConfig::new`]) reads
//! what is common today:
//!
//! * Lines that start with `;` or `#` are comments. After values, a `;`
//! that follows whitespace starts a comment and so does a `#` that follows
//! whitespace after the value (`key = value ; comment`), so `1;2;3`,
//! `A;B;` and `#ff0000` are values (see [`InlineComments`]).
//! * `=` and `:` separate keys and values, whitespace around keys and
//! values is removed.
//! * Lines that are indented more than the line of their key continue its
//! value (see [`Continuation`]), like in Python's `configparser`:
//!
//! ```ini
//! [options]
//! install_requires =
//! deser
//! requests
//! ```
//! * A value that is quoted as a whole (`"value"` or `'value'`) is read
//! without the quotes (see [`Quotes`]). This is how whitespace at the
//! start or end of a value and comment characters are written.
//! * A key without value (`skip-name-resolve`) is a key with a null value.
//! * Keys can come before the first section.
//!
//! These choices are based on a survey of INI files on GitHub: values are
//! quoted for PHP, MySQL, Windows, Unreal and most other readers that are
//! not Python, values with indented continuation lines are common in Python
//! tools (`tox.ini`, `setup.cfg`, `pylintrc`) and PlatformIO, and keys are
//! rarely indented more than the key before them.
//! [`DeserializerConfig::python`] reads files of Python's `configparser`
//! (no inline comments and quotes) and [`DeserializerConfig::git`] reads
//! git's config files ([`Syntax::Git`]).
//!
//! # Data Model
//!
//! An INI file is a map of its sections and the keys before the first
//! section, sections are maps of their keys:
//!
//! | INI file | deser |
//! |------------------------------------|-------------------------------------|
//! | `a = 1` | `{"a": "1"}` |
//! | `[s]` `a = 1` | `{"s": {"a": "1"}}` |
//! | `[s]` `a = 1` `a = 2` | `{"s": {"a": "1", "a": "2"}}` (a repeated key) |
//! | `[s]` `a = 1` `[s]` `b = 2` | `{"s": {"a": "1", "b": "2"}}` |
//! | `[s]` `a` | `{"s": {"a": null}}` |
//! | `[s]` | `{"s": {}}` |
//! | `[s "x"]` `a = 1` (git) | `{"s": {"x": {"a": "1"}}}` |
//!
//! Sections that are given more than once are merged. Keys can repeat
//! (the maps are [multimaps]): fields and map values that are collections
//! (like `Vec<T>`) collect the values of all occurrences of their key, also
//! if other keys are between them. Types that expect a single value receive
//! the last one unless the [`Context`](deser_core::Context) has another
//! [`DuplicateKeys`](deser_core::de::DuplicateKeys) policy. A name that is
//! a key and a section is an error.
//!
//! Everything in an INI file is text, the type of a value is only known to
//! the type it's deserialized into. Keys and values are therefore passed
//! on as [lexical atoms](deser_core::Atom::Lexical) which are parsed by the
//! types they are delivered to: numbers parse them, strings take them as
//! they are. Booleans accept `true`, `yes`, `on` and `1` and `false`, `no`,
//! `off` and `0`. Empty values are `None` for optionals of types that do
//! not accept them (`port =` is `None` for an `Option<u16>` and `Some("")`
//! for an `Option<String>`). Keys without value are null, the
//! [`Flag`](deser_core::adapters::Flag) adapter reads them as `true`:
//!
//! ```rust
//! use deser::adapters::Flag;
//!
//! #[derive(deser::Deserialize)]
//! struct Mysqld {
//! #[deser(as = Flag)]
//! skip_name_resolve: bool,
//! port: Option<u16>,
//! }
//!
//! #[derive(deser::Deserialize)]
//! struct MyCnf {
//! mysqld: Mysqld,
//! }
//!
//! let cnf: MyCnf =
//! deser_ini::from_str("[mysqld]\nskip_name_resolve\nport =\n").unwrap();
//! assert!(cnf.mysqld.skip_name_resolve);
//! assert_eq!(cnf.mysqld.port, None);
//! ```
//!
//! Lists in a single value (`hosts = a, b`) use the
//! [`Separated`](deser_core::adapters::Separated) adapter, values on
//! continuation lines are separated by line breaks
//! (`Separated<'\n'>`).
//!
//! Interpolation (`%(name)s`, `${name}`) and the `DEFAULT` section of
//! `configparser` are not supported, they are text.
//!
//! [multimaps]: deser_core::ContainerShape::set_multimap
//!
//! # Errors
//!
//! Errors point at the line and column of the input, also errors of values
//! that do not parse (like `port = http` for a `u16`). With `deser-path`
//! they also have the path of the value (`server.port`), see
//! [`Deserializer`].
//!
//! # Serialization
//!
//! Values are written as INI files (see [`SerializerConfig`]): the value
//! has to be a map (like a struct), its entries with maps as values are
//! sections and the others are written before the first section.
//! Sequences are written as repeated keys.
//!
//! # Streams
//!
//! INI files are read from a [`Read`](std::io::Read) with [`from_reader`]
//! and written to a [`Write`](std::io::Write) with [`to_writer`]. The
//! configurations also create readers and writers of
//! [`deser::io`](deser_core::io) ([`DeserializerConfig::reader`] and
//! [`SerializerConfig::writer`]). An INI file holds a single value, the
//! whole stream is read before it's parsed.
//!
//! # Features
//!
//! * `io` (enabled by default): reading and writing streams of the
//! standard library, see [streams](#streams).
//! * `speedups` (enabled by default): has no effect yet, it exists so
//! that all formats have it.
pub use ;
pub use to_writer;
pub use ;
pub use StreamDeserializer;
pub use from_reader;
use Error;
use Deserialize;
/// The syntax of the files.
///
/// ```
/// use std::collections::BTreeMap;
/// use deser_ini::{DeserializerConfig, Syntax};
///
/// type Config = BTreeMap<String, BTreeMap<String, String>>;
///
/// let git = DeserializerConfig::builder().syntax(Syntax::Git).build();
/// let config: Config = git
/// .from_str("[Alias]\n\tLG = \"log --graph\" # short\n")
/// .unwrap();
/// assert_eq!(config["alias"]["lg"], "log --graph");
/// ```
/// Where comments start after values.
///
/// Lines that start with `;` or `#` (after whitespace) are always comments.
///
/// ```
/// use std::collections::BTreeMap;
/// use deser_ini::{DeserializerConfig, InlineComments};
///
/// let input = "a = 1;2 ; one\nb = x #y\nc = #fff";
/// let with = |comments| -> BTreeMap<String, String> {
/// DeserializerConfig::builder()
/// .inline_comments(comments)
/// .build()
/// .from_str(input)
/// .unwrap()
/// };
/// let values = with(InlineComments::AfterWhitespace);
/// assert_eq!((&*values["a"], &*values["b"], &*values["c"]), ("1;2", "x", "#fff"));
/// let values = with(InlineComments::None);
/// assert_eq!((&*values["a"], &*values["b"], &*values["c"]), ("1;2 ; one", "x #y", "#fff"));
/// let values = with(InlineComments::Anywhere);
/// assert_eq!((&*values["a"], &*values["b"], &*values["c"]), ("1", "x", ""));
/// ```
/// How values continue on the next lines.
///
/// ```
/// use std::collections::BTreeMap;
/// use deser_ini::{Continuation, DeserializerConfig};
///
/// let config = DeserializerConfig::new();
/// let value: BTreeMap<String, String> =
/// config.from_str("deps =\n a\n b\nnext = 1").unwrap();
/// assert_eq!(value["deps"], "a\nb");
///
/// let config =
/// DeserializerConfig::builder().continuation(Continuation::Backslash).build();
/// let value: BTreeMap<String, String> =
/// config.from_str("cmd = a \\\n b").unwrap();
/// assert_eq!(value["cmd"], "a b");
/// ```
/// How quoted values are read.
///
/// ```
/// use std::collections::BTreeMap;
/// use deser_ini::{DeserializerConfig, Quotes};
///
/// let input = "a = \" x ; y \" ; comment\nb = \"say \\\"hi\\\"\"\nc = \"a\" \"b\"";
/// let values: BTreeMap<String, String> = deser_ini::from_str(input).unwrap();
/// assert_eq!(values["a"], " x ; y ");
/// assert_eq!(values["b"], "say \"hi\"");
/// assert_eq!(values["c"], "\"a\" \"b\"");
///
/// let config = DeserializerConfig::builder().quotes(Quotes::None).build();
/// let values: BTreeMap<String, String> = config.from_str("a = \"x\"").unwrap();
/// assert_eq!(values["a"], "\"x\"");
/// ```
/// Deserializes a value from an INI file.
///
/// This uses the default [`DeserializerConfig`], see the [crate
/// documentation](crate) for how INI files map onto deser.
///
/// ```
/// use std::collections::BTreeMap;
///
/// let value: BTreeMap<String, BTreeMap<String, u32>> =
/// deser_ini::from_str("[a]\nb = 1\nc = 2").unwrap();
/// assert_eq!(value["a"]["c"], 2);
/// ```
/// Deserializes a value from an INI file in a byte slice.
///
/// The input has to be UTF-8, a byte order mark is skipped.
///
/// ```
/// use std::collections::BTreeMap;
///
/// let value: BTreeMap<String, String> =
/// deser_ini::from_slice(b"\xef\xbb\xbfa = \xc3\xa4").unwrap();
/// assert_eq!(value["a"], "\u{e4}");
/// ```
// the examples of the readme are tested
;