GithubHelp home page GithubHelp logo

cconfig's Introduction

cconfig

Basic config.ini reader/writer written in Rust.

Usage

This library provides a Setting struct to hold individual setting data and a Config struct that manages them and a corresponding file. Simply create a Config instance and add all of your settings to it.

use cconfig::setting::Setting;
use cconfig::config::Config;
use cconfig::line_ending::LineEnding;

fn main() {
    // Create a new setting
    let key = String::from("my_key");
    let category = String::from("my_category");

    // A Setting's value type must implement the traits std::format::Display
    // and std::str::FromStr for serialization and deserialization respectively
    let value = 123u8;
    let my_setting = Setting::new(&category, &key, &value);

    // Relative or absolute path
    let path = String::from("foo.txt");

    // Load your config
    let config = Config::new(&path, LineEnding::LF, true);

    // Add a setting
    config.add_setting(&my_setting);

    // Add a setting with loose data
    config.add(&category, &key, &value);

    // Save your config
    config.save();
}

This is mostly an abstraction on a HashMap so there are some additional methods exposed through the Config impl to get information about your settings.

use cconfig::config::Config;
use cconfig::setting::Setting;

fn main() {
    let path = String::from("foo.txt");
    let config = Config::new(&path, LineEnding::LF, true);

    let key = String::from("my_key");
    let category = String::from("my_category");
    let default_value = 123u8;

    let my_mutable_setting = config.get_mut(&category, &key, &default_value);
    let new_value = 124u8;
    my_setting.set_value(new_value);

    let my_immutable_setting = config.get(&category, &key, &default_value);
    let deserialized_value = my_immutable_setting.get_value_or(default_value);

    let removed_setting = match config.remove(&key, &category) {
        Some(value) => value, // Removed setting value
        None => return, // Setting was not in HashMap
    };
}

cconfig's People

Contributors

t895 avatar

Watchers

 avatar

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.