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 +++++++++++++ src/lib.rs | 3 +++ 3 files changed, 48 insertions(+) create mode 100644 src/file/format/ini.rs (limited to 'src') 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), } } } diff --git a/src/lib.rs b/src/lib.rs index 6178375..ad205d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; -- cgit v1.2.3 From f259b6069ddf92becdb5edf300b8e2c2841b4812 Mon Sep 17 00:00:00 2001 From: "saber.wu" Date: Fri, 15 Jun 2018 13:15:43 +0800 Subject: fix doc test --- src/value.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/value.rs b/src/value.rs index a8ae94a..5d7562c 100644 --- a/src/value.rs +++ b/src/value.rs @@ -119,19 +119,17 @@ pub struct Value { /// A description of the original location of the value. /// /// A Value originating from a File might contain: - /// ``` + /// /// Settings.toml - /// ``` /// /// A Value originating from the environment would contain: - /// ``` + /// /// the envrionment - /// ``` /// /// A Value originating from a remote source might contain: - /// ``` + /// /// etcd+http://127.0.0.1:2379 - /// ``` + /// origin: Option, /// Underlying kind of the configuration value. -- cgit v1.2.3 From aa427d992894f4a098bd77b20d9f10801bc916e7 Mon Sep 17 00:00:00 2001 From: "san.dai" Date: Mon, 25 Jun 2018 16:38:06 +0800 Subject: option rust-ini --- src/file/format/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 65e2914..c01c386 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -39,7 +39,7 @@ pub enum FileFormat { /// HJSON (parsed with serde_hjson) #[cfg(feature = "hjson")] Hjson, - /// INI (parsed with serde_hjson) + /// INI (parsed with rust_ini) #[cfg(feature = "ini")] Ini, -- cgit v1.2.3