Crates.io | jprop |
lib.rs | jprop |
version | 0.2.0 |
created_at | 2025-09-07 14:47:53.277515+00 |
updated_at | 2025-09-13 16:47:33.427741+00 |
description | no-std parser for java .properties files that actually works |
homepage | |
repository | https://github.com/AlexanderSchuetz97/jprop |
max_upload_size | |
id | 1828198 |
size | 66,697 |
no-std parser for java .properties
files that actually works
As everyone should be aware, there are at least a dozen .properties parsers on crates.io; unfortunately, none of them work properly.
A glance into https://en.wikipedia.org/wiki/.properties should reveal that there are many things that can go wrong when trying to parse .properties files.
I have evaluated five different .properties parsers, and none of them are capable of parsing the files I need to parse, so I have come to the conclusion that I sadly have to make my own parser...
0 dependencies
no-std (feature gated, default features use std to allow for decoding of std::io::Read
)
full UTF-8 and ISO-8859-1 encoding support
decoding of 'byte' or 'string' sources
&[u8]
and friends&str
decoding of comments
document decoding
Vec<(String, String)>
or HashMap<String, String>
(std only),Vec<Element>
Element being an enum, which is either Comment or Key+Value pair.stream decoding
fn(Element) -> bool
Parse key, value as a HashMap<String, String>
use std::collections::HashMap;
use std::fs::File;
pub fn read_test_properties() {
let mut file = File::open("test.properties").expect("Failed to parse file");
let data: HashMap<String, String> = jprop::parse_utf8_to_map(&mut file).expect("Failed to parse file");
//use data here
//profit?
}
Parse the entire document as a stream.
use jprop::{Element, ParserPosition};
use std::fs::File;
//Your handler would probably have some fields and be more complex.
struct Handler;
impl jprop::PropertyHandler for Handler {
fn handle(&mut self, position: &ParserPosition, value: Element) -> bool {
println!("Position {}:{}", position.line+1, position.character_in_line+1);
match value {
Element::BlankLine => println!(),
Element::Comment(text) => println!("{}", text),
Element::Value(key, value) => println!("{} = {}", key, value),
}
true
}
}
pub fn read_test_properties_as_stream() {
let mut file = File::open("test.properties").expect("Failed to parse file");
let mut handler = Handler;
jprop::parse_utf8(&mut file, &mut handler).expect("Failed to parse file");
// use handler here
}
Parse from a &str
use std::collections::HashMap;
pub fn read_properties_from_string() {
let test = "abc=abc\nbcd=bcd\n#..."; //or 'include_str!("some_file.properties");' but that only works with utf-8 files.
let data: HashMap<String, String> = jprop::parse_str_to_map(test).expect("Failed to parse str");
//use data here
//profit?
}