//! The INI files the AWS tools keep (`~/.aws/credentials`, `~/.aws/config`): //! `[section]` headings and `key = value` lines, nothing more. use std::collections::BTreeMap; pub type Sections = BTreeMap>; pub fn parse(text: &str) -> Sections { let mut out = Sections::new(); let mut current: Option = None; for raw in text.lines() { let line = raw.trim(); if line.is_empty() || line.starts_with('#') || line.starts_with(';') { continue; } if let Some(name) = line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) { current = Some(name.trim().to_string()); out.entry(name.trim().to_string()).or_default(); continue; } if let (Some(section), Some((k, v))) = (¤t, line.split_once('=')) { out.entry(section.clone()) .or_default() .insert(k.trim().to_string(), v.trim().to_string()); } } out } #[cfg(test)] mod tests { use super::parse; #[test] fn sections_and_keys() { let s = parse("[default]\nregion = eu-west-2\n\n# a comment\n[profile work]\nregion=us-east-1\nsso_start_url = https://x\n"); assert_eq!(s["default"]["region"], "eu-west-2"); assert_eq!(s["profile work"]["region"], "us-east-1"); assert_eq!(s["profile work"]["sso_start_url"], "https://x"); assert!(parse("").is_empty()); } }