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

Add conversion from Section to INI #90

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
32 changes: 31 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ impl Properties {
self.data.remove_all(property_get_key!(s.as_ref()))
}

pub fn as_ini(&self, section_name: Option<&str>) -> Ini {
let mut ini_root = ListOrderedMultimap::<SectionKey, Properties>::default();
ini_root.append(section_key!(section_name), self.clone());
Ini {
sections: ini_root
}
}

fn get_mut<S: AsRef<str>>(&mut self, s: S) -> Option<&mut str> {
self.data.get_mut(property_get_key!(s.as_ref())).map(|v| v.as_mut_str())
}
Expand Down Expand Up @@ -654,7 +662,7 @@ impl Ini {
self.sections.keys_len()
}

/// Check if object coutains no section
/// Check if object contains no section
pub fn is_empty(&self) -> bool {
self.sections.is_empty()
}
Expand Down Expand Up @@ -1969,6 +1977,28 @@ a3 = n3
}
}

#[test]
fn ini_subset() {
let input = r"
[foo]
f1=f1

[bar]
b1=b1
";
let data = Ini::load_from_str(input).unwrap();
let foo_section = data.section(Some("foo")).unwrap();
let ini_subset = foo_section.as_ini(Some("foo"));

let mut buf = vec![];
ini_subset.write_to(&mut buf).unwrap();

assert_eq!(String::from_utf8(buf).unwrap(), r"[foo]
f1=f1
"
);
}

#[test]
fn duplicate_sections() {
// https://github.com/zonyitoo/rust-ini/issues/49
Expand Down