A .properties
parser with extended bracket notation for grouping keys.
This crate provides two static methods:
pub fn parse_file(file: &File) -> HashMap<String, String>
- Call to parse by file
pub fn parse_string(body: &str) -> HashMap<String, String>
- Call to parse by string
While .properties
format has no official RFC, the general description for this format is too simple and at times there's a need to group similar keys together.
For example:
# English translations for these labels used in the console app. console.en.welcome = Welcome to the exciting console application! console.en.get_started = To get started click on the "Run" button! console.en.quit.label = Quit console.en.quit.ask = Are you sure you want to quit?
As this file grows with all the languages that the console application will support, this becomes tediously repetitive and inefficient.
The extended syntax allows scoping with the {
and }
brace characters and automatically concatenates the sub keys together when parsing.
With the extended feature this becomes:
# English translations for these labels used in the console app. console. { en. { welcome = Welcome to the exciting console application! get_started = To get started click on the "Run" button! quit. { label = Quit ask = Are you sure you want to quit? } } }
The parsed keys will look identical to the first example in code.
Important
Remember this is combining the outer key and inner keys together! Therefore you must add the dot (.) postfix to the outer keys or use some other separator, otherwise parsed keys will be hard to read.
Here is a very simple example to show you how to get started:
let body = "my-app.{ service-in-use=\"{}\"\\ is in use. ask-delete=You want to delete \"{}\"? ask-restart={} already has a running service.\\ Do you want to restart it? } mail-service.{ default=Which mail do you want to read? empty= You have no mail } # General YES/NO labels *.{ yes=YES no=NO }"; let hash = super::parse_string(body); for (key, val) in &hash { println!("key={}, val={}", key, val); } assert_eq!(hash.len(), 7); assert_eq!( hash.get("my-app.service-in-use").unwrap(), "\"{}\"\nis in use." );
Tip
See more in the tests defined in lib.rs
.
This project is licensed under the Apache License, Version 2.0.