Skip to content

Commit

Permalink
Restrict lifetimes of SectionSetter methods (#135)
Browse files Browse the repository at this point in the history
* rustfmt

* SectionSetter::get should take &self, not &mut self

* restrict lifetime of SectionSetter to enable both chained and separate calls to set, add and delete

* add test for chained and separate SectionSetter calls
  • Loading branch information
rikyborg authored Aug 2, 2024
1 parent ecad660 commit 35dc113
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,11 @@ impl<'a> SectionSetter<'a> {
}

/// 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>
pub fn set<'b, K, V>(&'b mut self, key: K, value: V) -> &'b mut SectionSetter<'a>
where
K: Into<String>,
V: Into<String>,
'a: 'b,
{
self.ini
.entry(self.section_name.clone())
Expand All @@ -366,10 +367,11 @@ impl<'a> SectionSetter<'a> {
}

/// Add (append) key-value pair in this section
pub fn add<K, V>(&'a mut self, key: K, value: V) -> &'a mut SectionSetter<'a>
pub fn add<'b, K, V>(&'b mut self, key: K, value: V) -> &'b mut SectionSetter<'a>
where
K: Into<String>,
V: Into<String>,
'a: 'b,
{
self.ini
.entry(self.section_name.clone())
Expand All @@ -380,7 +382,11 @@ impl<'a> SectionSetter<'a> {
}

/// Delete the first entry in this section with `key`
pub fn delete<K: AsRef<str>>(&'a mut self, key: &K) -> &'a mut SectionSetter<'a> {
pub fn delete<'b, K>(&'b mut self, key: &K) -> &'b mut SectionSetter<'a>
where
K: AsRef<str>,
'a: 'b,
{
for prop in self.ini.section_all_mut(self.section_name.as_ref()) {
prop.remove(key);
}
Expand All @@ -389,7 +395,7 @@ impl<'a> SectionSetter<'a> {
}

/// Get the entry in this section with `key`
pub fn get<K: AsRef<str>>(&'a mut self, key: K) -> Option<&'a str> {
pub fn get<K: AsRef<str>>(&'a self, key: K) -> Option<&'a str> {
self.ini
.section(self.section_name.as_ref())
.and_then(|prop| prop.get(key))
Expand Down Expand Up @@ -2348,29 +2354,23 @@ bar = f
fn add_properties_api() {
// Test duplicate properties in a section
let mut ini = Ini::new();
ini.with_section(Some("foo"))
.add("a", "1")
.add("a", "2");
ini.with_section(Some("foo")).add("a", "1").add("a", "2");

let sec = ini.section(Some("foo")).unwrap();
assert_eq!(sec.get("a"), Some("1"));
assert_eq!(sec.get_all("a").collect::<Vec<&str>>(), vec!["1", "2"]);

// Test add with unique keys
let mut ini = Ini::new();
ini.with_section(Some("foo"))
.add("a", "1")
.add("b", "2");
ini.with_section(Some("foo")).add("a", "1").add("b", "2");

let sec = ini.section(Some("foo")).unwrap();
assert_eq!(sec.get("a"), Some("1"));
assert_eq!(sec.get("b"), Some("2"));

// Test string representation
let mut ini = Ini::new();
ini.with_section(Some("foo"))
.add("a", "1")
.add("a", "2");
ini.with_section(Some("foo")).add("a", "1").add("a", "2");
let mut buf = Vec::new();
ini.write_to(&mut buf).unwrap();
let ini_str = String::from_utf8(buf).unwrap();
Expand Down Expand Up @@ -2636,4 +2636,37 @@ x3 = nb
]
);
}

#[test]
fn section_setter_chain() {
// fix issue #134

let mut ini = Ini::new();
let mut section_setter = ini.with_section(Some("section"));

// chained set() calls work
section_setter.set("a", "1").set("b", "2");
// separate set() calls work
section_setter.set("c", "3");

assert_eq!("1", section_setter.get("a").unwrap());
assert_eq!("2", section_setter.get("b").unwrap());
assert_eq!("3", section_setter.get("c").unwrap());

// overwrite values
section_setter.set("a", "4").set("b", "5");
section_setter.set("c", "6");

assert_eq!("4", section_setter.get("a").unwrap());
assert_eq!("5", section_setter.get("b").unwrap());
assert_eq!("6", section_setter.get("c").unwrap());

// delete entries
section_setter.delete(&"a").delete(&"b");
section_setter.delete(&"c");

assert!(section_setter.get("a").is_none());
assert!(section_setter.get("b").is_none());
assert!(section_setter.get("c").is_none());
}
}

0 comments on commit 35dc113

Please sign in to comment.