INI Parsing

RecipeCratesCategories
rust-inicat-parser-implementations

Parse INI Configuration files with rust-ini

rust-ini rust-ini-crates.io rust-ini-github rust-ini-lib.rs

rust-ini is an INI configuration file parsing library.

use ini::Ini;

// INI is an informal standard for configuration files for some platforms or
// software. INI files are simple text files with a basic structure composed of
// "sections" and "properties".

// Read an INI configuration
fn main() -> anyhow::Result<()> {
    let config_str = r#"
[section1]
key1 = value1
key2 = value2

[section2]
key3 = value3
key4 = value4
"#;

    let ini = Ini::load_from_str(config_str)?;
    // Or use: Ini::load_from_file("conf.ini")

    // Get the first value for the given key in the given section
    let value1 = ini.get_from(Some("section1"), "key1");
    let value3 = ini.get_from(Some("section2"), "key3");

    println!("Value for key1: {:?}", value1);
    println!("Value for key3: {:?}", value3);

    // Or get a section, then retrieve the key
    let section = ini.section(Some("section2")).unwrap();
    let _value4 = section.get("key4").unwrap();

    // Or iterate through key value pairs
    for (sec, prop) in &ini {
        println!("Section: {:?}", sec);
        for (key, value) in prop.iter() {
            println!("{:?}:{:?}", key, value);
        }
    }

    // Write a .ini configuration file
    let mut conf = Ini::new();
    conf.with_section(Some("Section3"))
        .set("key5", "value5")
        .set("key6", "value6");
    conf.write_to_file("temp/conf.ini").unwrap();

    Ok(())
}