Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 deletions src/file/format/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,46 @@ use std::error::Error;
use ini::Ini;

use crate::map::Map;
use crate::value::{Value, ValueKind};
use crate::value::{Table, Value, ValueKind};

pub fn parse(
uri: Option<&String>,
text: &str,
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
let mut map: Map<String, Value> = Map::new();
let i = Ini::load_from_str(text)?;
for (sec, prop) in i.iter() {
match sec {
Some(sec) => {
let mut sec_map: Map<String, Value> = Map::new();
for (k, v) in prop.iter() {
sec_map.insert(
k.to_owned(),
Value::new(uri, ValueKind::String(v.to_owned())),
);
}
map.insert(sec.to_owned(), Value::new(uri, ValueKind::Table(sec_map)));
}
None => {
for (k, v) in prop.iter() {
map.insert(
k.to_owned(),
Value::new(uri, ValueKind::String(v.to_owned())),
);
}
}
}
let value = from_ini(uri, Ini::load_from_str(text)?);

match value.kind {
ValueKind::Table(map) => Ok(map),

_ => Ok(Map::new()),
}
Ok(map)
}

fn from_ini(
uri: Option<&String>,
data: Ini,
) -> Value {
let mut map = Map::<String, Value>::new();

let mut sections: Map<Option<&str>, Table> = data.into_iter().map(|(section, props)| {
let key = section;
let value = props.iter().map(|(k, v)| {
let key = k.to_owned();
let value = Value::new(uri, ValueKind::String(v.to_owned()));
(key, value)
}).collect();
(key, value)
}).collect();

// Hoist (optional) sectionless properties to the top-level, alongside sections:
map.extend(sections.remove(&None).unwrap_or_default());

// Wrap each section Table into Value for merging into `map`:
map.extend(sections.into_iter().map(|(k,v)| {
let key = k.unwrap_or_default().to_owned();
let value = Value::new(uri, ValueKind::Table(v));
(key , value)
}));

Value::new(uri, ValueKind::Table(map))
}