ini_parser_hromiak

Crates.ioini_parser_hromiak
lib.rsini_parser_hromiak
version0.1.0
created_at2025-11-11 20:04:03.966371+00
updated_at2025-11-11 20:04:03.966371+00
descriptionA parser for INI configuration files that parses sections, keys, and values into a structured format.
homepage
repositoryhttps://github.com/OlexiyHR/INI_Parser
max_upload_size
id1928116
size26,257
Oleksii Hromiak (OlexiyHR)

documentation

README

INI Parser

Brief Description

The INI Parser is a Rust-based parser that reads and interprets configuration data written in the .ini format. It can process files, extracting sections, keys, and values, and converting them into a structured form. This makes it easy for other programs to access configuration settings, validate them, or use them during runtime.

How the Parsing Works

The parsing process involves using the pest library to define grammar rules that describe the structure of an INI file.
The parser identifies sections, key-value pairs, and ignores comments and whitespace to produce clean, structured output.

Parsing Steps

  1. Section Detection – Each section is recognized by square brackets [Section].

  2. Key-Value Extraction – Each line with the pattern key = value is parsed as a configuration entry.

  3. Comments and Whitespace – Lines starting with ; or #, as well as empty lines, are ignored.

  4. Data Structuring – Parsed data is stored as a nested map structure:

    HashMap<String, HashMap<String, String>>
    

    where the outer map stores section names, and the inner map stores key-value pairs for each section.

Grammar

//A single space or tab character.
WHITESPACE = _{ " " | "\t" }

/// Line break (Windows or Unix style)
NEWLINE = _{ "\r\n" | "\n" }

/// Whole file: may contain zero or more sections, comments, or newlines.
file = { SOI ~ (section | comment | NEWLINE)* ~ EOI }

/// Comment line: starts with `;` or `#`, and continues until the end of the line.
comment = _{ (";" | "#") ~ (!NEWLINE ~ ANY)* ~ NEWLINE? }

/// Section: starts with `[name]` and may contain key-value pairs, comments, or newlines.
section = { "[" ~ name ~ "]" ~ (NEWLINE)* ~ (pair | comment | NEWLINE)* }

/// Key-value pair: `key = value`
pair = { key ~ WHITESPACE* ~ "=" ~ WHITESPACE* ~ value ~ NEWLINE* }

/// Section or key name: allows alphanumeric characters, underscores, and dashes.
name = @{ (ASCII_ALPHANUMERIC | "_" | "-")+ }

/// Key name: same rules as `name`.
key  = @{ (ASCII_ALPHANUMERIC | "_" | "-")+ }

/// Value: everything until a newline (including spaces or symbols).
value = @{ (!NEWLINE ~ ANY)* }

Example: Parsing a Simple INI File

For the input:

; This is the main configuration section
[main]
username = alice
password = secret

# Timeout settings
timeout = 30
retries = 5

; Empty lines are allowed
; Another comment line

[database]
host = localhost
port = 5432

# Credentials
user = dbuser
password = dbpass

; End of configuration

Running:

cargo run

Produces the parsed structure:

{
    "database": {
        "password": "dbpass",
        "user": "dbuser",
        "host": "localhost",
        "port": "5432",
    },
    "main": {
        "retries": "5",
        "timeout": "30",
        "username": "alice",
        "password": "secret",
    },
}

Crates.io

This parser is available as a Rust crate at https://crates.io/crates/ini_parser_hromiak

Commit count: 0

cargo fmt