Skip to content

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")
8081
None

What parse accepts:

  • [name] starts a section. Keys before the first section belong to the section "".
  • key = value or key: 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 in http://x.org/#top, is part of the value.
  • A value in double quotes keeps its spaces and ; and knows the escapes \", \\, \n and \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.

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.org
motd = " hello; world "
[log]
level = debug

Comments and blank lines are not kept when a file is written back.

import ini
pub fn empty() -> Ini

An INI file with nothing in it, to fill with set.

pub fn parse(text: &Str) -> Ini ! IniError

Reads INI text; a repeated section or key is an error, so a typo cannot hide a setting.

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.

pub fn get(&self, section: &Str, key: &Str) -> Option<Str>

The value of key in section (“” for keys before the first section).

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.

pub fn remove(&mut self, section: &Str, key: &Str) -> bool

Removes a key; false if it was not there.

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 "".

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 -> red
warning -> yellow
pub fn to_str(&self) -> Str

INI text that parse reads back the same; values are quoted when spaces or ; need it.