Skip to content

Commit

Permalink
Fix #63, SectionSetter::set should replace value with key in Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Feb 12, 2020
1 parent a1a568b commit 350e894
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-ini"
version = "0.15.0"
version = "0.15.1"
authors = ["Y. T. Chung <[email protected]>"]
description = "An Ini configuration file parsing library in Rust"
repository = "https://github.com/zonyitoo/rust-ini"
Expand Down
43 changes: 33 additions & 10 deletions src/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl<'a> SectionSetter<'a> {
SectionSetter { ini, section_name }
}

/// Set key-value pair in this section (all with the same name)
/// Set (replace) key-value pair in this section (all with the same name)
pub fn set<K, V>(&'a mut self, key: K, value: V) -> &'a mut SectionSetter<'a>
where K: Into<String>,
V: Into<String>
Expand Down Expand Up @@ -358,15 +358,15 @@ impl Properties {
where K: Into<String>,
V: Into<String>
{
self.data.append(property_insert_key!(k.into()), v.into());
self.data.insert(property_insert_key!(k.into()), v.into());
}

/// Replace key with (key, value) pair
pub fn replace<K, V>(&mut self, k: K, v: V)
/// Append key with (key, value) pair
pub fn append<K, V>(&mut self, k: K, v: V)
where K: Into<String>,
V: Into<String>
{
self.data.insert(property_insert_key!(k.into()), v.into());
self.data.append(property_insert_key!(k.into()), v.into());
}

/// Get the first value associate with the key
Expand Down Expand Up @@ -1031,7 +1031,7 @@ impl<'a> Parser<'a> {
}
SectionEntry::Occupied(mut o) => {
// Insert into the last (current) section
o.last_mut().insert(curkey, mval);
o.last_mut().append(curkey, mval);
}
}
curkey = "".into();
Expand Down Expand Up @@ -1176,8 +1176,8 @@ mod test {
#[test]
fn property_get_vec() {
let mut props = Properties::new();
props.insert("k1", "v1");
props.insert("k1", "v2");
props.append("k1", "v1");
props.append("k1", "v2");

let res = props.get_all("k1").collect::<Vec<&str>>();
assert_eq!(res, vec!["v1", "v2"]);
Expand All @@ -1189,8 +1189,8 @@ mod test {
#[test]
fn property_remove() {
let mut props = Properties::new();
props.insert("k1", "v1");
props.insert("k1", "v2");
props.append("k1", "v1");
props.append("k1", "v2");

let res = props.remove_all("k1").collect::<Vec<String>>();
assert_eq!(res, vec!["v1", "v2"]);
Expand Down Expand Up @@ -1686,4 +1686,27 @@ bar = f
assert_eq!(Some("e"), p3.get("foo"));
assert_eq!(Some("f"), p3.get("bar"));
}

#[test]
fn fix_issue63() {
let section = "PHP";
let key = "engine";
let value = "On";
let new_value = "Off";

// create a new configuration
let mut conf = Ini::new();
conf.with_section(Some(section)).set(key, value);

// assert the value is the one expected
let v = conf.get_from(Some(section), key).unwrap();
assert_eq!(v, value);

// update the section/key with a new value
conf.set_to(Some(section), key.to_string(), new_value.to_string());

// assert the new value was set
let v = conf.get_from(Some(section), key).unwrap();
assert_eq!(v, new_value);
}
}

0 comments on commit 350e894

Please sign in to comment.