1. Glue for Rust
  2. Parsing INI files

Defining the format

Lets start by outlining exactly what we will be parsing:

  • Line Break: a new line character.

  • Line Space: zero or more whitespace characters except any Line Break.

  • Comment: a ; character and anything that follows until the next Line Break.

  • Name: one or more alphanumeric, _ or - characters.

  • Separator: zero or more Line Spaces followed by a = character then more Line Spaces.

  • Values: one or more characters, except for ; and Line Breaks.

  • Property: a Name followed by a Separator and a Value and/or a Comment.

  • Items: any number of Line Breaks, Properties, or Comments until either the end of input or until a Section is found.

  • Section: a [ character followed by a Name and a trailing ] character and optionally some Items make up the contents of the section.

  • Document: any number of Sections, Comments or Line Breaks.

The sample we'll be parsing today looks like this:

[abcd]; section level comment
property=value; abc
another_property = value2

; document level comment
some_property=; no value set

Before we start defining parsers, we will need a data structure to store what we find in:

#[derive(Debug, PartialEq)]
enum Ini {
    Property(String, Vec<Ini>),
    Section(String, Vec<Ini>),