frostwalker 0.1.2

A TOML-like configuration language parser with zero dependencies outside of std.
Documentation
//! A TOML-like configuration language parser that supports single depth arrays, integers, strings and boolean literals.
//!
//! This library isn't intended to compete with `toml`. Frostwalker and `toml` have differing goals and I would recommend you use `toml` over Frostwalker as it supports more features, but Frostwalker has no dependencies apart from the standard library.
//! 
//! The use of this library is easy and doesn't require much work:
//! ```
//! use frostwalker::parse;
//!
//! let parsed_output = parse("yes = true\r\nkey = \"value\"\r\narray = [ 1, 5 ]");
//! let hashmap = parsed_output.unwrap();
//! assert_eq!(hashmap.get("yes").unwrap(), "true");
//!
//! assert_eq!(hashmap.get("key").unwrap(), "value");
//!
//! assert_eq!(hashmap.get("array").unwrap(), "2");
//! assert_eq!(hashmap.get("array[0]").unwrap(), "1");
//! assert_eq!(hashmap.get("array[1]").unwrap(), "5");
//! ```
//!
use std::collections::HashMap;

/// A structure for a lexical token. 
///
/// It is used by all components of the parser stack and is here as to keep each component separate from the others.
#[derive(Debug)]
#[derive(PartialEq)]
pub struct Token {
	pub class: Class,
	pub value: Option<String>,
}

/// An enumerator for types of lexical tokens. 
///
/// It is used by all components of the parser stack and is here as to keep each component separate from the others.
#[derive(Debug)]
#[derive(PartialEq)]
pub enum Class {
	/// Names and keys assigned by the programmer.
	IDENTIFIER,
	/// Punctuation and delimiters.
	SEPARATOR,
	/// Equals.
	EQUALS,
	/// Strings and integers.
	LITERAL,
	/// New lines.
	NEWLINE,
	/// True and false.
	BOOLEAN,
	/// Tokens that do not fit the lexer's ruleset.
	UNKNOWN,
}

pub mod lexer;
pub mod validator;
pub mod formatter;

/// The parsing function. This function takes in a string with configuration in it and either outputs a HashMap containing keys and values or an error message.
///
/// The parser wraps around each function of the parser stack (lexer, validator, and formatter) as to ensure the configuration text is parsed properly. Errors originate from the validator.
pub fn parse(source: &str) -> Result<HashMap<String, String>, String> {
	let tree = lexer::tokenize(source);
	let result = validator::validate(&tree);
	if result.is_some() {
		return Err(result.unwrap_or("Error unwrapping result string.".to_string()));
	}

	let output = formatter::format(tree);
	return Ok(output);
}

#[cfg(test)]
mod lexer_tests;

#[cfg(test)]
mod validator_tests;

#[cfg(test)]
mod formatter_tests;