Skip to main content

diffx_core/parser/
ini.rs

1use anyhow::Result;
2use serde_json::Value;
3
4pub fn parse_ini(content: &str) -> Result<Value> {
5    let mut result = serde_json::Map::new();
6    let mut current_section = String::new();
7    let mut global_section = serde_json::Map::new();
8
9    for line in content.lines() {
10        let line = line.trim();
11
12        if line.is_empty() || line.starts_with(';') || line.starts_with('#') {
13            continue;
14        }
15
16        if line.starts_with('[') && line.ends_with(']') {
17            current_section = line[1..line.len() - 1].to_string();
18            result.insert(
19                current_section.clone(),
20                Value::Object(serde_json::Map::new()),
21            );
22        } else if let Some(eq_pos) = line.find('=') {
23            let key = line[..eq_pos].trim().to_string();
24            let value = line[eq_pos + 1..].trim().to_string();
25
26            if current_section.is_empty() {
27                global_section.insert(key, Value::String(value));
28            } else if let Some(Value::Object(section)) = result.get_mut(&current_section) {
29                section.insert(key, Value::String(value));
30            }
31        }
32    }
33
34    // Add global section if it exists
35    if !global_section.is_empty() {
36        result.insert("default".to_string(), Value::Object(global_section));
37    }
38
39    Ok(Value::Object(result))
40}