Function parse_ini_content

Source
pub fn parse_ini_content(content: &str) -> Result<Ini, String>
Expand description

Parses INI file content into a configuration object without file I/O.

This is a pure function that takes raw INI text and parses it into an Ini struct. It’s useful for testing configuration parsing logic without reading from disk.

§Arguments

  • content - The raw INI file content as a string

§Returns

  • Ok(Ini) - Successfully parsed configuration
  • Err(String) - Parsing failed with error description

§INI Format

The INI format supported:

[section_name]
key1 = value1
key2 = value2

[another_section]
key3 = value3

§Examples

use rusty_commit_saver::config::parse_ini_content;

let ini_content = r#"
[obsidian]
root_path_dir = ~/Documents/Obsidian
commit_path = Diaries/Commits

[templates]
commit_date_path = %Y/%m-%B/%F.md
commit_datetime = %Y-%m-%d %H:%M:%S
"#;

let config = parse_ini_content(ini_content).unwrap();

// Access parsed values
assert_eq!(
    config.get("obsidian", "root_path_dir"),
    Some("~/Documents/Obsidian".to_string())
);
assert_eq!(
    config.get("templates", "commit_date_path"),
    Some("%Y/%m-%B/%F.md".to_string())
);

§Errors

Returns an error if:

  • INI syntax is invalid (malformed sections or key-value pairs)
  • The content cannot be parsed as valid UTF-8

§Testing

This function is particularly useful for unit testing without needing to create temporary files:

use rusty_commit_saver::config::parse_ini_content;

fn test_config_parsing() {
    let test_config = "[section]\nkey=value\n";
    let result = parse_ini_content(test_config);
    assert!(result.is_ok());
}