[][src]Crate curlyconf


Curlyconf is a configuration file reader for the configuration file format used by, for example, named.conf and dhcpd.conf.

Example config (file.cfg)

person charlie {
	fullname "Charlie Brown";
person snoopy {
	fullname "Snoopy";

Example code

use serde::Deserialize;

// The initial section of any config is a rust struct.
#[derive(Debug, Deserialize)]
struct Config {
    person: Vec<Person>,

#[derive(Debug, Deserialize)]
struct Person {
    #[serde(rename = "__label__")]
    name: String,
    fullname: Option<String>,
    address: Option<std::net::IpAddr>,

fn main() {
    // Read the configuration file.
    let config: Config = match curlyconf::from_file("file.cfg") {
        Ok(cfg) => cfg,
        Err(e) => {
            eprintln!("{}", e);

    // Print what we got (println!("{:?}", config) would be easier...).
    for (i, p) in config.person.iter().enumerate() {
        println!("{}: {} fullname {:?} addr {:?}", i, p.name, p.fullname, p.address);

This will print:

0: charlie fullname Some("Charlie Brown") addr Some(V4(
1: snoopy fullname Some("Snoopy") addr None

Curlyconf uses serde to deserialize the configuration file values to rust types, just like almost every other crate that does something similar.

Sections and values.

The configuration file contains section names, labels, sections, value names, and values:

  • sections. they have a section_name, an optional label, and contain a list of other sections and values. The rust type of a section is a struct.
  • values. this is a value_name, followed by a value. If the value is a Vec, there can be multiple values, separated by a comma.

A section can only have a label if:

  • it is part of a HashMap<Key, Section>, or
  • it is part of a Vec<Section> and the rust struct that corresponds to the section has a __label__ field. That field will be set to the label value.

The label type can be any type, it does not have to be a string - it could also be, for example, a PathBuf or IpAddr.

The basic structure of a config file is thus:

section_name [label] {
    value_name value [,value...];
    value_name value [,value...];
    section_name [label] {
        value_name value [,value...];

Enums are also supported (see the serde docs) so you can do things like:

#[derive(Debug, Deserialize)]
struct Config {
    animal: Animal,

#[derive(Debug, Deserialize)]
enum Animal {
    Cat {
         purrs: bool,
    Dog {
         barks: bool,

And then have a config like

animal cat {



Configuration builder.


Generic error returned by the configuration parser.


Reference to the parser for Deserialize impls.



Config file parser variant.



Make it possible for Deserialize impls to access the state of the parser.



Read configuration from a file.


Read configuration from a string.