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

Feat:read section from multiple files;meger and override key value #141

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
56 changes: 56 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use std::{
ops::{Index, IndexMut},
path::Path,
str::Chars,
collections::HashMap,
};

use cfg_if::cfg_if;
Expand Down Expand Up @@ -1028,6 +1029,34 @@ impl Ini {
Ini::load_from_file_opt(filename, ParseOption::default())
}

/// Load from files;overwrite and append
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write a complete document about this API.

pub fn load_from_files<P: AsRef<Path>>(filenames: Vec<P>) -> Result<Ini, Error> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filenames: &[P] would be better?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You haven't change the type of filenames yet.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, filenames: IntoIter<Item=P> may also be a good choice.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not fixed yet.

let mut merged = Ini::new();
let mut section_2_props:HashMap<Option<String>, Properties> = HashMap::new();
for filename in filenames{
zonyitoo marked this conversation as resolved.
Show resolved Hide resolved
match Ini::load_from_file(filename) {
Ok(ini) => {
for (section, props) in ini.sections {
if let Some(section_props) = section_2_props.get_mut(&section) {
for (key, value) in props {
section_props.insert(key, value);
}
} else {
section_2_props.insert(section, props);
}
}
}
Err(e) => return Err(e),
}
}
for (section, props) in section_2_props {
merged.sections.insert(section, props);
}
Ok(merged)
//Ini::load_from_file_opt(filename, ParseOption::default())
zonyitoo marked this conversation as resolved.
Show resolved Hide resolved
}


/// Load from a file, but do not interpret '\' as an escape character
pub fn load_from_file_noescape<P: AsRef<Path>>(filename: P) -> Result<Ini, Error> {
Ini::load_from_file_opt(
Expand Down Expand Up @@ -2795,4 +2824,31 @@ bla = a
}
assert_eq!(bla, "a");
}

#[test]
fn test_load_from_files() {
//Test both overwrite and append

use std::vec;
let file_name1 = temp_dir().join("rust_ini_load_xxx1");
let file_content1 = "[Test]Key=Value\n";

{
let mut file = File::create(&file_name1).expect("create");
file.write_all(file_content1.as_bytes()).expect("write");
}

let file_name2 = temp_dir().join("rust_ini_load_xxx2");
let file_content2 = "[Test]Key=Value2\nKey2=Value3\n";

{
let mut file = File::create(&file_name2).expect("create");
file.write_all(file_content2.as_bytes()).expect("write");
}

let ini = Ini::load_from_files(vec![&file_name1, &file_name2]).unwrap();
assert_eq!(ini.get_from(Some("Test"), "Key"), Some("Value2"));
assert_eq!(ini.get_from(Some("Test"), "Key2"), Some("Value3"));
}

}
Loading