Struct tini::Ini [] [src]

pub struct Ini { /* fields omitted */ }

Structure for INI-file data

Methods

impl Ini
[src]

Create an empty Ini

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());

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));

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());

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));

Write Ini to file. This function is similar to from_file in use.

Errors

Errors returned by File::create() and BufWriter::write_all()

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());

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));

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]));

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);
  }
}

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");
  }
}

Trait Implementations

impl Debug for Ini
[src]

Formats the value using the given formatter.

impl Display for Ini
[src]

Formats the value using the given formatter. Read more