OXYGENHUB

Coverage / crates/oxy-strata/src/ini.rs 100.0%

28 of 28 lines run, 3 of 5 functions · run #277 on switch-says · run missed

1
//! The INI files the AWS tools keep (`~/.aws/credentials`, `~/.aws/config`):
2
//! `[section]` headings and `key = value` lines, nothing more.
3
4
use std::collections::BTreeMap;
5
6
pub type Sections = BTreeMap<String, BTreeMap<String, String>>;
7
83×
pub fn parse(text: &str) -> Sections {
93×
    let mut out = Sections::new();
103×
    let mut current: Option<String> = None;
1113×
    for raw in text.lines() {
1213×
        let line = raw.trim();
1313×
        if line.is_empty() || line.starts_with('#') || line.starts_with(';') {
142×
            continue;
1511×
        }
1611×
        if let Some(name) = line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) {
175×
            current = Some(name.trim().to_string());
185×
            out.entry(name.trim().to_string()).or_default();
195×
            continue;
206×
        }
216×
        if let (Some(section), Some((k, v))) = (&current, line.split_once('=')) {
226×
            out.entry(section.clone())
236×
                .or_default()
246×
                .insert(k.trim().to_string(), v.trim().to_string());
256×
        }
26
    }
273×
    out
283×
}
29
30
#[cfg(test)]
31
mod tests {
32
    use super::parse;
33
34
    #[test]
351×
    fn sections_and_keys() {
361×
        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");
371×
        assert_eq!(s["default"]["region"], "eu-west-2");
381×
        assert_eq!(s["profile work"]["region"], "us-east-1");
391×
        assert_eq!(s["profile work"]["sso_start_url"], "https://x");
401×
        assert!(parse("").is_empty());
411×
    }
42
}