From 34f39278f2d5c368c0e2282a3914dbb8070073f0 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:27:09 +1300 Subject: [PATCH 1/3] refactor: `ini` format parser Intended to be easier to grok and match the same flow as other format parsers. Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/ini.rs | 60 ++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/file/format/ini.rs b/src/file/format/ini.rs index 9295e60e..f495b5c1 100644 --- a/src/file/format/ini.rs +++ b/src/file/format/ini.rs @@ -3,35 +3,45 @@ 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, Box> { - let mut map: Map = Map::new(); - let i = Ini::load_from_str(text)?; - for (sec, prop) in i.iter() { - match sec { - Some(sec) => { - let mut sec_map: Map = 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()), + } +} + +fn from_ini( + uri: Option<&String>, + data: Ini, +) -> Value { + let mut map = Map::new(); + + let mut sections: Map, Table> = data.into_iter().map(|(section, props)| {( + section, + props.iter().map(|(k, v)| {( + k.to_owned(), + Value::new(uri, ValueKind::String(v.to_owned())), + )}).collect() + )}).collect(); + + // These (optional) properties should exist top-level alongside sections: + if let Some(sectionless) = sections.remove(&None) { + map.extend(sectionless); } - Ok(map) + + // Wrap each section Table into Value for merging into `map`: + map.extend(sections.into_iter().map(|(k,v)| {( + k.unwrap_or_default().to_owned(), + Value::new(uri, ValueKind::Table(v)), + )})); + + Value::new(uri, ValueKind::Table(map)) } From 8ace2cd9b40c1fdcc1916f1f76267e2f784355a7 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:50:10 +1300 Subject: [PATCH 2/3] chore: Apply review feedback Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/ini.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/file/format/ini.rs b/src/file/format/ini.rs index f495b5c1..9cd6b9fa 100644 --- a/src/file/format/ini.rs +++ b/src/file/format/ini.rs @@ -22,26 +22,26 @@ fn from_ini( uri: Option<&String>, data: Ini, ) -> Value { - let mut map = Map::new(); + let mut map = Map::::new(); let mut sections: Map, Table> = data.into_iter().map(|(section, props)| {( section, - props.iter().map(|(k, v)| {( - k.to_owned(), - Value::new(uri, ValueKind::String(v.to_owned())), - )}).collect() + props.iter().map(|(k, v)| { + let key = k.to_owned(); + let value = Value::new(uri, ValueKind::String(v.to_owned())); + (key, value) + }).collect() )}).collect(); - // These (optional) properties should exist top-level alongside sections: - if let Some(sectionless) = sections.remove(&None) { - map.extend(sectionless); - } + // 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)| {( - k.unwrap_or_default().to_owned(), - Value::new(uri, ValueKind::Table(v)), - )})); + 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)) } From ff60fb656f22ab062db5819d5d96e84275bb22c0 Mon Sep 17 00:00:00 2001 From: polarathene <5098581+polarathene@users.noreply.github.com> Date: Sat, 7 Oct 2023 01:00:38 +1300 Subject: [PATCH 3/3] chore: Expand KV tuple for improved readability Signed-off-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- src/file/format/ini.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/file/format/ini.rs b/src/file/format/ini.rs index 9cd6b9fa..f7a25a5c 100644 --- a/src/file/format/ini.rs +++ b/src/file/format/ini.rs @@ -24,14 +24,15 @@ fn from_ini( ) -> Value { let mut map = Map::::new(); - let mut sections: Map, Table> = data.into_iter().map(|(section, props)| {( - section, - props.iter().map(|(k, v)| { + let mut sections: Map, 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() - )}).collect(); + }).collect(); + (key, value) + }).collect(); // Hoist (optional) sectionless properties to the top-level, alongside sections: map.extend(sections.remove(&None).unwrap_or_default());