This project is a Rust-based parser for INI configuration files. INI files are a simple, human-readable format for storing configuration data, where data is organized into sections, each containing key-value pairs.
The parser reads an INI file and parses its contents into a structured format. The output is a Config
structure, which contains a collection of Section
structs, each holding key-value pairs. This parser is useful for reading and manipulating configuration files commonly used in applications, games, and system settings.
The parsing process follows these steps:
[section_name]
).=
), with the key on the left and the value on the right.;
), are ignored during parsing.The result of the parsing is stored in a Config
structure, which contains multiple Section
structs. Each Section
contains a collection of KeyValue
pairs.
To use the INI parser, simply provide the content of the INI file as a string to the parse_ini
function. The function will return a Config
object, which can be further used to access the parsed sections and key-value pairs.
WHITESPACE = _{ " " | "\t" }
file = { SOI ~ (NEWLINE* ~ (comment | section) ~ NEWLINE*)* ~ EOI }
section = {"[" ~ name ~ "]" ~ NEWLINE+ ~ (NEWLINE* ~ (comment | pair))* ~ NEWLINE+}
name = @{ UPPERCASE_LETTER+ }
pair = { key ~ "=" ~ (array_value | value) }
key = @{ (ALPHABETIC)+ }
value = @{ (!("," | WHITESPACE | NEWLINE | "[" | "]") ~ ANY)+ }
array_value = { "[" ~ WHITESPACE* ~ value ~ (WHITESPACE* ~ "," ~ WHITESPACE* ~ value)* ~ WHITESPACE* ~ "]" }
comment = { ";" ~ (!NEWLINE ~ ANY)* }
let ini_content = r#"
; This is a comment
[section1]
key1=value1
key2=value2
[section2]
keyA=valueA
"#;
let config = parse_ini(ini_content)?;
let first = config.get_value("key1") // result is Some("value1")
let second = config.get_value("aaaaaa") // result is None
let third = config.get_value_in_section("section1", "key2") // result is Some("value2")
Documentation: https://docs.rs/ini_file_parser/latest/ini_file_parser/index.html