Struct tini::Ini [−][src]
pub struct Ini { /* fields omitted */ }
Expand description
Structure for INI-file data
Implementations
Create an empty Ini (similar to Ini::default)
Construct Ini from file
Errors
This function will return an Error if file cannot be opened or parsed
Examples
You may use Path
let path = Path::new("example.ini");
let conf = Ini::from_file(path);
assert!(conf.ok().is_some());
or &str
let conf = Ini::from_file("example.ini");
assert!(conf.ok().is_some());
Write Ini to file. This function is similar to from_file in use.
Errors
Errors returned by File::create and Write::write_all
Write Ini to any struct who implement Write trait.
Errors
Errors returned by Write::write_all
Example
let conf = Ini::default().section("a").item("a", 1);
// create output Vec<u8> buffer
let mut output = Vec::new();
// let's write data to Vec<u8>
conf.to_writer(&mut output);
// cast Vec<u8> to utf-8 string
let casted_result = String::from_utf8(output).unwrap();
assert_eq!(casted_result, "[a]\na = 1\n")
Set section name for the following methods in chain (item()
, items()
, etc.)
Warning
This function doesn’t create a section.
Example
let mut conf = Ini::new().section("empty");
assert_eq!(conf.to_string(), "");
// but section will be created on item() call
conf = conf.section("one").item("a", 1);
assert_eq!(conf.to_string(), "[one]\na = 1\n");
Add key-value pair to the end of section, specified in last section()
call,
or replace value if key already in section
Example
let mut conf = Ini::new().section("test")
.item("value", 10);
assert_eq!(conf.to_string(), "[test]\nvalue = 10\n");
// change existing value
conf = conf.section("test").item("value", "updated");
assert_eq!(conf.to_string(), "[test]\nvalue = updated\n");
Like item()
, but for vectors
name
must support Into to Stringvector
elements must support Display to support conversion to Stringsep
arbitrary string delimiter
Example
let conf = Ini::new()
.section("default")
// add a vector with `,` separator: 1,2,3,4
.item_vec_with_sep("a", &[1, 2, 3, 4], ",")
// add a vector with `|` separator: a|b|c
.item_vec_with_sep("b", &vec!["a", "b", "c"], "|");
let va: Option<Vec<u8>> = conf.get_vec("default", "a");
let vb: Vec<String> = conf.get_vec_with_sep("default", "b", "|").unwrap();
assert_eq!(va, Some(vec![1, 2, 3, 4]));
assert_eq!(vb, ["a", "b", "c"]);
Equivalent of item_vec_with_sep(name, vector, ", ")
name
must support Into to Stringvector
elements must support Display to support conversion to String
Example
let conf = Ini::new()
.section("default")
// add vector with default separator `, `
.item_vec("a", &[1, 2, 3, 4])
// and another vector
.item_vec("b", &vec!["a", "b", "c"]);
let va: Option<Vec<u8>> = conf.get_vec("default", "a");
let vb: Vec<String> = conf.get_vec("default", "b").unwrap();
assert_eq!(va, Some(vec![1, 2, 3, 4]));
assert_eq!(vb, ["a", "b", "c"]);
Append pairs from any object supporting IntoIterator to the section, specified in last section()
call.
Example
use std::collections::HashMap;
let mut conf = Ini::new()
.section("colors")
.items(vec![("black", "#000000"),
("white", "#ffffff")]);
// create custom section
let mut numbers = HashMap::new();
numbers.insert("round_pi", 3);
// and add to `conf`
conf = conf.section("numbers").items(numbers);
assert_eq!(conf.to_string(), [
"[colors]",
"black = #000000",
"white = #ffffff",
"",
"[numbers]",
"round_pi = 3",
""
].join("\n"));
Remove section from Ini.
Example
let mut config = Ini::from_string([
"[one]",
"a = 1",
"[two]",
"b = 2"
].join("\n")).unwrap();
// remove section
config = config.section("one").clear();
assert_eq!(config.to_string(), "[two]\nb = 2\n");
// clear section from old data and add new
config = config.section("two").clear().item("a", 1);
assert_eq!(config.to_string(), "[two]\na = 1\n");
Remove item from section.
Example
let mut config = Ini::from_string([
"[one]",
"a = 1",
"b = 2"
].join("\n")).unwrap();
config = config.section("one").erase("b");
assert_eq!(config.to_string(), "[one]\na = 1\n");
Get vector value of key
in section
. Value should use ,
as separator.
The function returns None if one of the elements can not be parsed.
- output type
T
must implement FromStr trait for auto conversion
Example
let conf = Ini::from_string("[section]\nlist = 1, 2, 3, 4").unwrap();
let value: Option<Vec<u8>> = conf.get_vec("section", "list");
assert_eq!(value, Some(vec![1, 2, 3, 4]));
Get vector value of key
in section
separated by sep
string.
The function returns None if one of the elements can not be parsed or not found.
- output type
T
must implement FromStr trait for auto conversion
Example
let conf = Ini::from_string("[section]\nlist = 1|2|3|4").unwrap();
let value: Option<Vec<u8>> = conf.get_vec_with_sep("section", "list", "|");
assert_eq!(value, Some(vec![1, 2, 3, 4]));
pub fn section_iter(&self, section: &str) -> SectionIter<'_>ⓘNotable traits for SectionIter<'a>impl<'a> Iterator for SectionIter<'a> type Item = (&'a String, &'a String);
pub fn section_iter(&self, section: &str) -> SectionIter<'_>ⓘNotable traits for SectionIter<'a>impl<'a> Iterator for SectionIter<'a> type Item = (&'a String, &'a String);
impl<'a> Iterator for SectionIter<'a> type Item = (&'a String, &'a String);
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
Example
let conf = Ini::from_string(["[search]",
"g = google.com",
"dd = duckduckgo.com"].join("\n")).unwrap();
let mut search = conf.section_iter("search");
assert_eq!(search.next(), Some((&"g".to_string(), &"google.com".to_string())));
assert_eq!(search.next(), Some((&"dd".to_string(), &"duckduckgo.com".to_string())));
assert_eq!(search.next(), None);
assert_eq!(conf.section_iter("absent").count(), 0);
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
let conf = Ini::new().section("foo")
.item("item", "value")
.item("other", "something")
.section("bar")
.item("one", "1");
for (name, section) in conf.iter() {
match name.as_str() {
"foo" => assert_eq!(section.iter().count(), 2),
"bar" => assert_eq!(section.iter().count(), 1),
_ => assert!(false),
}
}
pub fn iter_mut(&mut self) -> IniIterMut<'_>ⓘNotable traits for IniIterMut<'a>impl<'a> Iterator for IniIterMut<'a> type Item = (&'a String, &'a mut Section);
pub fn iter_mut(&mut self) -> IniIterMut<'_>ⓘNotable traits for IniIterMut<'a>impl<'a> Iterator for IniIterMut<'a> type Item = (&'a String, &'a mut Section);
impl<'a> Iterator for IniIterMut<'a> type Item = (&'a String, &'a mut Section);
Iterate over all sections in arbitrary order, yielding pairs of section name and mutable
iterator over the section elements. The concrete iterator element type is
(&'a String, SectionIterMut<'a>)
.
Example
let mut conf = Ini::new().section("foo")
.item("item", "value")
.item("other", "something")
.section("bar")
.item("one", "1");
for (name, section) in conf.iter_mut() {
for (key, val) in section.iter_mut() {
*val = String::from("replaced");
}
}
for (name, section) in conf.iter() {
for (key, val) in section.iter() {
assert_eq!(val.as_str(), "replaced");
}
}