dotproperties 0.1.0

Parser for the Java .properties file format
Documentation
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Apparently, refering to a function from a macro is not enough to consider it
// being used.
#![allow(dead_code)]

// The specifications used to implement this parser:
// https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html#load-java.io.Reader-

mod utf16;

use self::utf16::escape_sequence;
use std::char;

// Consume multiple characters considered as spaces by the format. Note that
// \u{c} is a "form feed". It can be written \f in C and Java (and in the
// format we are parsing), but it does not have a dedicated escape in Rust.
named!(eat_whitespaces<()>,
    do_parse!(
        many0!(one_of!(" \t\u{c}")) >>
        ()
    )
);

// Consume an end of line (Windows, legacy Mac or Unix-style)
named!(eat_one_eol<()>,
    do_parse!(
        alt!(complete!(tag!("\r\n")) | tag!("\r") | tag!("\n")) >>
        ()
    )
);

// Consume an end of line, or return Done if the input is empty.
named!(eat_one_eol_or_eof<()>,
    alt!(value!((), eof!()) | eat_one_eol)
);

/// A logical line is an antislash followed by an eol. The space characters
/// that follow are ignored. This parser consumes the logical line and produces
/// nothing.
named!(eat_one_logical_line<()>,
    do_parse!(
        tag!(r"\") >>
        eat_one_eol >>
        eat_whitespaces >>
        ()
    )
);

/// Consume multiple logical lines and produce nothing.
named!(eat_logical_lines<()>,
    do_parse!(
        many0!(eat_one_logical_line) >>
        ()
    )
);

/// Map the character x in an escape context \x to the value of the escape.
/// Note the wildcard at the end: any character can indeed be escaped and
/// produce the character itself.
fn escaped_char_to_char(v: char) -> char {
    match v {
        't' => '\t',
        'n' => '\n',
        'f' => '\u{c}',
        'r' => '\r',
        '\\' => '\\',
        _ => v
    }
}

/// Match a simple (non unicode) escape sequence \x, and produces its
/// corresponding character.
/// We take care to not match \u (an unicode escape) and \ followed by an eol
/// (a logical line split).
///
named!(escape_in_key_or_value<char>,
    do_parse!(
        tag!(r"\") >>
        c: none_of!("u\r\n") >>
        (escaped_char_to_char(c))
    )
);

/// Match all characters that don't need to be escaped in a key.
named!(char_in_key<char>,
    none_of!(":=\n\r \t\u{c}\\")
);

/// Match all characters that don't need to be escaped in a value.
named!(char_in_value<char>,
    none_of!("\n\r\\")
);

/// Match a real character in a key: a unicode esacpe, a simple escape, or
/// a simple character.
named!(one_char_in_key<char>,
    alt!(escape_sequence | escape_in_key_or_value | char_in_key)
);

/// Match a real character in a value: a unicode esacpe, a simple escape, or
/// a simple character.
named!(one_char_in_value<char>,
    alt!(escape_sequence | escape_in_key_or_value | char_in_value)
);

/// Match a whole key. takes care of ignoring the logical lines that can
/// appear between any real character.
named!(key<String>,
    do_parse!(
        chars: sep!(eat_logical_lines, many1!(one_char_in_key)) >>
        (chars.into_iter().collect())
    )
);

/// Match a whole value. takes care of ignoring the logical lines that can
/// appear between any real character.
named!(value<String>,
    do_parse!(
        chars: sep!(eat_logical_lines, many0!(one_char_in_value)) >>
        (chars.into_iter().collect())
    )
);

/// Consume interspersed whitespaces and logical line separators
named!(eat_whitespaces_and_logical_lines<()>,
    do_parse!(
        sep!(eat_logical_lines, eat_whitespaces) >>
        ()
    )
);

/// Match a full key/value line. Take care of allowing logical line separators
/// anywhere. Also, the line can end with either an eol or an eof.
named!(key_value_line<(String, String)>,
    do_parse!(
        eat_whitespaces_and_logical_lines >>
        k: key >>
        eat_whitespaces_and_logical_lines >>
        opt!(complete!(one_of!(":="))) >>
        eat_whitespaces_and_logical_lines >>
        v: value >>
        eat_one_eol_or_eof >>
        (k, v)
    )
);

/// Match a blank line (only whitespaces)
named!(blank_line<()>,
    do_parse!(
        eat_whitespaces >>
        eat_one_eol_or_eof >>
        ()
    )
);

/// The byte represents one of the two line separators
fn is_eol_char(v: u8) -> bool {
    let v = v as char;
    v == '\n' || v == '\r'
}

/// Match a line of comment
named!(comment_line<()>,
    do_parse!(
        eat_whitespaces >>
        one_of!("#!") >>
        take_till!(is_eol_char) >>
        eat_one_eol_or_eof >>
        ()
    )
);

/// An intermediary parser, that matches one of the three lines types.
/// `comment_line` and `blank_line` return (), whereas `key_value_line` returns
/// a pair of Strings. To use these 3 parsers in the same `alt!`, they must
/// return the same type. We use `value!` and `opt!` to do so.
named!(full_parser_opt<Vec<Option<(String, String)>>>,
    many0!(
        alt!(
            value!(None, complete!(comment_line)) |
            value!(None, complete!(blank_line)) |
            opt!(complete!(key_value_line))
        )
    )
);

/// Match the full file format, and return the list of key value pairs
/// extracted.
/// Internally, it takes the result of `full_parser_opt` and it removes the
/// None values
named!(pub full_parser<Vec<(String, String)>>,
    map!(full_parser_opt, |v| v.into_iter().filter_map(|x| x).collect())
);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_key() {
        // basic case
        assert_done!(key(b"hello"), String::from("hello"));

        // A space ends the key
        assert_done_partial!(key(b"hello world"), String::from("hello"), b" world");

        // A colon ends the key
        assert_done_partial!(key(b"hello:world"), String::from("hello"), b":world");

        // An equal sign ends the key
        assert_done_partial!(key(b"hello=world"), String::from("hello"), b"=world");

        // An eol ends the key
        assert_done_partial!(key(b"hello\nworld"), String::from("hello"), b"\nworld");
        assert_done_partial!(key(b"hello\rworld"), String::from("hello"), b"\rworld");

        // These characters are valid
        assert_done!(key(b"@#$%^&*()_+-`~?/.>,<|][{};\""), String::from("@#$%^&*()_+-`~?/.>,<|][{};\""));

        // Spaces can be escaped
        assert_done!(key(br"key\ with\ spaces"), String::from("key with spaces"));

        // Colons can be escaped
        assert_done!(key(br"key\:with\:colons"), String::from("key:with:colons"));

        // Equals can be escaped
        assert_done!(key(br"key\=with\=equals"), String::from("key=with=equals"));

        // Special characters can be escaped
        assert_done!(key(br"now\nwith\rsome\fspecial\tcharacters\\"), String::from("now\nwith\rsome\u{c}special\tcharacters\\"));

        // Escapes on non escapable characters are ignored
        assert_done!(key(br"w\iths\omeran\domch\arse\sca\pe\d"), String::from("withsomerandomcharsescaped"));

        // Unicode esacpes
        assert_done!(key(br"\u0048\u0065\u006c\u006c\u006f"), String::from("Hello"));

        // No input is not a key
        assert_incomplete!(key(b""));

        // With logical line splits
        assert_done!(key(b"abc\\\n   def"), String::from("abcdef"));
        assert_done!(key(b"gh\\\n    \\\r    \\\r\nij\\\n\t kl"), String::from("ghijkl"));

        // A byte above 127 is interpreted as a latin-1 extended character with
        // the same Unicode code point value.
        assert_done!(key(&[0xA9]), String::from("\u{a9}"));

        // An \u escape must be followed by 4 hex digits.
        assert_done_partial!(key(br"abc\uhello"), String::from("abc"), br"\uhello");
    }

    #[test]
    fn test_value() {
        // basic case
        assert_done!(value(b"hello"), String::from("hello"));

        // colons and equal signs are valid
        assert_done!(value(b"h:l=o"), String::from("h:l=o"));

        // spaces are valid, even at the end
        assert_done!(value(b"hello world  "), String::from("hello world  "));

        // These are valid characters
        assert_done!(value(b"/~`!@#$%^&*()-_=+[{]};:'\",<.>/?|"), String::from("/~`!@#$%^&*()-_=+[{]};:'\",<.>/?|"));

        // An eol ends the value
        assert_done_partial!(value(b"hello\nworld"), String::from("hello"), b"\nworld");
        assert_done_partial!(value(b"hello\rworld"), String::from("hello"), b"\rworld");

        // Special characters can be escaped
        assert_done!(value(br"now\nwith\rsome\fspecial\tcharacters\\"), String::from("now\nwith\rsome\u{c}special\tcharacters\\"));

        // Escapes on non escapable characters are ignored
        assert_done!(value(br"w\iths\omeran\domch\arse\sca\pe\d"), String::from("withsomerandomcharsescaped"));

        // Unicode esacpes
        assert_done!(value(br"\u0048\u0065\u006c\u006c\u006f"), String::from("Hello"));

        // No input is a valid value
        assert_done!(value(b""), String::from(""));

        // With logical line splits
        assert_done!(value(b"abc\\\n   def"), String::from("abcdef"));
        assert_done!(value(b"gh\\\n    \\\r    \\\r\nij\\\n\t kl"), String::from("ghijkl"));

        // A byte above 127 is interpreted as a latin-1 extended character with
        // the same Unicode code point value.
        assert_done!(value(&[0xA9]), String::from("\u{a9}"));

        // An \u escape must be followed by 4 hex digits.
        assert_done_partial!(value(br"abc\uhello"), String::from("abc"), br"\uhello");
    }

    #[test]
    fn test_key_value_line() {
        // Basic cases
        assert_done!(key_value_line(b"hello=world\n"), (String::from("hello"), String::from("world")));
        assert_done!(key_value_line(b"hello=world"), (String::from("hello"), String::from("world")));
        assert_done!(key_value_line(b"hello =world"), (String::from("hello"), String::from("world")));
        assert_done!(key_value_line(b"hello= world"), (String::from("hello"), String::from("world")));
        assert_done!(key_value_line(b"hello : world"), (String::from("hello"), String::from("world")));
        assert_done!(key_value_line(b"hello world"), (String::from("hello"), String::from("world")));
        assert_done!(key_value_line(b"  hello = world"), (String::from("hello"), String::from("world")));

        // An empty key is an error
        assert_error!(key_value_line(b"= world"));
        assert_error!(key_value_line(b" = world"));

        // Empty values
        assert_done!(key_value_line(b"hello=\n"), (String::from("hello"), String::from("")));
        assert_done!(key_value_line(b"hello="), (String::from("hello"), String::from("")));
        assert_done!(key_value_line(b"hello ="), (String::from("hello"), String::from("")));
        assert_done!(key_value_line(b"hello = "), (String::from("hello"), String::from("")));
        assert_done!(key_value_line(b" hello ="), (String::from("hello"), String::from("")));
        assert_done!(key_value_line(b"hello\n"), (String::from("hello"), String::from("")));
        assert_done!(key_value_line(b"hello"), (String::from("hello"), String::from("")));
        assert_done!(key_value_line(b"hello  "), (String::from("hello"), String::from("")));

        // Edge cases with escapes
        assert_done!(key_value_line(br"hello\=cruel=world"), (String::from("hello=cruel"), String::from("world")));
        assert_done!(key_value_line(br"hello\==world"), (String::from("hello="), String::from("world")));
        assert_done!(key_value_line(br"hello\ cruel = world"), (String::from("hello cruel"), String::from("world")));
        assert_done!(key_value_line(br"hello\  = world"), (String::from("hello "), String::from("world")));
        assert_done!(key_value_line(br"hello\ncruel = world"), (String::from("hello\ncruel"), String::from("world")));
        assert_done!(key_value_line(br"hello\n = world"), (String::from("hello\n"), String::from("world")));
        assert_done!(key_value_line(br"hello = \ world"), (String::from("hello"), String::from(" world")));

        // Unicode escapes
        assert_done!(key_value_line(br"hello\u003dcruel:world"), (String::from("hello=cruel"), String::from("world")));
        assert_error!(key_value_line(br"hello\ucruel : world"));

        // Empty content is incomplete
        assert_incomplete!(key_value_line(b""));

        // Logical lines
        assert_done!(key_value_line(b"abc\\\n   def = 1"), (String::from("abcdef"), String::from("1")));
        assert_done!(key_value_line(b"gh\\\n      \\\r\n      \\\rij\\\n  kl = 2"), (String::from("ghijkl"), String::from("2")));
        assert_done!(key_value_line(b"mn \\\n = 3"), (String::from("mn"), String::from("3")));
        assert_done!(key_value_line(b"op \\\n4"), (String::from("op"), String::from("4")));
        assert_done!(key_value_line(b"qrs =\\\n  5"), (String::from("qrs"), String::from("5")));
        assert_done!(key_value_line(b"tu = 67\\\n    89"), (String::from("tu"), String::from("6789")));
        assert_done!(key_value_line(b"vw 1\\\n2"), (String::from("vw"), String::from("12")));
    }

    #[test]
    fn test_blank() {

        assert_done!(blank_line(b"\n"), ());
        assert_done!(blank_line(b"\r"), ());
        assert_done!(blank_line(b"\r\n"), ());
        assert_done!(blank_line(b""), ());
        assert_done!(blank_line(b"   \t \n"), ());
        assert_done!(blank_line(b"   \t \r"), ());
        assert_done!(blank_line(b"   \t \r\n"), ());
        assert_done!(blank_line(b"   \t "), ());

        assert_done_partial!(blank_line(b"\nhello"), (), b"hello");
        assert_done_partial!(blank_line(b"\rhello"), (), b"hello");
        assert_done_partial!(blank_line(b"\r\nhello"), (), b"hello");
        assert_done_partial!(blank_line(b"   \t \nhello"), (), b"hello");
        assert_done_partial!(blank_line(b"   \t \rhello"), (), b"hello");
        assert_done_partial!(blank_line(b"   \t \r\nhello"), (), b"hello");
    }

    #[test]
    fn test_comment() {

        // Comments starting with #
        assert_done!(comment_line(b"#hello\n"), ());
        assert_done!(comment_line(b"#hello\r"), ());
        assert_done!(comment_line(b"#hello\r\n"), ());
        assert_done!(comment_line(b"#hello"), ());
        assert_done!(comment_line(b"#"), ());
        assert_done!(comment_line(b"   \t #hello\n"), ());
        assert_done!(comment_line(b"   \t #hello\r"), ());
        assert_done!(comment_line(b"   \t #hello\r\n"), ());
        assert_done!(comment_line(b"   \t #hello"), ());
        assert_done!(comment_line(b"   \t #"), ());

        // Comments starting with !
        assert_done!(comment_line(b"!hello\n"), ());
        assert_done!(comment_line(b"!hello\r"), ());
        assert_done!(comment_line(b"!hello\r\n"), ());
        assert_done!(comment_line(b"!"), ());
        assert_done!(comment_line(b"   \t !hello\n"), ());
        assert_done!(comment_line(b"   \t !hello\r"), ());
        assert_done!(comment_line(b"   \t !hello\r\n"), ());
        assert_done!(comment_line(b"   \t !"), ());

        // Escapes and logical lines are not interpreted
        assert_done!(comment_line(b"# \\"), ());
        assert_done!(comment_line(b"# \\\n"), ());

        // Any newline ends the comment line
        assert_done_partial!(comment_line(b"#\nhello"), (), b"hello");
        assert_done_partial!(comment_line(b"#\rhello"), (), b"hello");
        assert_done_partial!(comment_line(b"#\r\nhello"), (), b"hello");
        assert_done_partial!(comment_line(b"#   \t \nhello"), (), b"hello");
        assert_done_partial!(comment_line(b"#   \t \rhello"), (), b"hello");
        assert_done_partial!(comment_line(b"#   \t \r\nhello"), (), b"hello");
    }

    /// Create a key/value pair from two slices
    fn kv(k: &str, v: &str) -> (String, String) {
        (k.to_string(), v.to_string())
    }

    #[test]
    fn test_full_parser() {
        // Empty file
        assert_done!(full_parser(b""), vec![]);

        // Only comments
        assert_done!(full_parser(b"# Hello\n"), vec![]);
        assert_done!(full_parser(b"# Hello\n! world"), vec![]);

        // Only blank lines
        assert_done!(full_parser(b"  \t\n  \r  \t \r\n "), vec![]);

        // Simple key values
        assert_done!(full_parser(b"a = b\nc : d"), vec![kv("a", "b"), kv("c", "d")]);
        assert_done!(full_parser(b"a   b\nc = d"), vec![kv("a", "b"), kv("c", "d")]);

        // Mixed line types
        assert_done!(full_parser(b"# xx\na=b\n\nc:d\r\n  \t\r  !zz"), vec![kv("a", "b"), kv("c", "d")]);

        // Logical lines
        assert_done!(full_parser(b"#xx\n  a =\\\n  b\n\n\nc\\\n  \\\n : d"), vec![kv("a", "b"), kv("c", "d")]);
    }
}