2015-12-06 14:13:52 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate nom;
|
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
use nom::{
|
2019-04-20 10:16:38 +00:00
|
|
|
IResult,
|
|
|
|
combinator::{map_res, opt},
|
2019-04-17 10:14:15 +00:00
|
|
|
bytes::complete::{take_while, is_a},
|
|
|
|
sequence::{delimited, terminated},
|
2019-04-26 14:12:36 +00:00
|
|
|
character::complete::{char, alphanumeric1 as alphanumeric, space0 as space}
|
2019-04-17 10:14:15 +00:00
|
|
|
};
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
fn is_alphabetic(chr: char) -> bool {
|
2015-12-06 14:13:52 +00:00
|
|
|
(chr as u8 >= 0x41 && chr as u8 <= 0x5A) || (chr as u8 >= 0x61 && chr as u8 <= 0x7A)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_digit(chr: char) -> bool {
|
|
|
|
chr as u8 >= 0x30 && chr as u8 <= 0x39
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_alphanumeric(chr: char) -> bool {
|
|
|
|
is_alphabetic(chr) || is_digit(chr)
|
|
|
|
}
|
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
fn is_space(chr: char) -> bool {
|
2015-12-06 14:13:52 +00:00
|
|
|
chr == ' ' || chr == '\t'
|
|
|
|
}
|
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
fn is_line_ending_or_comment(chr: char) -> bool {
|
2015-12-06 14:13:52 +00:00
|
|
|
chr == ';' || chr == '\n'
|
|
|
|
}
|
|
|
|
|
2019-04-26 14:12:36 +00:00
|
|
|
fn not_line_ending(i: &str) -> IResult<&str, &str> {
|
|
|
|
take_while(|c| c != '\r' && c != '\n')(i)
|
|
|
|
}
|
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
fn space_or_line_ending(i: &str) -> IResult<&str, &str> {
|
|
|
|
is_a(" \r\n")(i)
|
|
|
|
}
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
fn right_bracket(c: char) -> bool {
|
2015-12-06 14:13:52 +00:00
|
|
|
c == ']'
|
|
|
|
}
|
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
fn category(i: &str) -> IResult<&str, &str> {
|
|
|
|
terminated(delimited(char('['), take_while(|c| c != ']'), char(']')), opt(is_a(" \r\n")))(i)
|
|
|
|
}
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
named!(key_value <&str,(&str,&str)>,
|
2016-10-18 10:19:22 +00:00
|
|
|
do_parse!(
|
|
|
|
key: alphanumeric >>
|
|
|
|
opt!(space) >>
|
2019-03-04 09:24:05 +00:00
|
|
|
tag!("=") >>
|
2016-10-18 10:19:22 +00:00
|
|
|
opt!(space) >>
|
2019-03-04 09:24:05 +00:00
|
|
|
val: take_till!(is_line_ending_or_comment) >>
|
2016-10-18 10:19:22 +00:00
|
|
|
opt!(space) >>
|
2019-03-04 09:24:05 +00:00
|
|
|
opt!(pair!(tag!(";"), not_line_ending)) >>
|
2016-10-18 10:19:22 +00:00
|
|
|
opt!(space_or_line_ending) >>
|
2019-04-17 10:14:15 +00:00
|
|
|
(key, val)
|
2015-12-06 14:13:52 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
named!(keys_and_values_aggregator<&str, Vec<(&str, &str)> >, many0!(key_value));
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
fn keys_and_values(input: &str) -> IResult<&str, HashMap<&str, &str>> {
|
2015-12-06 14:13:52 +00:00
|
|
|
match keys_and_values_aggregator(input) {
|
2017-12-10 17:39:11 +00:00
|
|
|
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
|
|
|
|
Err(e) => Err(e),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
named!(category_and_keys<&str,(&str,HashMap<&str,&str>)>,
|
2015-12-06 14:13:52 +00:00
|
|
|
pair!(category, keys_and_values)
|
|
|
|
);
|
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
named!(categories_aggregator<&str, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys));
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str>>> {
|
2015-12-06 14:13:52 +00:00
|
|
|
match categories_aggregator(input) {
|
2017-12-10 17:39:11 +00:00
|
|
|
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
|
|
|
|
Err(e) => Err(e),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_category_test() {
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_file = "[category]
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
parameter=value
|
2019-04-17 10:14:15 +00:00
|
|
|
key = value2";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_without_category = "parameter=value
|
|
|
|
key = value2";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
let res = category(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2019-04-17 10:14:15 +00:00
|
|
|
Ok((i, o)) => println!("i: {} | o: {:?}", i, o),
|
2017-12-10 17:39:11 +00:00
|
|
|
_ => println!("error"),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:28:33 +00:00
|
|
|
assert_eq!(res, Ok((ini_without_category, "category")));
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_key_value_test() {
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_file = "parameter=value
|
|
|
|
key = value2";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_without_key_value = "key = value2";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
let res = key_value(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2019-04-17 10:14:15 +00:00
|
|
|
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
|
2017-12-10 17:39:11 +00:00
|
|
|
_ => println!("error"),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:28:33 +00:00
|
|
|
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_key_value_with_space_test() {
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_file = "parameter = value
|
|
|
|
key = value2";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_without_key_value = "key = value2";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
let res = key_value(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2019-04-17 10:14:15 +00:00
|
|
|
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
|
2017-12-10 17:39:11 +00:00
|
|
|
_ => println!("error"),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:28:33 +00:00
|
|
|
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_key_value_with_comment_test() {
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_file = "parameter=value;abc
|
|
|
|
key = value2";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_without_key_value = "key = value2";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
let res = key_value(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2019-04-17 10:14:15 +00:00
|
|
|
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
|
2017-12-10 17:39:11 +00:00
|
|
|
_ => println!("error"),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:28:33 +00:00
|
|
|
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_multiple_keys_and_values_test() {
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_file = "parameter=value;abc
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
key = value2
|
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
[category]";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_without_key_value = "[category]";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
let res = keys_and_values(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2019-04-17 10:14:15 +00:00
|
|
|
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
|
2017-12-10 17:39:11 +00:00
|
|
|
_ => println!("error"),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut expected: HashMap<&str, &str> = HashMap::new();
|
|
|
|
expected.insert("parameter", "value");
|
|
|
|
expected.insert("key", "value2");
|
2017-09-07 12:28:33 +00:00
|
|
|
assert_eq!(res, Ok((ini_without_key_value, expected)));
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_category_then_multiple_keys_and_values_test() {
|
|
|
|
//FIXME: there can be an empty line or a comment line after a category
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_file = "[abcd]
|
2015-12-06 14:13:52 +00:00
|
|
|
parameter=value;abc
|
|
|
|
|
|
|
|
key = value2
|
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
[category]";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_after_parser = "[category]";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
let res = category_and_keys(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2019-04-17 10:14:15 +00:00
|
|
|
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
|
2017-12-10 17:39:11 +00:00
|
|
|
_ => println!("error"),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut expected_h: HashMap<&str, &str> = HashMap::new();
|
|
|
|
expected_h.insert("parameter", "value");
|
|
|
|
expected_h.insert("key", "value2");
|
2017-09-07 12:28:33 +00:00
|
|
|
assert_eq!(res, Ok((ini_after_parser, ("abcd", expected_h))));
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_multiple_categories_test() {
|
2019-04-17 10:14:15 +00:00
|
|
|
let ini_file = "[abcd]
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
parameter=value;abc
|
|
|
|
|
|
|
|
key = value2
|
|
|
|
|
|
|
|
[category]
|
|
|
|
parameter3=value3
|
|
|
|
key4 = value4
|
2019-04-17 10:14:15 +00:00
|
|
|
";
|
2015-12-06 14:13:52 +00:00
|
|
|
|
|
|
|
let res = categories(ini_file);
|
|
|
|
//println!("{:?}", res);
|
|
|
|
match res {
|
2019-04-17 10:14:15 +00:00
|
|
|
Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
|
2017-12-10 17:39:11 +00:00
|
|
|
_ => println!("error"),
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut expected_1: HashMap<&str, &str> = HashMap::new();
|
|
|
|
expected_1.insert("parameter", "value");
|
|
|
|
expected_1.insert("key", "value2");
|
|
|
|
let mut expected_2: HashMap<&str, &str> = HashMap::new();
|
|
|
|
expected_2.insert("parameter3", "value3");
|
|
|
|
expected_2.insert("key4", "value4");
|
|
|
|
let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
|
2017-12-10 17:39:11 +00:00
|
|
|
expected_h.insert("abcd", expected_1);
|
2015-12-06 14:13:52 +00:00
|
|
|
expected_h.insert("category", expected_2);
|
2019-04-17 10:14:15 +00:00
|
|
|
assert_eq!(res, Ok(("", expected_h)));
|
2015-12-06 14:13:52 +00:00
|
|
|
}
|