diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2018-07-02 15:26:09 -0700 |
---|---|---|
committer | Ryan Leckey <leckey.ryan@gmail.com> | 2018-07-02 15:26:09 -0700 |
commit | 0226b134bb1871fa4918b52a83073db748bc1b4d (patch) | |
tree | 49b60220b559b264e2e7d0b7f9041d9b4472310a /src | |
parent | 5e5f1a7f000dc227070ccb4bd8e0e3c1c4948282 (diff) | |
parent | aa427d992894f4a098bd77b20d9f10801bc916e7 (diff) |
Merge branch 'feature/ini' of https://github.com/woodgear/config-rs
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..c01c386 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 rust_ini) + #[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), } } } @@ -48,6 +48,9 @@ extern crate yaml_rust; #[cfg(feature = "hjson")] extern crate serde_hjson; +#[cfg(feature = "ini")] +extern crate ini; + mod error; mod value; mod de; |