Crates.io | serde_conl |
lib.rs | serde_conl |
version | 0.3.0 |
source | src |
created_at | 2024-10-21 05:15:30.486098 |
updated_at | 2024-11-04 06:42:27.370593 |
description | CONL is a human-centric configuration language. This crate has the serde bindings. |
homepage | |
repository | https://github.com/ConradIrwin/serde_conl |
max_upload_size | |
id | 1416936 |
size | 51,654 |
CONL is a post-minimalist, human-centric configuration language. It is a replacement for JSON/YAML/TOML, etc... that supports a JSON-like data model of values, maps and lists; but is designed to be much easier to work with.
For more information, see the README.
serde_conl
provides integration with Serde to serialize and deserialize CONL documents.
Add this crate to your project
cargo add serde_conl
Define a type to represent your configuration. Use #[serde(default)]
to ensure that any missing fields are set to the default values, and #[serde(deny_unknown_fields)]
to provide helpful error messages if someone misspells a field name.
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
#[serde(default, deny_unknown_fields)]
struct Config {
hostname: String,
port: u32,
master: bool,
ssh_args: Vec<String>,
}
Parse the file:
let file = std::fs::File::read("config.conl").unwrap();
let config: Config = serde_conl::from_slice(&file).unwrap();
Any value that serde supports can be used as a value in a CONL document. Keys can be any scalar value (strings, numbers, bools, etc...) but cannot be lists or maps.
That said, CONL is for configuration... so try to use values that make sense to type in a text file, and stay away from fancy programmer-centric type shenanigans. If you need them, serde_humanize_rs provides a bunch of types like durations and file sizes.
Options are typically represented by omitting the field from the map, but as
CONL has no nullability, the empty string is used in map keys or list items. A unit
()
or struct X;
is represented by the empty string.
Enums are represented as they are in JSON (but without the syntax noise). For example:
enum Example {
A,
B(u32),
C { a: u32, b: u32 },
D(u32, u32)
}
example_a = a
example_b
b = 1
example_c
c
a = 1
b = 2
example_d
= 1
= 2