ini
Read and write INI files: [section] headers followed by key = value lines, as used by many tools for settings.
import ini
let text = "; app settings\nname = demo\n\n[server]\nhost = example.com ; public name\nport = 8080\n"let conf = try ini.parse(&text)print(conf.get(&"", &"name"))print(conf.get(&"server", &"host"))let port = conf.get(&"server", &"port").unwrap_or("80").to_i64().unwrap_or(80)print(f"{port + 1}")print(conf.get(&"server", &"missing"))Some("demo")Some("example.com")8081NoneWhat parse accepts:
[name]starts a section. Keys before the first section belong to the section"".key = valueorkey: value; the first=or:separates them, so a value may contain more of them.- Lines starting with
;or#are comments, and so is everything after a;or#that follows a space. A#right after other text, as inhttp://x.org/#top, is part of the value. - A value in double quotes keeps its spaces and
;and knows the escapes\",\\,\nand\t. A value in single quotes is taken exactly as written. - An indented line continues the value above it, joined with a line break.
Everything is text: convert values with to_i64(), to_f64() or a comparison yourself. Section and key names are matched exactly, including case.
A section or a key that appears twice is an error, and so is a line that is neither a section, a key and value, nor a comment. Every IniError carries the line number.
Change a file and write it back
Section titled “Change a file and write it back”set changes a value or adds it at the end of its section (adding the section if needed), remove deletes one, and to_str writes the text. Values are quoted only where needed, so the result reads back the same.
import ini
let mut conf = try ini.parse(&"[server]\nhost = example.com\n")conf.set(&"server", &"host", &"example.org")conf.set(&"server", &"motd", &" hello; world ")conf.set(&"log", &"level", &"debug")print(conf.to_str())[server]host = example.orgmotd = " hello; world "
[log]level = debugComments and blank lines are not kept when a file is written back.
Reference
Section titled “Reference”import iniFunctions
Section titled “Functions”pub fn empty() -> IniAn INI file with nothing in it, to fill with set.
pub fn parse(text: &Str) -> Ini ! IniErrorReads INI text; a repeated section or key is an error, so a typo cannot hide a setting.
IniError
Section titled “IniError”pub struct IniError { pub line: i64 pub message: Str}pub struct Ini {}An INI file: sections of key = value lines, kept in their order; keys before any section are in “”.
It also has 1 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.
Ini.get
Section titled “Ini.get”pub fn get(&self, section: &Str, key: &Str) -> Option<Str>The value of key in section (“” for keys before the first section).
Ini.set
Section titled “Ini.set”pub fn set(&mut self, section: &Str, key: &Str, value: &Str)Sets a value, adding the section and the key at the end if they are new.
Ini.remove
Section titled “Ini.remove”pub fn remove(&mut self, section: &Str, key: &Str) -> boolRemoves a key; false if it was not there.
Ini.sections
Section titled “Ini.sections”pub fn sections(&self) -> Vec<Str>The section names in file order, without the unnamed one.
The section names in file order, without the unnamed section "".
Ini.entries
Section titled “Ini.entries”pub fn entries(&self, section: &Str) -> Vec<(Str, Str)>The (key, value) pairs of a section in file order; empty if there is no such section.
The (key, value) pairs of one section in file order, for going through every setting:
import ini
let conf = try ini.parse(&"[colors]\nerror = red\nwarning = yellow\n")for (key, value) in conf.entries(&"colors") { print(f"{key} -> {value}")}error -> redwarning -> yellowIni.to_str
Section titled “Ini.to_str”pub fn to_str(&self) -> StrINI text that parse reads back the same; values are quoted when spaces or ; need it.