#configuration #processing #wrapper #files #library #libconfig

librustconfig

libRustConfig is rust wrapper around libconfig library. Library for processing configuration files.

2 releases

new 0.1.1 Sep 14, 2020
0.1.0 Jun 21, 2020

#39 in Configuration

MIT license

70KB
992 lines

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:

[dependencies]
librustconfig = "0.1.*"

Usage

Add to your crate root:

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
use libconfig::config::{Config, OptionType};
use std::path::Path;

let mut cfg = Config::new();
if cfg.load_from_string(
	"section1 : {
		integer_value = -12;
    	boolean_value = true;
    	int64_value = 99999L;
    	float_value = 0.9999991;
    	string_value = \"test string value \";
    }";
).is_err() {
    panic!("Can\t load configuration from string value!");
}
Insert
let group = cfg.create_section("group");
if group.is_none() {
    panic!("Can't create new group section!");
}

if group.unwrap().write_string("value", "string value").is_none() {
    panic!("Can't write string value!");
}
Insert group
let array = group.create_array("array_list");
if array.is_none() {
    panic!("Can't create new array option group!");
}

if array.write_int32(12).is_none() {
    panic!("Can't write array element value!");
}
Search
if !cfg.value("section1").unwrap().is_section().unwrap() {
    panic!("Value must be a group!");
}

let _int_val = cfg.value("section1.integer_Value").unwrap().as_int32();
if int_val.is_none() {
    panic!("Can't read integer_value from configuration");
}

match cfg.value("section1.int64_value").unwrap().value_type().unwrap() {
    OptionType::Int64Type => { /* ... do something ... */ }
    _ => { /* ... do nothing ... */ }
}
Search default
let _bool_val = cfg.value("section1.boolean_value").unwrap().as_bool_default(false);
Iterate
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
if cfg.save_to_file(Path::new("config.cfg")).is_err() {
    panic!("Can't save configuration to file!");
}

Dependencies