Struct tini::Ini
[−]
[src]
pub struct Ini { /* fields omitted */ }
Structure for INI-file data
Methods
impl Ini
[src]
fn new() -> Ini
Create an empty Ini
fn from_file<S: AsRef<Path> + ?Sized>(path: &S) -> Result<Ini, Error>
Construct Ini from file
Errors
Errors returned by File::open()
and BufReader::read_to_string()
Examples
You may use Path
use std::path::Path; use tini::Ini; let path = Path::new("./examples/example.ini"); let conf = Ini::from_file(path); assert!(conf.ok().is_some());
or &str
use tini::Ini; let conf = Ini::from_file("./examples/example.ini"); assert!(conf.ok().is_some());
fn from_buffer<S: Into<String>>(buf: S) -> Ini
Construct Ini from buffer
Example
use tini::Ini; let conf = Ini::from_buffer("[section]\none = 1"); let value: Option<u8> = conf.get("section", "one"); assert_eq!(value, Some(1));
fn section<S: Into<String>>(self, name: S) -> Self
Set section name for following item()
s. This function doesn't create a
section.
Example
use tini::Ini; let conf = Ini::new().section("empty"); assert_eq!(conf.to_buffer(), String::new());
fn item<S: Into<String>>(self, name: S, value: S) -> Self
Add key-value pair to last section
Example
use tini::Ini; let conf = Ini::new().section("test") .item("value", "10"); let value: Option<u8> = conf.get("test", "value"); assert_eq!(value, Some(10));
fn to_file<S: AsRef<Path> + ?Sized>(&self, path: &S) -> Result<(), Error>
Write Ini to file. This function is similar to from_file
in use.
Errors
Errors returned by File::create()
and BufWriter::write_all()
fn to_buffer(&self) -> String
Write Ini to buffer
Example
use tini::Ini; let conf = Ini::from_buffer("[section]\none = 1"); // you may use `conf.to_buffer()` let value: String = conf.to_buffer(); // or format!("{}", conf); // let value: String = format!("{}", conf); // but the result will be the same assert_eq!(value, "[section]\none = 1".to_owned());
fn get<T: FromStr>(&self, section: &str, key: &str) -> Option<T>
Get scalar value of key in section
Example
use tini::Ini; let conf = Ini::from_buffer("[section]\none = 1"); let value: Option<u8> = conf.get("section", "one"); assert_eq!(value, Some(1));
fn get_vec<T>(&self, section: &str, key: &str) -> Option<Vec<T>> where
T: FromStr,
T: FromStr,
Get vector value of key in section
The function returns None
if one of the elements can not be parsed.
Example
use tini::Ini; let conf = Ini::from_buffer("[section]\nlist = 1, 2, 3, 4"); let value: Option<Vec<u8>> = conf.get_vec("section", "list"); assert_eq!(value, Some(vec![1, 2, 3, 4]));
fn iter(&self) -> IniIter
Iterate over all sections, yielding pairs of section name and iterator
over the section elements. The concrete iterator element type is
(&'a String, std::collections::hash_map::Iter<'a, String, String>)
.
Example
use tini::Ini; let conf = Ini::new().section("foo") .item("item", "value") .item("other", "something") .section("bar") .item("one", "1"); for (section, iter) in conf.iter() { for (key, val) in iter { println!("section: {} key: {} val: {}", section, key, val); } }
fn iter_mut(&mut self) -> IniIterMut
Iterate over all sections, yielding pairs of section name and mutable
iterator over the section elements. The concrete iterator element type is
(&'a String, std::collections::hash_map::IterMut<'a, String, String>)
.
Example
use tini::Ini; let mut conf = Ini::new().section("foo") .item("item", "value") .item("other", "something") .section("bar") .item("one", "1"); for (section, iter_mut) in conf.iter_mut() { for (key, val) in iter_mut { *val = String::from("replaced"); } }