libRustConfig

It is rust bindings and wrapper around libconfig library. Library for processing configuration files.

Table of contents

Requirements

Library is writing used latest stable Rust Compiler (rustc 1.46.0 (04488afe3 2020-08-24)).

Installation

Add this to your Cargo.toml:

toml [dependencies] librustconfig = "0.1.*"

Usage

Add to your crate root:

rust extern crate config;

Bindings

libconfig-sys crate contains the libconfig translated headers to use this library in Rust programs.

Wrapper

libconfig crate contains the libconfig safe wrapper.

Usage example

Create

```rust use libconfig::config::{Config, OptionType}; use std::path::Path;

let mut cfg = Config::new(); if cfg.loadfromstring( "section1 : { integervalue = -12; booleanvalue = true; int64value = 99999L; floatvalue = 0.9999991; stringvalue = \"test string value \"; }"; ).iserr() { panic!("Can\t load configuration from string value!"); } ```

Insert

```rust let group = cfg.createsection("group"); if group.isnone() { panic!("Can't create new group section!"); }

if group.unwrap().writestring("value", "string value").isnone() { panic!("Can't write string value!"); } ```

Insert group

```rust let array = group.createarray("arraylist"); if array.is_none() { panic!("Can't create new array option group!"); }

if array.writeint32(12).isnone() { panic!("Can't write array element value!"); } ```

Search

```rust if !cfg.value("section1").unwrap().is_section().unwrap() { panic!("Value must be a group!"); }

let intval = cfg.value("section1.integerValue").unwrap().asint32(); if intval.isnone() { panic!("Can't read integer_value from configuration"); }

match cfg.value("section1.int64value").unwrap().valuetype().unwrap() { OptionType::Int64Type => { /* ... do something ... / } _ => { / ... do nothing ... */ } } ```

Search default

rust let _bool_val = cfg.value("section1.boolean_value").unwrap().as_bool_default(false);

Iterate

rust for arr_val in cfg.value("group.array_list").unwrap().as_array() { if arr_val.as_int32().in_none() { panic!("Can't read array item value!"); } /* ... do something with array item ... */ }

Save

rust if cfg.save_to_file(Path::new("config.cfg")).is_err() { panic!("Can't save configuration to file!"); }