deser_hjson
This is a Serde 1.0 compatible deserializer for Hjson, tailored for derive powered deserialization.
Hjson is a good language for a configuration file. Such files should be written by a human, read and modified by other humans, then deserialized into a precise structure by a program:
let file_content = read_to_string?;
let configuration = from_str;
If the configuration file is invalid or doesn't match the expected type, the error details the expectation and the error precise location.
Example
This Hjson document comes from Hjson's introduction
use ;
let hjson = r#"
{
// use #, // or /**/ comments,
// omit quotes for keys
key: 1
// omit quotes for strings
contains: everything on this line
// omit commas at the end of a line
cool: {
foo: 1
bar: 2
}
// allow trailing commas
list: [
1,
2,
]
// and use multiline strings
realist:
'''
My half empty glass,
I will fill your empty half.
Now you are half full.
'''
}
"#;
// we'll deserialize it into this struct:
let mut cool = new;
cool.insert;
cool.insert;
let expected = Example ;
// Here's the deserialization and the equality check:
assert_eq!;
Performances
A serious benchmark is missing now but this deserializer seems fast enough.
Broot can be configured either with TOML or with Hjson (the selection is dynamic, based on the file extension).
Broot configuration loading takes about 100µs when in Hjson while it takes about 2ms in TOML (so Hjson seems to be 20 times faster than TOML on this simple almost anecdotical test). It's very possible the TOML deserializer is slower only because it's more strict and does more checks, though (I'm not familiar enough with its implementation).