Giter VIP home page Giter VIP logo

librustconfig's Introduction

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!");
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.