Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README example code #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,54 @@ Where the documentation is ambiguous or incomplete, behavior is based on the beh

## Example
```rust
#![allow(unused)]

use java_properties::read;
use java_properties::write;
use java_properties::PropertiesIter;
use java_properties::PropertiesWriter;
use std::collections::HashMap;
use std::env::temp_dir;
use std::fs::File;
use std::io::prelude::*;
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");
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)?;

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

// Writing advanced
let mut src_map2 = HashMap::new();
src_map2.insert("c".to_string(), "d".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 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);

// 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)?;
println!("file: {:#?}", file_name.as_path());

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