From ba6014543dfb4040921bb4809c6b293cfdf33c84 Mon Sep 17 00:00:00 2001 From: "saber.wu" Date: Thu, 14 Jun 2018 17:09:43 +0800 Subject: support ini --- src/file/format/ini.rs | 32 ++++++++++++++++++++++++++++++++ src/file/format/mod.rs | 13 +++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/file/format/ini.rs (limited to 'src/file') diff --git a/src/file/format/ini.rs b/src/file/format/ini.rs new file mode 100644 index 0000000..b7c0f71 --- /dev/null +++ b/src/file/format/ini.rs @@ -0,0 +1,32 @@ +use source::Source; +use std::collections::HashMap; +use std::error::Error; +use value::{Value, ValueKind}; +use ini::Ini; + +pub fn parse( + uri: Option<&String>, + text: &str, +) -> Result, Box> { + let mut map: HashMap = HashMap::new(); + let i = Ini::load_from_str(text)?; + for (sec, prop) in i.iter() { + match *sec { + Some(ref sec) => { + let mut sec_map: HashMap = HashMap::new(); + for (k, v) in prop.iter() { + sec_map.insert(k.to_lowercase().clone(), + Value::new(uri, ValueKind::String(v.clone()))); + } + map.insert(sec.to_lowercase().clone(), Value::new(uri, ValueKind::Table(sec_map))); + } + None => { + for (k, v) in prop.iter() { + map.insert(k.to_lowercase().clone(), + Value::new(uri, ValueKind::String(v.clone()))); + } + } + } + } + Ok(map) +} diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 5dfdfde..65e2914 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -19,6 +19,9 @@ mod yaml; #[cfg(feature = "hjson")] mod hjson; +#[cfg(feature = "ini")] +mod ini; + #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum FileFormat { /// TOML (parsed with toml) @@ -36,6 +39,10 @@ pub enum FileFormat { /// HJSON (parsed with serde_hjson) #[cfg(feature = "hjson")] Hjson, + /// INI (parsed with serde_hjson) + #[cfg(feature = "ini")] + Ini, + } lazy_static! { @@ -56,6 +63,9 @@ lazy_static! { #[cfg(feature = "hjson")] formats.insert(FileFormat::Hjson, vec!["hjson"]); + #[cfg(feature = "ini")] + formats.insert(FileFormat::Ini, vec!["ini"]); + formats }; } @@ -90,6 +100,9 @@ impl FileFormat { #[cfg(feature = "hjson")] FileFormat::Hjson => hjson::parse(uri, text), + + #[cfg(feature = "ini")] + FileFormat::Ini => ini::parse(uri, text), } } } -- cgit v1.2.3