serde-libconfigasaurus 0.4.0

Serde serialization and deserialization for libconfig format
Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
Top-level document wrapper for reading and writing libconfig data.

<p align="center">
  <img src="https://gitlab.com/rustographer/serde-libconfigasaurus/-/raw/v0.4.0/assets/v0.4/triceratops.png" alt="triceratops logo" width="300" >
</p>

# **Config API Documentation**

The `Config` struct is the primary entry point for working with libconfig data. It wraps the root `Value::Group` of a configuration document. Per the grammar (`<configuration> ::= <setting-list> | <empty>`), the top level of a document is a list of settings — braces delimit nested groups only.

## Overview

`Config` wraps a private root `Value::Group`. It derefs to `Value`, so all
`Value` methods (indexing, lookup, set, remove, type checks) work directly
on a `Config`; `Config::into_value()` recovers ownership of the root, and
`Config::try_from(value)` wraps an existing group (non-groups are rejected).

`Config` implements `Serialize` and `Deserialize`, so it can itself be used
as a serde target: `libconfig::from_str::<Config>(...)` succeeds only for
documents whose root is a group.

## Creating a Config

### Empty Config

```rust
use libconfig::Config;

let mut config = Config::new();
config.set("name", "My App").unwrap();
config.set("version", 1).unwrap();
config.set("debug", false).unwrap();

assert_eq!(config["name"], "My App");
assert_eq!(config["version"], 1);
```

`Config::new()` creates an empty configuration group. `set` accepts
anything convertible into a `Value` and validates the setting name.

### Parsing a String

```rust
use libconfig::Config;

// A configuration is a list of settings at the top level
let config = Config::from_str("name = \"My App\";\nversion = 1;").unwrap();
assert_eq!(config["name"], "My App");

// Braces around the top level are not part of the grammar
// (<configuration> ::= <setting-list> | <empty>) and are rejected;
// braces delimit nested groups only
assert!(Config::from_str("{ name = \"My App\"; }").is_err());
```

### Parse Errors

Errors carry the failure position (1-based) and a typed kind:

```rust
use libconfig::{Config, ErrorKind};

let err = Config::from_str("a = 1;\nb = @;\n").unwrap_err();
assert_eq!(err.line(), Some(2));
assert_eq!(err.column(), Some(5));
assert_eq!(err.to_string(), "syntax error at line 2, column 5");
assert!(matches!(err.kind(), ErrorKind::Syntax));
```

### Reading a File

```rust ,no_run
use libconfig::Config;

let config = Config::from_file("settings.cfg").unwrap();
println!("{}", config["database.host"]);
```

`Config::from_file` reads the file and parses it with `Config::from_str`.
The inverse, `Config::to_file(path)`, serializes the document and writes it
(with a trailing newline) to the given path:

```rust ,no_run
use libconfig::Config;

let mut config = Config::new();
config.set("name", "svc").unwrap();
config.to_file("settings.cfg").unwrap();
```

### Include Directives

`Config::from_str` and `Config::from_file` process `@include` directives per
section 2.11 of the libconfig manual: each directive is replaced by the
contents of the named file before parsing.

```text
# file: test.cfg
info: {
  name = "Winston Churchill";
  @include "quote.cfg"
  country = "UK";
};
```

Rules, matching the C library:

- A directive must appear on its own line: `@include "path"` (trailing
  comments are allowed). Directives inside `/* ... */` comments are ignored.
- Backslashes and double quotes in the path are escaped as `\\` and `\"`.
- Includes may be nested at most 10 levels deep; exceeding the limit
  produces `ErrorKind::Include`.
- Relative paths are resolved against the current working directory by
  default. To resolve them against a specific directory instead (the
  equivalent of `config_set_include_dir()`), use
  `Config::from_str_with_include_dir` / `Config::from_file_with_include_dir`:

```rust ,no_run
use libconfig::Config;

let config = Config::from_file_with_include_dir("test.cfg", "/etc/myapp").unwrap();
```

For full control (the equivalent of `config_set_include_func()`), a custom
resolver maps each directive path to the ordered list of files to inline —
enabling wildcard expansion, search paths, or variable substitution:

```rust ,no_run
use libconfig::Config;

let config = Config::from_file_with_include_fn("test.cfg", |path| {
    // e.g. expand "conf.d/*.cfg" with your favorite glob crate
    Ok(vec![std::path::PathBuf::from(path)])
}).unwrap();
```

Files produced by the resolver are scanned for nested `@include` directives
(resolved by the same function), up to the 10-level limit.

Note that the serde-level `libconfig::from_str` does **not** process
`@include` directives; include handling is a `Config` feature.

### Wrapping an Existing Value

`TryFrom<Value>` wraps a group value as a document; non-group values are
rejected, since a configuration is a group of settings:

```rust
use libconfig::{Config, Value};

let v = Value::from_str("name = \"test\";").unwrap();
let config = Config::try_from(v).unwrap();
assert_eq!(config["name"], "test");

assert!(Config::try_from(Value::Integer(42)).is_err());
```

### Default

```rust
use libconfig::Config;

let config = Config::default(); // Same as Config::new()
assert!(config.is_group());
assert_eq!(config.as_group().unwrap().len(), 0);
```

## Serialization

### Config::to_string

Serializes the config to a libconfig string. The top level is an implicit
setting list; nested groups are brace-delimited:

```rust
use libconfig::Config;

let config = Config::from_str("name = \"test\";\nversion = 42;").unwrap();
let output = config.to_string().unwrap();
assert_eq!(output, "name = \"test\";\nversion = 42;");
```

### Write Options

`WriteOptions` mirrors the C writer's formatting knobs (manual chapter 3).
Every combination produces grammar-valid output; defaults keep this crate's
canonical style:

```rust
use libconfig::{Config, WriteOptions};

let config = Config::from_str("port = 80; db = { host = \"h\"; };").unwrap();

// The C library's cosmetic style: ':' for groups, '{' on its own line
let opts = WriteOptions::new()
    .colon_assignment_for_groups(true)
    .open_brace_on_separate_line(true);
let text = config.to_string_with_options(&opts).unwrap();
assert_eq!(text, "port = 80;\ndb :\n{\n  host = \"h\";\n};");
```

Available options: `float_precision(n)` (fixed digits; default is the
shortest form that round-trips exactly), `scientific_notation(bool)`,
`tab_width(n)` (0 = tabs), `semicolon_separators(bool)`,
`colon_assignment_for_groups(bool)`, `colon_assignment_for_non_groups(bool)`,
and `open_brace_on_separate_line(bool)`. `Config::to_file_with_options` and
crate-level `to_string_with_options` accept the same struct.

### Display

`Config` implements `Display`, so it can be used with `format!` and `println!`:

```rust
use libconfig::Config;

let config = Config::from_str("x = 10; y = 20;").unwrap();
println!("{}", config);
// x = 10;
// y = 20;
```

### Serde Serialize

`Config` implements `Serialize`. Per the configuration file grammar, the top
level of a document is an implicit setting list, so serde serialization
produces no outer braces (braces only delimit nested groups):

```rust
use libconfig::Config;

let config = Config::from_str("a = 1; b = 2;").unwrap();

// Serde serialization writes the grammar's implicit top-level format
let serde_output = libconfig::to_string(&config).unwrap();
assert_eq!(serde_output, "a = 1;\nb = 2;");

// Config::to_string produces the same implicit format
let format_output = config.to_string().unwrap();
assert!(!format_output.starts_with('{'));
```

## Reading Values

Since `Config` derefs to `Value`, all `Value` access methods work directly:

### Indexing

```rust
use libconfig::Config;

let config = Config::from_str("name = \"test\"; ports = [80, 443];").unwrap();

assert_eq!(config["name"], "test");
assert_eq!(config["ports"][0], 80);
```

### Path Lookup

```rust
use libconfig::Config;

let config = Config::from_str(r#"
    database = {
        host = "localhost";
        credentials = {
            username = "admin";
        };
    };
"#).unwrap();

assert_eq!(
    config.lookup("database.credentials.username").unwrap().as_str(),
    Some("admin")
);

// Bracket notation for array indices
assert_eq!(
    config.lookup("database.host").unwrap().as_str(),
    Some("localhost")
);
```

### Type Checks and Conversions

```rust
use libconfig::Config;

let config = Config::from_str("count = 42; name = \"test\";").unwrap();

assert!(config["count"].is_i32());
assert_eq!(config["count"].as_i32(), Some(42));
assert_eq!(config["name"].as_str(), Some("test"));

// Config itself is always a group
assert!(config.is_group());
```

## Writing Values

### Setting Values

`set` takes anything convertible into a `Value`; explicit `Value` variants
are only needed for aggregates:

```rust
use libconfig::{Config, Value};

let mut config = Config::new();

// Scalars convert automatically
config.set("name", "My App").unwrap();
config.set("version", 1).unwrap();
config.set("ratio", 0.75).unwrap();
config.set("debug", true).unwrap();
config.set("big_number", 100000000000i64).unwrap();

// Arrays and lists are built from Values
config.set("ports", Value::Array(vec![80.into(), 443.into()])).unwrap();

// Intermediate groups are created automatically
config.set("database.host", "localhost").unwrap();
config.set("database.port", 5432).unwrap();

assert_eq!(config["database.host"], "localhost");
assert_eq!(config["database.port"], 5432);
```

`set` returns a `Result`: names are validated against the grammar
(`[A-Za-z*][-A-Za-z0-9_*]*`) and traversal failures are typed:

```rust
use libconfig::{Config, ErrorKind};

let mut config = Config::new();
assert!(matches!(
    config.set("my key", 1).unwrap_err().kind(),
    ErrorKind::InvalidSettingName(_)
));
config.set("scalar", 5).unwrap();
assert!(matches!(
    config.set("scalar.child", 1).unwrap_err().kind(),
    ErrorKind::PathTraversal(_)
));
```

### Modifying Existing Values

```rust
use libconfig::{Config, Value};

let mut config = Config::from_str("name = \"old\"; version = 1;").unwrap();

// Overwrite via set
config.set("name", "new").unwrap();
assert_eq!(config["name"], "new");

// Overwrite via lookup_mut
*config.lookup_mut("version").unwrap() = Value::Integer(2);
assert_eq!(config["version"], 2);
```

### Removing Values

```rust
use libconfig::{Config, Value};

let mut config = Config::from_str("a = 1; b = 2; c = 3;").unwrap();

let removed = config.remove("b");
assert_eq!(removed, Some(Value::Integer(2)));
assert!(config.lookup("b").is_none());
assert_eq!(config["a"], 1);
assert_eq!(config["c"], 3);
```

## Round-Trip Preservation

`Config` preserves key ordering through round-trips:

```rust
use libconfig::Config;

let input = "zebra = 1;\napple = 2;\nmonkey = 3;";
let config = Config::from_str(input).unwrap();
let output = config.to_string().unwrap();
assert_eq!(output, input); // Exact match, order preserved
```

## Indentation

Nested groups are indented with 2 spaces per level by default (see
`WriteOptions::tab_width` to change this):

```rust
use libconfig::Config;

let mut config = Config::new();
config.set("server.listen.host", "0.0.0.0").unwrap();
config.set("server.listen.port", 8080).unwrap();

let output = config.to_string().unwrap();
// server = {
//   listen = {
//     host = "0.0.0.0";
//     port = 8080;
//   };
// };
```

## Config vs Value

| | `Config` | `Value` |
|---|---|---|
| **Purpose** | Document-level wrapper | Individual data values |
| **Root** | Always a group (setting list) | Any value type |
| **`from_str`** | Requires a document (settings) | Also parses bare values |
| **Serialization** | `to_string` / `to_file` (+ `_with_options`) | `libconfig::to_string(&value)` |
| **Construction** | `Config::new()`, `Config::from_str()` | `Value::from(42)`, `"x".into()`, variants |
| **File I/O** | `Config::from_file()` + `@include` | No |

Use `Config` when working with complete configuration documents. Use `Value` variants when constructing individual values to pass to `Config::set()`.