INI File Parser
Parse INI sections and key-value pairs into a sorted map, trimming quotes and rejecting malformed headers or empty keys.
Code
use std::collections::BTreeMap;
#[derive(Debug, Default)]
struct IniFile {
sections: BTreeMap<String, BTreeMap<String, String>>,
}
impl IniFile {
fn get(&self, section: &str, key: &str) -> Option<&str> {
self.sections.get(section)?.get(key).map(String::as_str)
}
}
fn parse_ini(input: &str) -> Result<IniFile, String> {
let mut ini = IniFile::default();
let mut section = String::from("default");
ini.sections.insert(section.clone(), BTreeMap::new());
for (index, raw_line) in input.lines().enumerate() {
let line = raw_line.trim();
if line.is_empty() || line.starts_with(';') || line.starts_with('#') {
continue;
}
if line.starts_with('[') {
if !line.ends_with(']') {
return Err(format!("line {}: unclosed section header", index + 1));
}
let name = line[1..line.len() - 1].trim();
if name.is_empty() {
return Err(format!("line {}: empty section name", index + 1));
}
section = name.to_string();
ini.sections.entry(section.clone()).or_default();
continue;
}
let (key, value) = match line.split_once('=') {
Some(pair) => pair,
None => return Err(format!("line {}: expected key=value", index + 1)),
};
let key = key.trim();
if key.is_empty() {
return Err(format!("line {}: empty key", index + 1));
}
ini.sections
.entry(section.clone())
.or_default()
.insert(key.to_string(), value.trim().trim_matches('"').to_string());
}
Ok(ini)
}
fn main() {
let document = r#"
; application settings
[server]
host = "0.0.0.0"
port = 8080
[database]
url = postgres://localhost/app
pool_size = 10
"#;
let ini = parse_ini(document).expect("valid ini");
println!("host: {:?}", ini.get("server", "host"));
println!("pool_size: {:?}", ini.get("database", "pool_size"));
println!("missing: {:?}", ini.get("server", "missing"));
println!("error: {:?}", parse_ini("[broken\nkey=1").is_err());
}How it works
The working surface is the functions get, parse_ini and main — everything else is wiring.
Iteration happens inside for (index, raw_line) in input.lines().enumerate() — everything outside it runs once. Errors ride back inside Result, forcing an explicit decision at every call site. The match expression is exhaustive — the compiler enforces that no case is missing.
Points of care
- Error handling is typed — each
Resultmust be matched or forwarded with?.
Using it
The main signature reads fn get(&self, section: &str, key: &str) -> Option<&str>
Running the file prints labeled results such as host:, pool_size:, missing: — each operation’s effect is visible in the output.
Execute the file and main walks through parse_ini and get, in that order.
Key statements
return Err(format!("line {}: unclosed section header", index + 1));
return Err(format!("line {}: empty section name", index + 1));
return Err(format!("line {}: empty key", index + 1));
Interactive Tools for This Topic
FAQ
- How do errors reach the caller?
- Through `Result` values. Nothing panics inside the snippet; the caller decides how to handle each failure.
- What does INI File Parser print when run?
- Run it as a complete program — the output lines carry labels such as `host:`, `pool_size:`, `missing:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.