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

Iterating over Ini yields "Section" type instead of "SectionIter" (2.0 - breaking change) #42

Merged
merged 4 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
118 changes: 43 additions & 75 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ impl Ini {
.and_then(|x| x.split(sep).map(|s| s.trim().parse()).collect::<Result<Vec<T>, _>>().ok())
}

/// An iterator visiting all key-value pairs in order of appearance in section.
/// An iterator visiting all key-value pairs of a section in order of appearance.
///
/// If section with given name doesn't exist in document, method returns empty iterator
///
Expand All @@ -491,14 +491,12 @@ impl Ini {
///
/// assert_eq!(conf.section_iter("absent").count(), 0);
/// ```
pub fn section_iter(&self, section: &str) -> SectionIter {
let section = self.document.get(section).unwrap_or(&self.empty_section);
SectionIter { document: &section, iter: section.iter() }
pub fn section_iter(&self, section: &str) -> ordered_hashmap::Iter<String, String> {
self.document.get(section).unwrap_or(&self.empty_section).iter()
}

/// Iterate over all sections in order of appearance, yielding pairs of
/// section name and iterator over the section elements. The iterator
/// element type is `(&'a String, SectionIter<'a>)`.
/// Iterate over all sections in order of appearance, yielding pairs of section name
/// and section instance, which can be iterated over or queried similarly to Ini instances.
///
/// # Example
/// ```
Expand All @@ -509,10 +507,10 @@ impl Ini {
/// .section("bar")
/// .item("one", "1");
///
/// for (name, section_iter) in conf.iter() {
/// for (name, section) in conf.iter() {
/// match name.as_str() {
/// "foo" => assert_eq!(section_iter.count(), 2),
/// "bar" => assert_eq!(section_iter.count(), 1),
/// "foo" => assert_eq!(section.iter().count(), 2),
/// "bar" => assert_eq!(section.iter().count(), 1),
/// _ => assert!(false),
/// }
/// }
Expand All @@ -533,14 +531,14 @@ impl Ini {
/// .section("bar")
/// .item("one", "1");
///
/// for (name, section_iter) in conf.iter_mut() {
/// for (key, val) in section_iter {
/// for (name, section) in conf.iter_mut() {
/// for (key, val) in section.iter_mut() {
/// *val = String::from("replaced");
/// }
/// }
///
/// for (name, section_iter) in conf.iter() {
/// for (key, val) in section_iter {
/// for (name, section) in conf.iter() {
/// for (key, val) in section.iter() {
/// assert_eq!(val.as_str(), "replaced");
/// }
/// }
Expand All @@ -556,7 +554,7 @@ impl fmt::Display for Ini {
// insert section block
items.push(format!("[{}]", name));
// add items
for (key, value) in section {
for (key, value) in section.iter() {
items.push(format!("{} = {}", key, value));
}
// and blank line between sections
Expand All @@ -572,91 +570,37 @@ impl Default for Ini {
}
}

/// An iterator over the sections of an ini documet
/// An iterator over the sections of an ini document
pub struct IniIter<'a> {
#[doc(hidden)]
iter: ordered_hashmap::Iter<'a, String, Section>,
}

impl<'a> Iterator for IniIter<'a> {
type Item = (&'a String, SectionIter<'a>);
type Item = (&'a String, &'a Section);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(name, section)| (name, SectionIter { document: &section, iter: section.iter() }))
self.iter.next()
}
}

/// A mutable iterator over the sections of an ini documet
/// A mutable iterator over the sections of an ini document
pub struct IniIterMut<'a> {
#[doc(hidden)]
iter: ordered_hashmap::IterMut<'a, String, Section>,
}

impl<'a> Iterator for IniIterMut<'a> {
type Item = (&'a String, SectionIterMut<'a>);
type Item = (&'a String, &'a mut Section);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(name, section)| (name, SectionIterMut { iter: section.iter_mut() }))
}
}

type Section = OrderedHashMap<String, String>;

/// An iterator over the entries of a section
pub struct SectionIter<'a> {
#[doc(hidden)]
document: &'a Section,
iter: ordered_hashmap::Iter<'a, String, String>,
}

impl<'a> Iterator for SectionIter<'a> {
type Item = (&'a String, &'a String);

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}

impl<'a> SectionIter<'a> {
/// Get scalar value of key
///
/// - output type `T` must implement [FromStr] trait for auto conversion
///
/// # Example
/// ```
/// # use tini::Ini;
/// let conf = Ini::from_string("[section]\nkey=1\nvalue=2").unwrap();
///
/// for (name, section) in conf.iter() {
/// let key = section.get("key");
/// let value = section.get("value");
/// assert_eq!(key, Some(1));
/// assert_eq!(value, Some(2));
/// }
/// ```
pub fn get<T>(&'a self, key: &str) -> Option<T>
where
T: FromStr,
{
self.document.get(key).and_then(|x| x.parse().ok())
dralley marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// A mutable iterator over the entries of a section
pub struct SectionIterMut<'a> {
#[doc(hidden)]
iter: ordered_hashmap::IterMut<'a, String, String>,
}

impl<'a> Iterator for SectionIterMut<'a> {
type Item = (&'a String, &'a mut String);

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}
type Section = OrderedHashMap<String, String>;

#[cfg(test)]
mod library_test {
Expand Down Expand Up @@ -748,4 +692,28 @@ mod library_test {
assert_eq!(["c", "b", "a"], keys[..]);
Ok(())
}

#[test]
fn random_access_section() -> Result<(), Error> {
let ini = Ini::from_string("[a]\nc = 1\nb = 2\na = 3\n\n[b]\na = 1\nb = 2\nc = 3\n\n[c]\na=0\nd=4")?;
for (name, section) in ini.iter() {
match name.as_str() {
"a" => {
assert_eq!(section.get("a"), Some(&"3".to_owned()));
assert_eq!(section.get("d"), None);
},
"b" => {
assert_eq!(section.get("a"), Some(&"1".to_owned()));
assert_eq!(section.get("d"), None);
},
"c" => {
assert_eq!(section.get("a"), Some(&"0".to_owned()));
assert_eq!(section.get("b"), None);
assert_eq!(section.get("d"), Some(&"4".to_owned()));
},
_ => unreachable!()
}
}
Ok(())
}
}
11 changes: 0 additions & 11 deletions src/ordered_hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,6 @@ where
}
}

impl<K, V> IntoIterator for OrderedHashMap<K, V>
where
K: Eq + Hash,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V>;

fn into_iter(self) -> Self::IntoIter {
IntoIter { base: self.base, keys_iterator: self.keys.into_iter() }
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The compiler complained about conflicting implementations


impl<K, V> FromIterator<(K, V)> for OrderedHashMap<K, V>
where
Expand Down