3 releases

0.1.5 Jul 8, 2021
0.1.4 Jul 8, 2021

#35 in Configuration

Custom license

18KB
100 lines

Rust-ConfigParser CI Crates.io Lines of code

โš™ Very simple config parsing lib for rust!

I made this because I just needed a simple config parser for one of my projects and wanted to learn how to make a rust crate. Hopefully you will find it useful as well. :P

๐Ÿ’  Install

Just add the following to your Cargo.toml:

[dependencies]
simple_config_parser = "0.1.5"

๐Ÿ“€ Quick Start

This config parser is made for use with a simplified version of an ini file. There are no sections and currently no Escape character support.

; This is a comment
# This is also a comment
hello = World
rust = Is great
test = "TEST"

๐Ÿณ Why

There are already a few config parsers out there for rust so why use this one?

There are a few reasons:

  • It's super simple to use
  • Its faster then some other popular config parsers (by only a few hundred Nano seconds but still)
  • It's code is easy to understand (For me at least)
  • It would make me happy (:P)

๐Ÿ’ฅExamples

Create a new config.

// Import Lib
use simple_config_parser::config::Config;

// Create a new config with no file
let mut cfg = Config::new(None);

// Create a new config with a file
let mut cfg2 = Config::new(Some("config.cfg"));

Read a config file and parse it.

// Import Lib
use simple_config_parser::config::Config;

// Create a new config with a file
let mut cfg = Config::new(Some("config.cfg"));

// Read / parse config file
cfg.read().ok().expect("Error reading the config file");

Load config from a string.

// Import Lib
use simple_config_parser::config::Config;

// Create a new config with no file
let mut cfg = Config::new(None);

// Parse config from string
cfg.parse("hello = World\nrust = Is great\ntest = \"TEST\"").ok().expect("Error parsing the config file");

Get a value from a config.

// Import Lib
use simple_config_parser::config::Config;

// Create a new config with no file
let mut cfg = Config::new(None);
cfg.parse("hello = World\nrust = Is great").ok().unwrap();

// Create a new config with a file
let mut cfg2 = Config::new(Some("config.cfg"));

// Get a value from the config (As a string)
println!("Hello, {}", cfg.get("hello").unwrap());

Get value from a config as a bool, int and float.

// Import Lib
use simple_config_parser::config::Config;

// Create a new config with no file
let mut cfg = Config::new(None);
cfg.parse("hello = True\nrust = 15\npi = 3.1415926535").ok().unwrap();

// Get a value from the config as bool
assert_eq!(cfg.get_bool("hello").unwrap(), true);

// Get a value from the config as int
assert_eq!(cfg.get_int("rust").unwrap(), 15);

// Get a value from the config as float
assert_eq!(cfg.get_float("pi").unwrap(), 3.1415926535);

No runtime deps