diff options
author | saber.wu <saber.wu@trantect.com> | 2018-06-14 17:09:43 +0800 |
---|---|---|
committer | saber.wu <saber.wu@trantect.com> | 2018-06-15 11:13:53 +0800 |
commit | ba6014543dfb4040921bb4809c6b293cfdf33c84 (patch) | |
tree | 523708c74eb31e5c861c7e474f092fafbd416879 /src | |
parent | e8fa9fee96185ddd18ebcef8a925c75459111edb (diff) |
support ini
Diffstat (limited to 'src')
-rw-r--r-- | src/file/format/ini.rs | 32 | ||||
-rw-r--r-- | src/file/format/mod.rs | 13 | ||||
-rw-r--r-- | src/lib.rs | 3 |
3 files changed, 48 insertions, 0 deletions
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<HashMap<String, Value>, Box<Error + Send + Sync>> { + let mut map: HashMap<String, Value> = 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<String, Value> = 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), } } } @@ -43,6 +43,9 @@ extern crate yaml_rust; #[cfg(feature = "hjson")] extern crate serde_hjson; +#[cfg(feature = "ini")] +extern crate ini; + mod error; mod value; mod de; |