-
Notifications
You must be signed in to change notification settings - Fork 80
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
fabc4f3
4340be6
c5ebd8f
63ba0dc
d2901d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ use std::{ | |
ops::{Index, IndexMut}, | ||
path::Path, | ||
str::Chars, | ||
collections::HashMap, | ||
}; | ||
|
||
use cfg_if::cfg_if; | ||
|
@@ -1028,6 +1029,34 @@ impl Ini { | |
Ini::load_from_file_opt(filename, ParseOption::default()) | ||
} | ||
|
||
/// Load from files;overwrite and append | ||
pub fn load_from_files<P: AsRef<Path>>(filenames: Vec<P>) -> Result<Ini, Error> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You haven't change the type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(§ion) { | ||
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( | ||
|
@@ -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")); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.