serde-libconfigasaurus
A Rust serde implementation for the libconfig configuration format.
Supported Types
All libconfig value types are supported:
| libconfig type | Rust / Value type |
|---|---|
| boolean | bool / Value::Bool |
| integer | i32 / Value::Integer |
| integer64 | i64 / Value::Integer64 |
| float | f64 / Value::Float |
| string | String / Value::String |
array [...] |
Vec<T> / Value::Array (homogeneous scalars) |
list (...) |
tuples / Value::List (heterogeneous values) |
group {...} |
structs / Value::Group (named settings) |
Integer literals support decimal, hex (0xFF), binary (0b1010), and octal (0o17 / 0q17) formats. Values exceeding the 32-bit range are automatically promoted to 64-bit, or use the L suffix explicitly (100L).
Quick Start
[]
= "0.3.2"
= { = "1.0", = ["derive"] }
Typed Deserialization
use Deserialize;
use from_str;
let config = r#"
hostname = "localhost";
port = 8080;
ssl = true;
"#;
let server: Server = from_str.unwrap;
Serialization
use Serialize;
use to_string;
let settings = AppSettings ;
// The serde serializer always wraps the top-level struct in { }.
// For braces-free output, use Config::new() + set() + Config::to_string().
let output = to_string.unwrap;
// {
// name = "My App";
// version = 1;
// }
Dynamic Config
When you don't know the structure at compile time, use Config for dynamic access:
use Config;
let cfg = from_str.unwrap;
// Index with dotted paths
assert_eq!;
assert_eq!;
assert_eq!;
Reading from a File
use Config;
let cfg = from_file.unwrap;
println!;
Setting Paths
Values can be read, written, and removed using libconfig-style dotted paths. Array and list elements are addressed with bracket notation ([index]) or bare numeric segments:
use ;
let mut cfg = from_str.unwrap;
// Read
assert_eq!;
assert_eq!; // bare numeric also works
// Write (set existing)
cfg.set;
assert_eq!;
// Write (auto-creates intermediate groups)
cfg.set;
assert_eq!;
// Remove (returns the removed value)
let mut cfg = from_str.unwrap;
let removed = cfg.remove;
assert_eq!;
assert!;
// Remove array element (subsequent elements shift down)
let mut cfg = from_str.unwrap;
cfg.remove;
assert_eq!;
Format Features
- Top-level implicit groups (no outer braces required)
- Case-insensitive booleans (
true,True,TRUE,FaLsE) - Adjacent string concatenation (
"hello" " world"->"hello world") - Comments:
#,//,/* */ - Setting separators: semicolons, commas, or whitespace (all optional)
- Both
=and:as key-value delimiters
Examples
License
MIT OR Apache-2.0