| Crates.io | ini_parser_hromiak |
| lib.rs | ini_parser_hromiak |
| version | 0.1.0 |
| created_at | 2025-11-11 20:04:03.966371+00 |
| updated_at | 2025-11-11 20:04:03.966371+00 |
| description | A parser for INI configuration files that parses sections, keys, and values into a structured format. |
| homepage | |
| repository | https://github.com/OlexiyHR/INI_Parser |
| max_upload_size | |
| id | 1928116 |
| size | 26,257 |
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.
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.
Section Detection – Each section is recognized by square brackets [Section].
Key-Value Extraction – Each line with the pattern key = value is parsed as a configuration entry.
Comments and Whitespace – Lines starting with ; or #, as well as empty lines, are ignored.
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.
//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)* }
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",
},
}
This parser is available as a Rust crate at https://crates.io/crates/ini_parser_hromiak