2017-02-10 23:19:18 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate nom;
|
|
|
|
|
|
|
|
use nom::IResult;
|
2018-10-22 10:43:49 +03:00
|
|
|
use nom::types::CompleteStr;
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
fn is_alphabetic(chr: char) -> bool {
|
2017-02-10 23:19:18 +03: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)
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
fn is_space(chr: char) -> bool {
|
2017-02-10 23:19:18 +03:00
|
|
|
chr == ' ' || chr == '\t'
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
fn is_line_ending_or_comment(chr: char) -> bool {
|
2017-02-10 23:19:18 +03:00
|
|
|
chr == ';' || chr == '\n'
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
named!(alphanumeric<CompleteStr,CompleteStr>, take_while_s!(is_alphanumeric));
|
|
|
|
named!(not_line_ending<CompleteStr,CompleteStr>, is_not_s!("\r\n"));
|
|
|
|
named!(space<CompleteStr,CompleteStr>, take_while_s!(is_space));
|
|
|
|
named!(space_or_line_ending<CompleteStr,CompleteStr>, is_a_s!(" \r\n"));
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
fn right_bracket(c: char) -> bool {
|
2017-02-10 23:19:18 +03:00
|
|
|
c == ']'
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
named!(category <CompleteStr, &str>,
|
2018-06-12 21:03:01 +03:00
|
|
|
do_parse!(
|
|
|
|
tag_s!("[") >>
|
|
|
|
name: take_till_s!(right_bracket) >>
|
|
|
|
tag_s!("]") >>
|
|
|
|
opt!(space_or_line_ending) >>
|
2018-10-22 10:43:49 +03:00
|
|
|
(name.0)
|
2017-02-10 23:19:18 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
named!(key_value <CompleteStr,(&str,&str)>,
|
2018-06-12 21:03:01 +03:00
|
|
|
do_parse!(
|
|
|
|
key: alphanumeric >>
|
|
|
|
opt!(space) >>
|
|
|
|
tag_s!("=") >>
|
|
|
|
opt!(space) >>
|
|
|
|
val: take_till_s!(is_line_ending_or_comment) >>
|
|
|
|
opt!(space) >>
|
|
|
|
opt!(pair!(tag_s!(";"), not_line_ending)) >>
|
|
|
|
opt!(space_or_line_ending) >>
|
2018-10-22 10:43:49 +03:00
|
|
|
(key.0, val.0)
|
2017-02-10 23:19:18 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
named!(keys_and_values_aggregator<CompleteStr, Vec<(&str, &str)> >, many0!(key_value));
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
fn keys_and_values(input: CompleteStr) -> IResult<CompleteStr, HashMap<&str, &str>> {
|
2017-02-10 23:19:18 +03:00
|
|
|
match keys_and_values_aggregator(input) {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
|
|
|
|
Err(e) => Err(e),
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
named!(category_and_keys<CompleteStr,(&str,HashMap<&str,&str>)>,
|
2017-02-10 23:19:18 +03:00
|
|
|
pair!(category, keys_and_values)
|
|
|
|
);
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
named!(categories_aggregator<CompleteStr, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys));
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
fn categories(input: CompleteStr) -> IResult<CompleteStr, HashMap<&str, HashMap<&str, &str>>> {
|
2017-02-10 23:19:18 +03:00
|
|
|
match categories_aggregator(input) {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, tuple_vec)) => Ok((i, tuple_vec.into_iter().collect())),
|
|
|
|
Err(e) => Err(e),
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_category_test() {
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_file = CompleteStr(
|
|
|
|
"[category]
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
parameter=value
|
2018-10-22 10:43:49 +03:00
|
|
|
key = value2",
|
|
|
|
);
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_without_category = CompleteStr(
|
|
|
|
"parameter=value
|
|
|
|
key = value2",
|
|
|
|
);
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
let res = category(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, o)) => println!("i: {} | o: {:?}", i.0, o),
|
|
|
|
_ => println!("error"),
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
assert_eq!(res, Ok((ini_without_category, "category")));
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_key_value_test() {
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_file = CompleteStr(
|
|
|
|
"parameter=value
|
|
|
|
key = value2",
|
|
|
|
);
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_without_key_value = CompleteStr("key = value2");
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
let res = key_value(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i.0, o1, o2),
|
|
|
|
_ => println!("error"),
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_key_value_with_space_test() {
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_file = CompleteStr(
|
|
|
|
"parameter = value
|
|
|
|
key = value2",
|
|
|
|
);
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_without_key_value = CompleteStr("key = value2");
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
let res = key_value(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i.0, o1, o2),
|
|
|
|
_ => println!("error"),
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_key_value_with_comment_test() {
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_file = CompleteStr(
|
|
|
|
"parameter=value;abc
|
|
|
|
key = value2",
|
|
|
|
);
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_without_key_value = CompleteStr("key = value2");
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
let res = key_value(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i.0, o1, o2),
|
|
|
|
_ => println!("error"),
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_multiple_keys_and_values_test() {
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_file = CompleteStr(
|
|
|
|
"parameter=value;abc
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
key = value2
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
[category]",
|
|
|
|
);
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_without_key_value = CompleteStr("[category]");
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
let res = keys_and_values(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, ref o)) => println!("i: {} | o: {:?}", i.0, o),
|
|
|
|
_ => println!("error"),
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut expected: HashMap<&str, &str> = HashMap::new();
|
|
|
|
expected.insert("parameter", "value");
|
|
|
|
expected.insert("key", "value2");
|
2018-10-22 10:43:49 +03:00
|
|
|
assert_eq!(res, Ok((ini_without_key_value, expected)));
|
2017-02-10 23:19:18 +03: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
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_file = CompleteStr(
|
|
|
|
"[abcd]
|
2017-02-10 23:19:18 +03:00
|
|
|
parameter=value;abc
|
|
|
|
|
|
|
|
key = value2
|
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
[category]",
|
|
|
|
);
|
2017-02-10 23:19:18 +03:00
|
|
|
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_after_parser = CompleteStr("[category]");
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
let res = category_and_keys(ini_file);
|
|
|
|
println!("{:?}", res);
|
|
|
|
match res {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, ref o)) => println!("i: {} | o: {:?}", i.0, o),
|
|
|
|
_ => println!("error"),
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut expected_h: HashMap<&str, &str> = HashMap::new();
|
|
|
|
expected_h.insert("parameter", "value");
|
|
|
|
expected_h.insert("key", "value2");
|
2018-10-22 10:43:49 +03:00
|
|
|
assert_eq!(res, Ok((ini_after_parser, ("abcd", expected_h))));
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_multiple_categories_test() {
|
2018-10-22 10:43:49 +03:00
|
|
|
let ini_file = CompleteStr(
|
|
|
|
"[abcd]
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
parameter=value;abc
|
|
|
|
|
|
|
|
key = value2
|
|
|
|
|
|
|
|
[category]
|
|
|
|
parameter3=value3
|
|
|
|
key4 = value4
|
2018-10-22 10:43:49 +03:00
|
|
|
",
|
|
|
|
);
|
2017-02-10 23:19:18 +03:00
|
|
|
|
|
|
|
let res = categories(ini_file);
|
|
|
|
//println!("{:?}", res);
|
|
|
|
match res {
|
2018-10-22 10:43:49 +03:00
|
|
|
Ok((i, ref o)) => println!("i: {} | o: {:?}", i.0, o),
|
|
|
|
_ => println!("error"),
|
2017-02-10 23:19:18 +03: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();
|
2018-10-22 10:43:49 +03:00
|
|
|
expected_h.insert("abcd", expected_1);
|
2017-02-10 23:19:18 +03:00
|
|
|
expected_h.insert("category", expected_2);
|
2018-10-22 10:43:49 +03:00
|
|
|
assert_eq!(res, Ok((CompleteStr(""), expected_h)));
|
2017-02-10 23:19:18 +03:00
|
|
|
}
|