22 releases

0.0.22 Feb 5, 2024
0.0.21 Jul 11, 2022
0.0.20 Jan 5, 2020
0.0.19 Dec 27, 2019
0.0.11 Jul 28, 2015

#359 in Configuration

Download history 54/week @ 2023-10-29 15/week @ 2023-11-05 33/week @ 2023-11-12 16/week @ 2023-11-19 54/week @ 2023-11-26 94/week @ 2023-12-03 10/week @ 2023-12-10 35/week @ 2023-12-17 53/week @ 2023-12-24 9/week @ 2023-12-31 38/week @ 2024-01-07 37/week @ 2024-01-14 34/week @ 2024-01-21 51/week @ 2024-01-28 39/week @ 2024-02-04 107/week @ 2024-02-11

255 downloads per month

LGPL-3.0

86KB
3.5K SLoC

This crate implements the parsing for the SLRConfig format. Basic usage revolves around the creation and use of the ConfigElement type, like so:

#[macro_use]
extern crate serde_derive;
extern crate slr_config;

use slr_config::{to_element, from_element, ConfigElement};
use std::path::Path;

fn main()
{
// Parse config element from value.
let root = ConfigElement::from_str("key = value").unwrap();
assert_eq!(root.as_table().unwrap()["key"].as_value().unwrap(), "value");

// Create a new table and print it to a string.
let mut root = ConfigElement::new_table();
let val = ConfigElement::new_value("value");
root.insert("key", val);
assert_eq!(root.to_string(), "key = value\n");

// You can use Serde as well.
#[derive(Serialize, Deserialize)]
struct TestSchema
{
key: u32,
arr: Vec<u32>,
}

let mut schema = TestSchema {
key: 0,
arr: vec![],
};
schema = from_element(&ConfigElement::from_str("key = 5, arr = [1, 2]").unwrap(), None).unwrap();
assert_eq!(schema.key, 5);
assert_eq!(schema.arr.len(), 2);
assert_eq!(schema.arr[0], 1);
assert_eq!(schema.arr[1], 2);

let elem = to_element(&schema).unwrap();
assert_eq!(elem.as_table().unwrap()["key"].as_value().unwrap(), "5");
assert_eq!(elem.as_table().unwrap()["arr"].as_array().unwrap()[0].as_value().unwrap(), "1");
}

Dependencies

~1.3–2MB
~39K SLoC