#java #properties

java-properties

A library for reading and writing Java properties files in Rust

10 releases (stable)

1.4.1 Mar 10, 2022
1.4.0 Jul 9, 2021
1.3.0 Oct 25, 2020
1.2.0 Feb 24, 2019
0.1.1 Feb 6, 2016

#250 in Development tools

Download history 8836/week @ 2022-08-12 8929/week @ 2022-08-19 8091/week @ 2022-08-26 7622/week @ 2022-09-02 7217/week @ 2022-09-09 7826/week @ 2022-09-16 7469/week @ 2022-09-23 7185/week @ 2022-09-30 7502/week @ 2022-10-07 10329/week @ 2022-10-14 9022/week @ 2022-10-21 9123/week @ 2022-10-28 10278/week @ 2022-11-04 8482/week @ 2022-11-11 8708/week @ 2022-11-18 6397/week @ 2022-11-25

35,794 downloads per month
Used in 29 crates (10 directly)

MIT license

41KB
1K SLoC

Java properties for Rust

This is a library for reading and writing Java properties files in Rust. The specification is taken from the Properties documentation. Where the documentation is ambiguous or incomplete, behavior is based on the behavior of java.util.Properties.

Example

use std::collections::HashMap;
use std::env::temp_dir;
use std::fs::File;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::prelude::*;

let mut file_name = temp_dir();
file_name.push("java-properties-test.properties");

// Writing
let mut map1 = HashMap::new();
map1.insert("a".to_string(), "b".to_string());
let mut f = File::create(&file_name)?;
write(BufWriter::new(f), &map1)?;

// Reading
let mut f = File::open(&file_name)?;
let map2 = read(BufReader::new(f))?;
assert_eq!(src_map1, dst_map1);

lib.rs:

Utilities for reading and writing Java properties files

The specification is taken from https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html. Where the documentation is ambiguous or incomplete, behavior is based on the behavior of java.util.Properties.

Examples

use java_properties::PropertiesIter;
use java_properties::PropertiesWriter;
use java_properties::read;
use java_properties::write;
use std::collections::HashMap;
use std::env::temp_dir;
use std::fs::File;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::prelude::*;

# fn main() -> std::result::Result<(), java_properties::PropertiesError> {
let mut file_name = temp_dir();
file_name.push("java-properties-test.properties");

// Writing simple
let mut src_map1 = HashMap::new();
src_map1.insert("a".to_string(), "b".to_string());
let mut f = File::create(&file_name)?;
write(BufWriter::new(f), &src_map1)?;

// Writing advanced
let mut src_map2 = HashMap::new();
src_map2.insert("a".to_string(), "b".to_string());
let mut f = File::create(&file_name)?;
let mut writer = PropertiesWriter::new(BufWriter::new(f));
for (k, v) in &src_map2 {
  writer.write(&k, &v)?;
}
writer.flush();

// Reading simple
let mut f2 = File::open(&file_name)?;
let dst_map1 = read(BufReader::new(f2))?;
assert_eq!(src_map1, dst_map1);

// Reading advanced
let mut f = File::open(&file_name)?;
let mut dst_map2 = HashMap::new();
PropertiesIter::new(BufReader::new(f)).read_into(|k, v| {
  dst_map2.insert(k, v);
})?;
assert_eq!(src_map2, dst_map2);
# Ok(())
# }

Dependencies

~3.5MB
~68K SLoC