Crate slr_config

source ·
Expand description

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 slr_config;

use slr_config::{ConfigElement, ElementRepr};
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");

	// Compile-time schemas automate the above process in many situations.
	slr_def!
	{
		struct TestSchema
		{
			key: u32 = 0,
			arr: Vec<u32> = vec![]
		}
	}

	let mut schema = TestSchema::new();
	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 = schema.to_element();
	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");
}

Re-exports

pub use self::ConfigElementKind::*;

Macros

A macro to define the compile-time schemas for configuration elements. You can use this macro to define structs and enums, like so:

Structs

A configuration element.
The error type used throughout this crate.
Annotated representation of the configuration source string.

Enums

The kind of the configuration element.
An enum describing the kind of the error, to allow treating different errors differenly.

Traits

Describes a way to convert a type to a ConfigElement and back.

Functions

Deserialize a value to a ConfigElement.
Serialize a value to a ConfigElement.