aboutsummaryrefslogtreecommitdiff
path: root/examples/ini/parser_str.rs
blob: 8f7b9cefcbadc46d3d27912316b8e18d42c57580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::collections::HashMap;

use winnow::prelude::*;
use winnow::{
    ascii::{alphanumeric1 as alphanumeric, space0 as space},
    combinator::opt,
    combinator::repeat,
    combinator::{delimited, terminated},
    token::{take_till0, take_while},
};

pub type Stream<'i> = &'i str;

pub fn categories<'s>(
    input: &mut Stream<'s>,
) -> PResult<HashMap<&'s str, HashMap<&'s str, &'s str>>> {
    repeat(0.., category_and_keys).parse_next(input)
}

fn category_and_keys<'s>(i: &mut Stream<'s>) -> PResult<(&'s str, HashMap<&'s str, &'s str>)> {
    (category, keys_and_values).parse_next(i)
}

fn category<'s>(i: &mut Stream<'s>) -> PResult<&'s str> {
    terminated(
        delimited('[', take_while(0.., |c| c != ']'), ']'),
        opt(take_while(1.., [' ', '\r', '\n'])),
    )
    .parse_next(i)
}

fn keys_and_values<'s>(input: &mut Stream<'s>) -> PResult<HashMap<&'s str, &'s str>> {
    repeat(0.., key_value).parse_next(input)
}

fn key_value<'s>(i: &mut Stream<'s>) -> PResult<(&'s str, &'s str)> {
    let key = alphanumeric.parse_next(i)?;
    let _ = (opt(space), "=", opt(space)).parse_next(i)?;
    let val = take_till0(is_line_ending_or_comment).parse_next(i)?;
    let _ = opt(space).parse_next(i)?;
    let _ = opt((";", not_line_ending)).parse_next(i)?;
    let _ = opt(space_or_line_ending).parse_next(i)?;

    Ok((key, val))
}

fn is_line_ending_or_comment(chr: char) -> bool {
    chr == ';' || chr == '\n'
}

fn not_line_ending<'s>(i: &mut Stream<'s>) -> PResult<&'s str> {
    take_while(0.., |c| c != '\r' && c != '\n').parse_next(i)
}

fn space_or_line_ending<'s>(i: &mut Stream<'s>) -> PResult<&'s str> {
    take_while(1.., [' ', '\r', '\n']).parse_next(i)
}

#[test]
fn parse_category_test() {
    let ini_file = "[category]

parameter=value
key = value2";

    let ini_without_category = "parameter=value
key = value2";

    let res = category.parse_peek(ini_file);
    println!("{:?}", res);
    match res {
        Ok((i, o)) => println!("i: {} | o: {:?}", i, o),
        _ => println!("error"),
    }

    assert_eq!(res, Ok((ini_without_category, "category")));
}

#[test]
fn parse_key_value_test() {
    let ini_file = "parameter=value
key = value2";

    let ini_without_key_value = "key = value2";

    let res = key_value.parse_peek(ini_file);
    println!("{:?}", res);
    match res {
        Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
        _ => println!("error"),
    }

    assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}

#[test]
fn parse_key_value_with_space_test() {
    let ini_file = "parameter = value
key = value2";

    let ini_without_key_value = "key = value2";

    let res = key_value.parse_peek(ini_file);
    println!("{:?}", res);
    match res {
        Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
        _ => println!("error"),
    }

    assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}

#[test]
fn parse_key_value_with_comment_test() {
    let ini_file = "parameter=value;abc
key = value2";

    let ini_without_key_value = "key = value2";

    let res = key_value.parse_peek(ini_file);
    println!("{:?}", res);
    match res {
        Ok((i, (o1, o2))) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
        _ => println!("error"),
    }

    assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
}

#[test]
fn parse_multiple_keys_and_values_test() {
    let ini_file = "parameter=value;abc

key = value2

[category]";

    let ini_without_key_value = "[category]";

    let res = keys_and_values.parse_peek(ini_file);
    println!("{:?}", res);
    match res {
        Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
        _ => println!("error"),
    }

    let mut expected: HashMap<&str, &str> = HashMap::new();
    expected.insert("parameter", "value");
    expected.insert("key", "value2");
    assert_eq!(res, Ok((ini_without_key_value, expected)));
}

#[test]
fn parse_category_then_multiple_keys_and_values_test() {
    //FIXME: there can be an empty line or a comment line after a category
    let ini_file = "[abcd]
parameter=value;abc

key = value2

[category]";

    let ini_after_parser = "[category]";

    let res = category_and_keys.parse_peek(ini_file);
    println!("{:?}", res);
    match res {
        Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
        _ => println!("error"),
    }

    let mut expected_h: HashMap<&str, &str> = HashMap::new();
    expected_h.insert("parameter", "value");
    expected_h.insert("key", "value2");
    assert_eq!(res, Ok((ini_after_parser, ("abcd", expected_h))));
}

#[test]
fn parse_multiple_categories_test() {
    let ini_file = "[abcd]

parameter=value;abc

key = value2

[category]
parameter3=value3
key4 = value4
";

    let res = categories.parse_peek(ini_file);
    //println!("{:?}", res);
    match res {
        Ok((i, ref o)) => println!("i: {} | o: {:?}", i, o),
        _ => println!("error"),
    }

    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();
    expected_h.insert("abcd", expected_1);
    expected_h.insert("category", expected_2);
    assert_eq!(res, Ok(("", expected_h)));
}